0
点赞
收藏
分享

微信扫一扫

Spring----配置文件(bean,AOP,事务······等)总结

耳一文 2023-05-05 阅读 115


首先来看一个标准的Spring配置文件 applicationContext.xml

 



[html] 
      
    view plain 
    copy 
    

        
    
 
  
1. <?xml version="1.0" encoding="UTF-8"?>  
2. <beans xmlns="http://www.springframework.org/schema/beans"  
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
4. xmlns:context="http://www.springframework.org/schema/context"  
5. xmlns:tx="http://www.springframework.org/schema/tx"  
6. xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
7.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
8.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"  
9. default-autowire="byName" default-lazy-init="true">  
10.   
11. <!-- 配置数据源 -->  
12. <bean id="dataSource"  
13. class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
14. <property name="driverClassName">  
15. <value>com.mysql.jdbc.Driver</value>  
16. </property>  
17. <property name="url">  
18. <value>  
19. characterEncoding=utf-8  
20. </value>  
21. </property>  
22. <property name="username">  
23. <value>root</value>  
24. </property>  
25. <property name="password">  
26. <value>123</value>  
27. </property>  
28. </bean>  
29.   
30. <!--配置SessionFactory -->  
31. <bean id="sessionFactory"  
32. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
33. <property name="dataSource">  
34. <ref bean="dataSource" />  
35. </property>  
36. <property name="mappingResources">  
37. <list>  
38. <value>com/ssh/pojo/User.hbm.xml</value>  
39. </list>  
40. </property>  
41. <property name="hibernateProperties">  
42. <props>  
43. <prop key="hibernate.show_sql">true</prop>  
44. </props>  
45. </property>  
46. </bean>  
47.    
48. <!-- 事务管理 -->  
49. <bean id="transactionManager"  
50. class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
51. <property name="sessionFactory">  
52. <ref bean="sessionFactory" />  
53. </property>  
54. </bean>  
55.    
56. <!-- hibernateTemplate -->  
57. <bean id="hibernateTemplate"  
58. class="org.springframework.orm.hibernate3.HibernateTemplate">  
59. <property name="sessionFactory">  
60. <ref bean="sessionFactory" />  
61. </property>  
62. </bean>  
63.   
64. <!-- 配置数据持久层 -->  
65. <bean id="userDao"  
66. class="com.ssh.dao.impl.UserDaoImpl">  
67. <property name="hibernateTemplate" ref="hibernateTemplate"></property>  
68. </bean>  
69.    
70.   
71. <!-- 配置业务逻辑层 -->  
72. <bean id="userService"  
73. class="com.ssh.service.impl.UserServiceImpl">  
74. <property name="userDao" ref="userDao"></property>  
75. </bean>  
76.    
77.   
78. <!-- 配置控制层 -->  
79. <bean id="UserAction"  
80. class="com.ssh.action.UserAction"  scope="prototype">  
81. <property name="userService" ref="userService"></property>  
82. </bean>  
83. <!-- 配置pojo -->  
84. <bean id="User" class="com.ssh.pojo.User" scope="prototype"/>  
85. </beans>

 

下面是详解:

 

[html] 
      
    view plain 
    copy 
    

        
    
 
  
