Spring完全学习文档

IOC和AOP
1.搭建spring:(1)添加jar包(2)设置配置文件(3)
2.获取配置文件:
  AplicationContext apct=new ClassPathXmlApplicationContext("bean.xml");
  service=apct.getBean("获取一个bean");

IOC配置和应用
一,xml配置
1.注入方式
  (1)setter   @Resource
  (2)构造方法注入
  (3)结构注入
  Xml:<bean id="需要注入的名称(name)" class="">
     </bean>
     <bean id="" class="">
         <property name="名称" ref="引用名称"/>
         <constructor-arg>//构造方法的注入
             <ref bean="引用注入的名称(name)"/>
         </constructor-arg>
     </bean>
2.bean的scope:singleton(单例,一直都是一个对象),prototype(每声明一次将获取不同的对象),request,session,global session
3.集合注入:
  参考相关文档//set,list,map 的注入
  <property name="set">
     <set>
       <value></value>
       <value></value>
      </set>
  </property>
4.简单属性注入:<property name="属性名" value="值"/> 
5.自动装配:
  (1)在<bean标签上 autowire="(byName,byType....)"  默认按byName(按名字装配) byType(按类型来装配,如果同一类型有两个时将报错)
  (2)在beans标签上 default-autowire="byName"  指定全局的默认装配方式
6.生命周期
  <bean  lizy-init="true"  表示在用到时才会初始化该bean,而在启动服务器时并不进行初始化()
  <bean init-method="初始化加载的方法" destroy-method="结束的方法"

二,Annotation配置
1.修改bean.xml配置文件加入xmlns命名空间和<context:annotation-config/>
2.自动装配: 在相应的属性上注解 @Autowired  @Qualifier(name="指定bean.xml中的bean的id")
  example:
(1) @Autowried
  public void setName(@Qualifier("name")String name){
  }
  @Required  //表示必须要写的方法,在初始化时将检查该项
  (2) @Resource(name="注入名") //重要
      在配置文件中设置:
      <context:component-scan base-package="指定需要查询的包"/> //使用该项将自动在包下面去查找
      在需要注入的类中注解:@Component(value="注入名")
      (还存在其他的注解:@Repostory,@Service,@Cntroller)
3.生命周期
  @Scope("prototype/singleton")  //生存范围
  构造完成之后:@PostConstruct  相当于xml中的init-method,指加载该类时执行的方法
  @PreDestroy 容器销毁之前执行的方法,相当于xml中的destroy-method.


面向切面编程(AOP)
一,Annotation配置AOP
1.添加日志逻辑

2.添加xmlns命名空间,在添加配置<aop:aspectj-autoproxy/>  //使用aspectj注解
  (aspectj是一个专门用来产生代理(面向AOP)的框架)
 
  在切面类上配置注解 @Aspect @Component, 在方法上配置 @Before(植入点语法)
  植入点语法:
  例子:execution(public void cn.bywei.dao.impl.UserDaoImpl.save(cn.bywei.model.User))
       execution(* cn.bywei.dao.impl.*.*(*))
  JoinPoint  连接点
  PointCut  
  Aspect   切面
  Advice   切面上的逻辑(@before,@after),加在切入点上的建议
  Target  被代理的对象,把我们的逻辑植入的对象
  Weave   植入
   (1)@Pointcut("execution(* cn.bywei.dao.impl.*.*(*))")
      public void myMethod(){};
      @Before("myMethod()")
   (2)@After    //
   (3)@AfterThrowing  //当抛异常时
   (4)@Around("myMethod()")
      public void around(ProceedingJoinPoint pjp){
        pjp.proceed();
      }
二,xml配置AOP



数据源配置
1.dpcp 
  (1)直接在文件中配置数据库连接信息dbcp.BasicDataSource
  (2)配置一个
      <bean class="***.PropertyPlaceholderConfigurer">//通过占位符配置
        <property name="locations">
              <value>classpath:jdbc.properties</value>
       </property>
      </bean>
   dataSource中通过占位符来配置读取 jdbc.properties文件中的信息.
2.c3p0
3.proxool


Spring和Hibernate整合
1.建立SessionFactory
  在 LocalSessionFactoryBean中不支持Annotation
  而 可以使用 AnnotationSessionFactoryBean 来支持Annotation
  <property name="dataSource" ref="dataSource"/> //注入属性
 
  //配置需要注解的实体类
  (1)<property name="annotatedClasses">
       <list>
         <value>cn.bywei.model.User</value>
       </list>
   </property>

  (2)自动扫描包下面的实体类
    <property name="packageToScan">
       <list>
         <value>cn.bywei.model</value>
       </list>
    </property>
 
  //指定Hibernate的配置信息
  <property name="hibernateProperties">
     <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.show_sql">true</prop>
     </props
  </property>
2.aop主要使用在声明式事务管理
  Annotation:
  (1)在xml配置文件中添加命名空间
     添加事务管理 <tx:annotation-driven transaction-manager="txManager"/>
     添加<bean id="txManager" class="*****.HibernateTransactionManager">
                 <property name="sessionFactory" ref="sessionFactory"/>
         </bean>
     在一个方法上加事务:@Transactional
   Xml:
  

3.HibernateTemplate
  <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
     <property name="sessionFactory" ref="sessionFactory"/>
  </bean>
  使用:
   hibernateTemplate.save(User);
   hibernateTemplate封装了一系列的方法.
   callback() --回调
4.
  @Component
  public class SuerDao extends HibernateDaoSupport {
     @Resource("sessionFactory")
     public void setSuperSessionFactory(SessionFactory sessionFactory){
       super.setSessionFactory(sessionFactory);
     }
  }
  this.getHibernateTemplate().save(实体类);

5.初始化bean
  配置
  <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  引入配置文件:
   <contenxt-param>
      <param-name>contextconfig</param-name>
      <param-value>claspah:beans.xml</param-value>
  </context-param>
6.使用load时,配置把session打开到视图层
  <filter>
     <filter-name>openSessionInView</filter-name>
     <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInView</filter-class>
  </filter>
  <filter-mapping>**************
********</filter-mapping>

7.中文问题
  <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter>
        <init-param>
              <param-name>encoding</param-name>
              <param-value>GBK</param-value>
        </init-param>
  </filter>
  <filter-mapping>**************
********</filter-mapping>

猜你喜欢

转载自wjheye.iteye.com/blog/1035935
今日推荐