`

spring学习总结

阅读更多

本文总结一下spring2.5.6的知识点。

一、spring IOC的配置与使用

    1、IOC

          简而言之,IOC为控制反转。将自己new出来的对象,改为有容器进行赋值。具体有以下两种情况

          (1)初始化具体值 (2)灵活、动态的装配。

    2、XML的配置

           (1)、创建容器,命名为beans.xml,放在classpath下,大致配置如下

                      

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="xx" class="com.test.xx"> </bean> </beans>


      (2)注入类型

              Setter注入方式

                         

<bean id="x" class="com.test.xxx"> <property name="xxx" value="8"></property> </bean>


           构造方法注入方式

        

<bean id="x" class="com.test.xxx"> <constructor-arg> <ref bean="u"/> </constructor-arg> </bean>//注意这种方式要进行构造方法的定义


           接口注入方式

                代码略

        (3)简单属性的注入(int ,long,double)

           

<property name="xxx" value="8"></property>


 

      (4)bean 中的scope属性 

          singleton表示的是单例,prototype表示的是多例

   (5)集合注入<!--EndFragment-->

<!--EndFragment--><!--EndFragment-->

               

<bean name="x" class="com.test.xxx"> <property name="sets"> <set> <value>1</value> <value>2</value> </set> </property> <property name="lists"> <list> <value>1</value> <value>2</value> <value>3</value> </list> </property> <property name="maps"> <map> <entry key="1" value="1"></entry> <entry key="2" value="2"></entry> <entry key="3" value="3"></entry> <entry key="4" value="4"></entry> </map> </property> </bean>


     (6)自动装配

              autoWire  bean中的一个属性,常用的有byname,bytype

     (7)生命周期

             lazy-init=“true”:当容器初始化的时候,对应的bean不进行初始化

             init-method destroy-method不要和prototype一起用

  3、Annotation的配置

    (1)容器的配置

       

 

<!--EndFragment-->

    (2)@autoWire的配置

         默认按类型by type ,如果想用byName,使用@Qulifier, 如果写在set上,@qualifier需要写在参数上

    (3)@Resource<!--EndFragment-->

                需要加入  j2ee/common-annotations.jar,默认按名称,如果名称找不到,就按类型,可以 指定先按名称,不足:如果没有源码,就无法运用annotation,只能用xml

     (4)@Component

          初始化的名字默认为类名首字母小写,可以指定初始化bean的名字

    (5)@scope,@PostConstruct,@preDestroy

             用法略

二、spring AOP的配置与使用

       1、annotation的配置

              (1)xml配置

                    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.bjsxt"/> <aop:aspectj-autoproxy /> <!-- <aop:aspectj-autoproxy />这句的作用是:容器可以解析aop对应的annotation了。我们可以用@Aspect注解来建立我们的拦截类,可以用@Before等注解某个方法来建立处理方法。 @component 可以让容器来管理我们的拦截类 --> </beans>


         (2)annotation的具体例子

                

@Aspect @Component public class LogInterceptor { @Pointcut("execution(public * com.test.service..*.add(..))") public void myMethod(){}; @Before("myMethod()") public void before() { System.out.println("method before"); } @Around("myMethod()") public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable { System.out.println("method around start"); pjp.proceed(); System.out.println("method around end"); } }

    (3)常见的annotation

               @Pointcut @Before @AfterReturning @AfterThrowing @After @Around  

 2、xml的配置

              (1)xml的具体例子

                      

<bean id="interceptor" class="com.test.aop.Interceptor"></bean> <aop:config> <aop:aspect id="logAspect" ref="interceptor"> <aop:before method="before" pointcut="execution(public * com.test.service..*.add(..))" /> </aop:aspect> </aop:config>

   

 三、Spring整合Hibernate

           1、整合步骤

                  (1)创建datasource,将连接数据库的属性注入到datasource中

                          

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>

      (2)创建sessionFactory,将datasource注入到sessionFactory中

           

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="annotatedClasses"> <list> <value>com.test.Model.User</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean>

    (3)将sessionFactory注入到dao层。

     (4)jar包问题自己动手解决!

     

     2、声明式事务管理

              (1)事务一般加在service层。                                

               (2)xml的配置

                       

xmlns:tx="http://www.springframework.org/schema/tx" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

                (3)annotation的配置

                        

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="txManager"/>

       然后再某个方法上加上: @Transactional(propagation=Propagation.REQUIRED)

       注意不要使用opensession,要使用getcurrentsession

                 (4)xml配置

               

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="getUser" read-only="true"/> <tx:method name="add*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="busynessService" expression="execution(public * com.test.service.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="busynessService"/> </aop:config>

 (4)HibernateTemplate和HibernateDaoSupport

 

        通过向HibernateTemplate注入datasource,就可以用HibernateTemplate的save()等方法进行数据库数据的存取

 

 

      HibernateDaoSupport的用法为:先用一个类A继承它,然后向A的父类注入HibernateTemplate或者sessionFactory,然后用自己的DAO层的实现类继承它,然后就可以使用了!

 

 

 

 

<!--EndFragment-->

<!--EndFragment--><!--EndFragment-->

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics