Spring配置模板
1.基本配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-2.5.xsd 10 "> 11 12 13 <context:component-scan base-package="com.persia"> 14 <!-- 开启组件扫描 --> 15 </context:component-scan> 16 17 <context:annotation-config> 18 <!--开启注解处理器--> 19 </context:annotation-config> 20 21 <!-- 使用注解,省去了propertity的xml配置,减少xml文件大小 --> 22 <bean id="personServiceAnno" class="com.persia.PersonServiceAnnotation"></bean> 23 <bean id="personDaoBeanAnno" class="com.persia.PersonDaoBean"></bean> 24 <bean id="personDaoBeanAnno2" class="com.persia.PersonDaoBean"></bean> 25 26 <!-- 自动装配 --> 27 <bean id="personServiceAutoInject" class="com.persia.PersonServiceAutoInject" autowire="byName"></bean> 28 29 30 <bean id="personService" class="com.persia.PersonServiceBean"> 31 <!-- 由spring容器去创建和维护,我们只要获取就可以了 --> 32 </bean> 33 34 <bean id="personService2" class="com.persia.PersonServiceBeanFactory" factory-method="createInstance" 35 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>
2.开启AOP:
注解方式:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:aop="http://www.springframework.org/schema/aop" 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/aop 10 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-2.5.xsd 13 "> 14 15 <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 16 <bean id="myInterceptor" class="com.persia.service.MyInterceptor"></bean> 17 <bean id="personServiceImpl" class="com.persia.service.impl.PersonServiceImpl"></bean> 18 </beans>
XML方式:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:aop="http://www.springframework.org/schema/aop" 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/aop 10 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-2.5.xsd 13 "> 14 15 <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 16 17 <bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean> 18 <bean id="aspectBean" class="com.persia.service.MyInterceptor"></bean> 19 20 <aop:config> 21 <aop:aspect id="myaop" ref="aspectBean"> 22 <aop:pointcut id="mycut" expression="execution(* com.persia.service.impl.PersonServiceImpl.*(..))"/> 23 <aop:pointcut id="argcut" 24 expression="execution(* com.persia.service.impl.PersonServiceImpl.*(..)) and args(name)"/> 25 <aop:before pointcut-ref="mycut" method="doAccessCheck"/> 26 <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/> 27 <aop:after-throwing pointcut-ref="mycut" method="doThrowing"/> 28 <aop:after pointcut-ref="argcut" method="doAfter" arg-names="name"/> 29 <aop:around pointcut-ref="mycut" method="arround"/> 30 </aop:aspect> 31 32 </aop:config> 33 34 </beans>
3.开启事务和注解:
注解方式:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:tx="http://www.springframework.org/schema/tx" 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/context 11 http://www.springframework.org/schema/context/spring-context-2.5.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 14 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 15 "> 16 17 <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 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 <!-- 配置事务管理器--> 36 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 37 <property name="dataSource" ref="dataSource"/> 38 </bean> 39 <!-- 配置业务bean --> 40 <bean id="personService" class="com.persia.service.impl.PersonServiceImpl"> 41 <property name="ds" ref="dataSource"></property> 42 </bean> 43 44 <!-- 采用@Transactional注解方式来使用事务 --> 45 <tx:annotation-driven transaction-manager="txManager"/> 46 47 48 </beans>
XML方式:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:tx="http://www.springframework.org/schema/tx" 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/context 11 http://www.springframework.org/schema/context/spring-context-2.5.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 14 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 15 "> 16 17 <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 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 <!-- 配置事务管理器 --> 36 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 37 <property name="dataSource" ref="dataSource"/> 38 </bean> 39 <!-- 配置业务bean --> 40 <bean id="personService" class="com.persia.service.impl.PersonServiceImpl"> 41 <property name="ds" ref="dataSource"></property> 42 </bean> 43 44 45 <!-- 使用XML来使用事务管理--> 46 <aop:config> 47 <!-- 配置一个切面,和需要拦截的类和方法 --> 48 <aop:pointcut id="transactionPointcut" expression="execution(* com.persia.service.*.*(..))"/> 49 <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/> 50 </aop:config> 51 <!-- 配置一个事务通知 --> 52 <tx:advice id="txAdvice" transaction-manager="txManager"> 53 <tx:attributes> 54 <!-- 方法以get开头的,不使用事务 --> 55 <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/> 56 <!-- 其他方法以默认事务进行 --> 57 <tx:method name="*"/> 58 </tx:attributes> 59 </tx:advice> 60 61 62 </beans>
4.SSH:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:tx="http://www.springframework.org/schema/tx" 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/context 11 http://www.springframework.org/schema/context/spring-context-2.5.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 14 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 15 "> 16 17 18 <!-- 配置数据源 --> 19 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 20 <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 21 <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/> 22 <property name="username" value="root"/> 23 <property name="password" value=""/> 24 <!-- 连接池启动时的初始值 --> 25 <property name="initialSize" value="1"/> 26 <!-- 连接池的最大值 --> 27 <property name="maxActive" value="500"/> 28 <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> 29 <property name="maxIdle" value="2"/> 30 <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> 31 <property name="minIdle" value="1"/> 32 </bean> 33 34 <!-- 配置hibernate的sessionFactory --> 35 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 36 <property name="dataSource"> 37 <ref bean="dataSource"/> 38 </property> 39 <property name="mappingResources"> 40 <list> 41 <value>com/persia/model/Person.hbm.xml</value> 42 </list> 43 </property> 44 45 <!-- 1.首先在sessionFactory里面配置以上3条设置 --> 46 <!-- 2.然后得在类路径下面添加一个ehcache.xml的缓存配置文件 --> 47 <!-- 3.最后在要使用缓存的实体bean的映射文件里面配置缓存设置 --> 48 <!--使用二级缓存--> 49 <!-- 不使用查询缓存,因为命中率不是很高 --> 50 <!-- 使用Ehcache缓存产品 --> 51 <property name="hibernateProperties"> 52 <value> 53 hibernate.dialect=org.hibernate.dialect.MySQL5Dialect 54 hibernate.hbm2ddl.auto=update 55 hibernate.show_sql=false 56 hibernate.format_sql=false 57 hibernate.cache.use_second_level_cache=true 58 hibernate.cache.use_query_cache=false 59 hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider 60 </value> 61 </property> 62 </bean> 63 64 <!-- 配置Spring针对hibernate的事务管理器 --> 65 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 66 <property name="sessionFactory" ref="sessionFactory"/> 67 </bean> 68 69 <!-- 配置使用注解的方式来使用事务 --> 70 <tx:annotation-driven transaction-manager="txManager"/> 71 72 <!-- 使用手工配置的注解方式来注入bean --> 73 <context:annotation-config></context:annotation-config> 74 75 <!--定义要注入的业务bean --> 76 <bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean> 77 78 <!--将Struts的action交给Spring容器来管理 --> 79 <bean name="/person/list" class="com.persia.struts.PersonListAction"> 80 <!--1.这里要求name和struts-config里面的action的path名称一致,因为id不允许有特殊字符--> 81 <!--2.还得在Struts-config文件里面添加Spring的请求处理器,该处理器会根据action的path属性到Spring容器里面寻找这个bean,若找到了则用这个bean来处理用户的请求--> 82 <!--3.然后去掉action的type标签和值(可选),当Spring处理器找不到该bean时,才会使用Struts的action--> 83 <!--4.最后在action里面使用Spring的注入方式来注入业务bean--> 84 </bean> 85 86 <bean name="/person/manage" class="com.persia.struts.PersonManageAction"></bean> 87 </beans>
5.SSH2:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:tx="http://www.springframework.org/schema/tx" 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/context 11 http://www.springframework.org/schema/context/spring-context-2.5.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 14 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 15 "> 16 17 18 <!-- 配置数据源 --> 19 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 20 <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 21 <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/> 22 <property name="username" value="root"/> 23 <property name="password" value=""/> 24 <!-- 连接池启动时的初始值 --> 25 <property name="initialSize" value="1"/> 26 <!-- 连接池的最大值 --> 27 <property name="maxActive" value="500"/> 28 <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> 29 <property name="maxIdle" value="2"/> 30 <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> 31 <property name="minIdle" value="1"/> 32 </bean> 33 34 <!-- 配置hibernate的sessionFactory --> 35 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 36 <property name="dataSource"> 37 <ref bean="dataSource"/> 38 </property> 39 <property name="mappingResources"> 40 <list> 41 <value>com/persia/model/Person.hbm.xml</value> 42 </list> 43 </property> 44 45 <!-- 1.首先在sessionFactory里面配置以上3条设置 --> 46 <!-- 2.然后得在类路径下面添加一个ehcache.xml的缓存配置文件 --> 47 <!-- 3.最后在要使用缓存的实体bean的映射文件里面配置缓存设置 --> 48 <!--使用二级缓存--> 49 <!-- 不使用查询缓存,因为命中率不是很高 --> 50 <!-- 使用Ehcache缓存产品 --> 51 <property name="hibernateProperties"> 52 <value> 53 hibernate.dialect=org.hibernate.dialect.MySQL5Dialect 54 hibernate.hbm2ddl.auto=update 55 hibernate.show_sql=false 56 hibernate.format_sql=false 57 hibernate.cache.use_second_level_cache=true 58 hibernate.cache.use_query_cache=false 59 hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider 60 </value> 61 </property> 62 </bean> 63 64 <!-- 配置Spring针对hibernate的事务管理器 --> 65 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 66 <property name="sessionFactory" ref="sessionFactory"/> 67 </bean> 68 69 <!-- 配置使用注解的方式来使用事务 --> 70 <tx:annotation-driven transaction-manager="txManager"/> 71 72 <!-- 使用手工配置的注解方式来注入bean --> 73 <context:annotation-config></context:annotation-config> 74 75 <!--定义要注入的业务bean --> 76 <bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean> 77 78 <!--注入Struts 2的action --> 79 <bean id="personList" class="com.persia.struts2.action.PersonListAction"></bean> 80 </beans>
6.SSJ:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:tx="http://www.springframework.org/schema/tx" 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/context 11 http://www.springframework.org/schema/context/spring-context-2.5.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 14 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 15 "> 16 17 18 <!-- 使用手工配置的注解方式来注入bean --> 19 <context:annotation-config></context:annotation-config> 20 21 <!-- 1.配置Spring集成JPA --> 22 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> 23 <property name="persistenceUnitName" value="SpringJPAPU"/> 24 </bean> 25 26 <!--2.配置Spring针对JPA的事务 --> 27 <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 28 <property name="entityManagerFactory" ref="entityManagerFactory"/> 29 </bean> 30 31 <!--3.开启事务注解 --> 32 <tx:annotation-driven transaction-manager="txManager"/> 33 34 <!--以上3个Spring集成JPA的配置,在web项目先添加Spring支持,后添加JPA支持时会自动生成 --> 35 36 <!-- 配置业务bean --> 37 <bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean> 38 39 <!-- 配置Struts的action --> 40 <bean name="/person/list" class="com.persia.struts.PersonListAction"/> 41 <bean name="/person/manage" class="com.persia.struts.PersonManageAction"/> 42 </beans>
转载于https://blog.csdn.net/yajunren/article/details/10118841