1. 1.基本配置:  
2. <?xml version="1.0" encoding="UTF-8"?>  
3. <beans  
4. xmlns="http://www.springframework.org/schema/beans"  
5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
6. xmlns:context="http://www.springframework.org/schema/context"  
7. xsi:schemaLocation="http://www.springframework.org/schema/beans   
8.                     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
9.                     http://www.springframework.org/schema/context  
10.                     http://www.springframework.org/schema/context/spring-context-2.5.xsd  
11. >  
12.   
13.   
14. <context:component-scan base-package="com.persia">  
15. <!-- 开启组件扫描 -->  
16. </context:component-scan>  
17.   
18. <context:annotation-config>  
19. <!--开启注解处理器-->  
20. </context:annotation-config>  
21.   
22. <!-- 使用注解,省去了propertity的xml配置,减少xml文件大小 -->  
23. <bean id="personServiceAnno" class="com.persia.PersonServiceAnnotation"></bean>  
24. <bean id="personDaoBeanAnno" class="com.persia.PersonDaoBean"></bean>  
25. <bean id="personDaoBeanAnno2" class="com.persia.PersonDaoBean"></bean>  
26.   
27. <!-- 自动注解 -->  
28. <bean id="personServiceAutoInject" class="com.persia.PersonServiceAutoInject" autowire="byName"></bean>  
29.   
30.   
31. <bean id="personService" class="com.persia.PersonServiceBean">  
32. <!-- 由spring容器去创建和维护,我们只要获取就可以了 -->  
33. </bean>  
34.   
35. <bean id="personService2" class="com.persia.PersonServiceBeanFactory" factory-method="createInstance" lazy-init="true"   
36. init-method="init"  destroy-method="destory">  
37. <!-- 静态工厂获取bean -->  
38. </bean>  
39.   
40. <bean id="fac" class="com.persia.PersonServiceBeanInsFactory"></bean>  
41. <bean id="personService3" factory-bean="fac" factory-method="createInstance" scope="prototype">  
42. <!-- 实例工厂获取bean,先实例化工厂再实例化bean-->  
43. </bean>  
44.   
45.   
46. <!-- ref方式注入属性 -->  
47. <bean id="personDao" class="com.persia.PersonDaoBean"></bean>  
48. <bean id="personService4" class="com.persia.PersonServiceBean">  
49. <property name="personDao" ref="personDao"></property>  
50. </bean>  
51.   
52. <!-- 内部bean方式注入 -->  
53. <bean id="personService5" class="com.persia.PersonServiceBean">  
54. <property name="personDao">  
55. <bean class="com.persia.PersonDaoBean"></bean>  
56. </property>  
57. <property name="name" value="persia"></property>  
58. <property name="age" value="21"></property>  
59.     
60. <property name="sets">  
61. <!-- 集合的注入 -->  
62. <set>  
63. <value>第一个</value>  
64. <value>第二个</value>  
65. <value>第三个</value>  
66. </set>  
67. </property>  
68.     
69. <property name="lists">  
70. <!-- 集合的注入 -->  
71. <list>  
72. <value>第一个l</value>  
73. <value>第二个l</value>  
74. <value>第三个l</value>  
75. </list>  
76.       
77. </property>  
78.     
79. <property name="properties">  
80. <props>  
81. <prop key="key1">value1</prop>  
82. <prop key="key2">value2</prop>  
83. <prop key="key3">value3</prop>  
84. </props>  
85. </property>  
86.     
87. <property name="map">  
88. <map>  
89. <entry key="key1" value="value-1"></entry>  
90. <entry key="key2" value="value-2"></entry>  
91. <entry key="key3" value="value-3"></entry>  
92. </map>  
93. </property>  
94. </bean>  
95.   
96. <bean id="personService6" class="com.persia.PersonServiceBean">  
97. <constructor-arg index="0" value="构造注入的name" ></constructor-arg>  
98. <!-- 基本类型可以不写type -->  
99. <constructor-arg index="1" type="com.persia.IDaoBean" ref="personDao">  
100. </constructor-arg>   
101. </bean>  
102.   
103. </beans>




[html]   view plain copy

1. 2.开启AOP:  
2. <?xml version="1.0" encoding="UTF-8"?>  
3. <beans  
4. xmlns="http://www.springframework.org/schema/beans"  
5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
6. xmlns:context="http://www.springframework.org/schema/context"  
7. xmlns:aop="http://www.springframework.org/schema/aop"  
8. xsi:schemaLocation="http://www.springframework.org/schema/beans   
9.                     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
10.                      http://www.springframework.org/schema/aop  
11.                     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
12.                     http://www.springframework.org/schema/context  
13.                     http://www.springframework.org/schema/context/spring-context-2.5.xsd  
14. >  
15.   
16. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>  
17. <bean id="myInterceptor" class="com.persia.service.MyInterceptor"></bean>  
18. <bean id="personServiceImpl" class="com.persia.service.impl.PersonServiceImpl"></bean>  
19. </beans>AOP的xml版本<?xml version="1.0" encoding="UTF-8"?>  
20. <beans  
21. xmlns="http://www.springframework.org/schema/beans"  
22. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
23. xmlns:context="http://www.springframework.org/schema/context"  
24. xmlns:aop="http://www.springframework.org/schema/aop"  
25. xsi:schemaLocation="http://www.springframework.org/schema/beans   
26.                     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
27.                      http://www.springframework.org/schema/aop  
28.                     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
29.                     http://www.springframework.org/schema/context  
30.                     http://www.springframework.org/schema/context/spring-context-2.5.xsd  
31. >  
32.   
33. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>  
34.   
35. <bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean>  
36. <bean id="aspectBean" class="com.persia.service.MyInterceptor"></bean>  
37.   
38. <aop:config>  
39. <aop:aspect id="myaop" ref="aspectBean">  
40. <aop:pointcut id="mycut" expression="execution(* com.persia.service.impl.PersonServiceImpl.*(..))"/>  
41. <aop:pointcut id="argcut" expression="execution(* com.persia.service.impl.PersonServiceImpl.*(..)) and args(name)"/>    
42. <aop:before pointcut-ref="mycut" method="doAccessCheck"  />  
43. <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>  
44. <aop:after-throwing pointcut-ref="mycut" method="doThrowing"/>  
45. <aop:after pointcut-ref="argcut" method="doAfter" arg-names="name"/>  
46. <aop:around pointcut-ref="mycut" method="arround"/>  
47. </aop:aspect>  
48.     
49. </aop:config>  
50.   
51. </beans>  
 
 
 
    [html] 
      
    view plain 
    copy 
    

        
    
 
  
1. 3.开启事务和注解:  
2. <?xml version="1.0" encoding="UTF-8"?>  
3. <beans  
4. xmlns="http://www.springframework.org/schema/beans"  
5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
6. xmlns:context="http://www.springframework.org/schema/context"  
7. xmlns:aop="http://www.springframework.org/schema/aop"  
8. xmlns:tx="http://www.springframework.org/schema/tx"   
9. xsi:schemaLocation="http://www.springframework.org/schema/beans   
10.                     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
11.                     http://www.springframework.org/schema/context  
12.                     http://www.springframework.org/schema/context/spring-context-2.5.xsd  
13.                     http://www.springframework.org/schema/aop  
14.                     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
15.                     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
16. >  
17.   
18. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>  
19.                      
20. <!-- 配置数据源 -->     
21. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">     
22. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>     
23. <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>     
24. <property name="username" value="root"/>     
25. <property name="password" value=""/>     
26. <!-- 连接池启动时的初始值 -->     
27. <property name="initialSize" value="1"/>     
28. <!-- 连接池的最大值 -->     
29. <property name="maxActive" value="500"/>     
30. <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->     
31. <property name="maxIdle" value="2"/>     
32. <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->     
33. <property name="minIdle" value="1"/>     
34. </bean>    
35.      
36. <!-- 配置事务管理器-->     
37. <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">     
38. <property name="dataSource" ref="dataSource"/>     
39. </bean>    
40. <!-- 配置业务bean -->  
41. <bean id="personService" class="com.persia.service.impl.PersonServiceImpl">  
42. <property name="ds" ref="dataSource"></property>  
43. </bean>  
44.      
45. <!-- 采用@Transactional注解方式来使用事务 -->     
46. <tx:annotation-driven transaction-manager="txManager"/>   
47.   
48.   
49. </beans>  
 
 
 
    [html] 
      
    view plain 
    copy 
    

        
    
 
  
1. XML版本:  
2. <?xml version="1.0" encoding="UTF-8"?>  
3. <beans  
4. xmlns="http://www.springframework.org/schema/beans"  
5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
6. xmlns:context="http://www.springframework.org/schema/context"  
7. xmlns:aop="http://www.springframework.org/schema/aop"  
8. xmlns:tx="http://www.springframework.org/schema/tx"   
9. xsi:schemaLocation="http://www.springframework.org/schema/beans   
10.                     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
11.                     http://www.springframework.org/schema/context  
12.                     http://www.springframework.org/schema/context/spring-context-2.5.xsd  
13.                     http://www.springframework.org/schema/aop  
14.                     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
15.                     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
16. >  
17.   
18. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>  
19.                      
20. <!-- 配置数据源 -->     
21. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">     
22. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>     
23. <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>     
24. <property name="username" value="root"/>     
25. <property name="password" value=""/>     
26. <!-- 连接池启动时的初始值 -->     
27. <property name="initialSize" value="1"/>     
28. <!-- 连接池的最大值 -->     
29. <property name="maxActive" value="500"/>     
30. <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->     
31. <property name="maxIdle" value="2"/>     
32. <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->     
33. <property name="minIdle" value="1"/>     
34. </bean>    
35.      
36. <!-- 配置事务管理器 -->  
37. <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">     
38. <property name="dataSource" ref="dataSource"/>     
39. </bean>    
40. <!-- 配置业务bean -->  
41. <bean id="personService" class="com.persia.service.impl.PersonServiceImpl">  
42. <property name="ds" ref="dataSource"></property>  
43. </bean>  
44.     
45.     
46. <!-- 使用XML来使用事务管理-->    
47. <aop:config>    
48. <!-- 配置一个切面,和需要拦截的类和方法 -->     
49. <aop:pointcut id="transactionPointcut" expression="execution(* com.persia.service..*.*(..))"/>    
50. <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>    
51. </aop:config>   
52. <!-- 配置一个事务通知 -->      
53. <tx:advice id="txAdvice" transaction-manager="txManager">    
54. <tx:attributes>   
55. <!-- 方法以get开头的,不使用事务 -->   
56. <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>   
57. <!-- 其他方法以默认事务进行 -->   
58. <tx:method name="*"/>    
59. </tx:attributes>    
60. </tx:advice>    
61.      
62.     
63. </beans>  
 
 
 
    [html] 
      
    view plain 
    copy 
    

        
    
 
  
1. 4.SSH:  
2. <?xml version="1.0" encoding="UTF-8"?>  
3. <beans  
4. xmlns="http://www.springframework.org/schema/beans"  
5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
6. xmlns:context="http://www.springframework.org/schema/context"  
7. xmlns:aop="http://www.springframework.org/schema/aop"  
8. xmlns:tx="http://www.springframework.org/schema/tx"   
9. xsi:schemaLocation="http://www.springframework.org/schema/beans   
10.                     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
11.                     http://www.springframework.org/schema/context  
12.                     http://www.springframework.org/schema/context/spring-context-2.5.xsd  
13.                     http://www.springframework.org/schema/aop  
14.                     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
15.                     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
16. >  
17.   
18.   
19. <!-- 配置数据源 -->     
20. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">     
21. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>     
22. <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>     
23. <property name="username" value="root"/>     
24. <property name="password" value=""/>     
25. <!-- 连接池启动时的初始值 -->     
26. <property name="initialSize" value="1"/>     
27. <!-- 连接池的最大值 -->     
28. <property name="maxActive" value="500"/>     
29. <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->     
30. <property name="maxIdle" value="2"/>     
31. <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->     
32. <property name="minIdle" value="1"/>     
33. </bean>    
34.     
35. <!-- 配置hibernate的sessionFactory -->  
36. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
37. <property name="dataSource"><ref bean="dataSource" /></property>  
38. <property name="mappingResources">  
39. <list>  
40. <value>com/persia/model/Person.hbm.xml</value>  
41. </list>  
42. </property>  
43.      
44. <!-- 1.首先在sessionFactory里面配置以上3条设置 -->  
45. <!-- 2.然后得在类路径下面添加一个ehcache.xml的缓存配置文件 -->  
46. <!-- 3.最后在要使用缓存的实体bean的映射文件里面配置缓存设置 -->  
47. <!--使用二级缓存-->   
48. <!-- 不使用查询缓存,因为命中率不是很高 -->   
49. <!-- 使用Ehcache缓存产品 -->    
50. <property name="hibernateProperties">  
51. <value>  
52. hibernate.dialect=org.hibernate.dialect.MySQL5Dialect  
53. hibernate.hbm2ddl.auto=update  
54. hibernate.show_sql=false  
55. hibernate.format_sql=false  
56. hibernate.cache.use_second_level_cache=true  
57. hibernate.cache.use_query_cache=false  
58. hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider  
59. </value>  
60. </property>  
61. </bean>  
62.   
63. <!-- 配置Spring针对hibernate的事务管理器 -->  
64. <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
65. <property name="sessionFactory" ref="sessionFactory"/>  
66. </bean>  
67.   
68. <!-- 配置使用注解的方式来使用事务 -->   
69. <tx:annotation-driven transaction-manager="txManager"/>  
70.   
71. <!-- 使用手工配置的注解方式来注入bean -->  
72. <context:annotation-config></context:annotation-config>  
73.   
74. <!--定义要注入的业务bean -->  
75. <bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean>  
76.   
77. <!--将Struts的action交给Spring容器来管理 -->  
78. <bean name="/person/list" class="com.persia.struts.PersonListAction">  
79. <!--1.这里要求name和struts-config里面的action的path名称一致,因为id不允许有特殊字符-->  
80. <!--2.还得在Struts-config文件里面添加Spring的请求处理器,该处理器会根据action的path属性到Spring容器里面寻找这个bean,若找到了则用这个bean来处理用户的请求-->  
81. <!--3.然后去掉action的type标签和值(可选),当Spring处理器找不到该bean时,才会使用Struts的action-->  
82. <!--4.最后在action里面使用Spring的注入方式来注入业务bean-->  
83. </bean>  
84.   
85. <bean name="/person/manage" class="com.persia.struts.PersonManageAction"></bean>  
86. </beans>  
 
 
 
    [html] 
      
    view plain 
    copy 
    

        
    
 
  
1. 5.SSH2:  
2. <?xml version="1.0" encoding="UTF-8"?>  
3. <beans  
4. xmlns="http://www.springframework.org/schema/beans"  
5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
6. xmlns:context="http://www.springframework.org/schema/context"  
7. xmlns:aop="http://www.springframework.org/schema/aop"  
8. xmlns:tx="http://www.springframework.org/schema/tx"   
9. xsi:schemaLocation="http://www.springframework.org/schema/beans   
10.                     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
11.                     http://www.springframework.org/schema/context  
12.                     http://www.springframework.org/schema/context/spring-context-2.5.xsd  
13.                     http://www.springframework.org/schema/aop  
14.                     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
15.                     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
16. >  
17.   
18.   
19. <!-- 配置数据源 -->     
20. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">     
21. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>     
22. <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>     
23. <property name="username" value="root"/>     
24. <property name="password" value=""/>     
25. <!-- 连接池启动时的初始值 -->     
26. <property name="initialSize" value="1"/>     
27. <!-- 连接池的最大值 -->     
28. <property name="maxActive" value="500"/>     
29. <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->     
30. <property name="maxIdle" value="2"/>     
31. <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->     
32. <property name="minIdle" value="1"/>     
33. </bean>    
34.     
35. <!-- 配置hibernate的sessionFactory -->  
36. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
37. <property name="dataSource"><ref bean="dataSource" /></property>  
38. <property name="mappingResources">  
39. <list>  
40. <value>com/persia/model/Person.hbm.xml</value>  
41. </list>  
42. </property>  
43.      
44. <!-- 1.首先在sessionFactory里面配置以上3条设置 -->  
45. <!-- 2.然后得在类路径下面添加一个ehcache.xml的缓存配置文件 -->  
46. <!-- 3.最后在要使用缓存的实体bean的映射文件里面配置缓存设置 -->  
47. <!--使用二级缓存-->   
48. <!-- 不使用查询缓存,因为命中率不是很高 -->   
49. <!-- 使用Ehcache缓存产品 -->    
50. <property name="hibernateProperties">  
51. <value>  
52. hibernate.dialect=org.hibernate.dialect.MySQL5Dialect  
53. hibernate.hbm2ddl.auto=update  
54. hibernate.show_sql=false  
55. hibernate.format_sql=false  
56. hibernate.cache.use_second_level_cache=true  
57. hibernate.cache.use_query_cache=false  
58. hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider  
59. </value>  
60. </property>  
61. </bean>  
62.   
63. <!-- 配置Spring针对hibernate的事务管理器 -->  
64. <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
65. <property name="sessionFactory" ref="sessionFactory"/>  
66. </bean>  
67.   
68. <!-- 配置使用注解的方式来使用事务 -->   
69. <tx:annotation-driven transaction-manager="txManager"/>  
70.   
71. <!-- 使用手工配置的注解方式来注入bean -->  
72. <context:annotation-config></context:annotation-config>  
73.   
74. <!--定义要注入的业务bean -->  
75. <bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean>  
76.   
77. <!--注入Struts 2的action -->  
78. <bean id="personList" class="com.persia.struts2.action.PersonListAction"></bean>  
79. </beans>  
 
 
 
    [html] 
      
    view plain 
    copy 
    

        
    
 
  
1. 6.SSJ:  
2. <?xml version="1.0" encoding="UTF-8"?>  
3. <beans  
4. xmlns="http://www.springframework.org/schema/beans"  
5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
6. xmlns:context="http://www.springframework.org/schema/context"  
7. xmlns:aop="http://www.springframework.org/schema/aop"  
8. xmlns:tx="http://www.springframework.org/schema/tx"   
9. xsi:schemaLocation="http://www.springframework.org/schema/beans   
10.                     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
11.                     http://www.springframework.org/schema/context  
12.                     http://www.springframework.org/schema/context/spring-context-2.5.xsd  
13.                     http://www.springframework.org/schema/aop  
14.                     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
15.                     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
16. >  
17.   
18.   
19. <!-- 使用手工配置的注解方式来注入bean -->  
20. <context:annotation-config></context:annotation-config>  
21.   
22. <!-- 1.配置Spring集成JPA -->  
23. <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">  
24. <property name="persistenceUnitName" value="SpringJPAPU"/>  
25. </bean>  
26.   
27. <!--2.配置Spring针对JPA的事务 -->  
28. <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
29. <property name="entityManagerFactory" ref="entityManagerFactory"/>  
30. </bean>  
31.   
32. <!--3.开启事务注解 -->  
33. <tx:annotation-driven transaction-manager="txManager"/>  
34.     
35. <!--以上3个Spring集成JPA的配置,在web项目先添加Spring支持,后添加JPA支持时会自动生成 -->  
36.   
37. <!-- 配置业务bean -->  
38. <bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean>  
39.   
40. <!-- 配置Struts的action -->  
41. <bean name="/person/list" class="com.persia.struts.PersonListAction"/>  
42. <bean name="/person/manage" class="com.persia.struts.PersonManageAction"/>  
43. </beans>


 

Spring加载多个配置文件

对于大多数的应用,从表现层的action,到持久层的DataSource,都被Spring 作为
bean 管理。如果这些bean 被配置在同一个文件中,阅读及维护该配置文件将是一件非
常有挑战的事情。
因此, Spring 建议:将一个大的配置文件分解成多个小的配置文件,使每个配置文
件仅仅管理功能近似于bean; 这样不仅可以分散配置文件,降低修改配置文件的风险,
而且更符合"分而治之"的软件工程原理。
多个配置文件最终需要汇总, ApplicationContext提供如下方式来汇总多个配置文件:
.使用App1icationContext 加载多个配置文件。
• Web 应用启动时加载多个配置文件。
• XML 配置文件中导入其他配置。

1 ApplicationContext 加载多个配置文件 
ApplicatonContext 的常用实现类有如下两个:
• ClassPathXm1 ApplicationContext 。
• FileSystemXm1ApplicationContext 。
这两个类都可以用来加载多个配置文件,它们的构造器都可以接收一个数组,并在
该数组中存放多个配置文件。ClassPathXm1ApplicationContext 可采用如下代码加载多个
配置文件:
/I创建配置文件数组
/I假设有3 个配置文件: a.xml , b.xml , c.xml
Str工ng[) configLocations = {"a.xml" , "b.xml" , "c.xml"}
以配置文件数组为参数,创建ApplicationContext
ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocations);
与采用FileSystemXm1ApplicationContext创建ApplicationContext的方式相似,区别
仅在于二者搜索配置文件的路径不同:ClassPathXm1ApplicationContext通过CLASSPATH
路径搜索配置文件:而FileSystemXm1ApplicationContext则在当前路径搜索配置文件。
2 Web 应用启动时加载多个配置文件 
参看5.12.3 节所述,通过ContextLoaderListener 也可加载多个配置文件,可利用
337
轻量级J2EE 企业应用实战一一-Struts+Spring+Hibernate 整合开发
<context-pararn>元素来指定多个配置文件位置,其配置如下:
< l-- 确定配置文件的位置一〉
<context-param>
< param-name>contextConfigLocation</param-name>
< 1-- 此处可以列出多个Spring 的XML 配置文件>
< param-value>/WEB-INF/daoContext.xml IWEB-INF/applicationContext.xml<1
param-value>
< context-param>
3 XML 配置文件中导人其他配置文件 
配置文件本身和其子元素import. 可用于导入其他配置文件。具体的配置示例如下:

 



[html]   view plain copy

 

1. <?xml version="1.0" encod工口g="gb2312"?>  
2. <!一指定Spring 配置文件的dtd>  
3. <!DOCTYPE beans PUBLIC "-IISPR工NGIIDTD BEANIIEN"  
4. ''http://www.springframework.org/dtd/spring-beans.dtd''>  
5. <!-- Spring 配置文件的根元素->  
6. <beans>  
7. <!一导入第→份配置文件: serv工ces.xml一〉  
8. <import resource="serv工ces.xml"l>  
9. <!-- 导入第二份配置文件: resources/messageSource.xml 一〉  
10. <import resource="resources/messageSource.xml"l>  
11. <!-- 导入第二份配置文件: resourcesl themeSource.xml -->  
12. <import resource="/resources/themeSource.xml"l>  
13. <!-- 下面定义该文件垦的其他bean…〉  
14. <bean id="beanl" class=". .."1>  
15. <bean id="bea口2" class="..."I>  
16. </beans>

举报

相关推荐

0 条评论