<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName" default-lazy-init="true">
<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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName" default-lazy-init="true">
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>
jdbc:mysql://localhost/ssh?characterEncoding=utf-8
</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>
jdbc:mysql://localhost/ssh?characterEncoding=utf-8
</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123</value>
</property>
</bean>
<!--配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/ssh/pojo/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- hibernateTemplate -->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/ssh/pojo/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- hibernateTemplate -->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 配置数据持久层 -->
<bean id="userDao"
class="com.ssh.dao.impl.UserDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<bean id="userDao"
class="com.ssh.dao.impl.UserDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<!-- 配置业务逻辑层 -->
<bean id="userService"
class="com.ssh.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userService"
class="com.ssh.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- 配置控制层 -->
<bean id="UserAction"
class="com.ssh.action.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<!-- 配置pojo -->
<bean id="User" class="com.ssh.pojo.User" scope="prototype"/>
</beans>
<bean id="UserAction"
class="com.ssh.action.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<!-- 配置pojo -->
<bean id="User" class="com.ssh.pojo.User" scope="prototype"/>
</beans>
//////////////////////////////////////////////// 下面是详解: ////////////////////////////////////////////////////////////////////////
1.基本配置:
<?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"
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
">
<?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"
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
">
<context:component-scan base-package="com.persia">
<!-- 开启组件扫描 -->
</context:component-scan>
<context:annotation-config>
<!--开启注解处理器-->
</context:annotation-config>
<!--开启注解处理器-->
</context:annotation-config>
<!-- 使用注解,省去了propertity的xml配置,减少xml文件大小 -->
<bean id="personServiceAnno" class="com.persia.PersonServiceAnnotation"></bean>
<bean id="personDaoBeanAnno" class="com.persia.PersonDaoBean"></bean>
<bean id="personDaoBeanAnno2" class="com.persia.PersonDaoBean"></bean>
<bean id="personServiceAnno" class="com.persia.PersonServiceAnnotation"></bean>
<bean id="personDaoBeanAnno" class="com.persia.PersonDaoBean"></bean>
<bean id="personDaoBeanAnno2" class="com.persia.PersonDaoBean"></bean>
<!-- 自动注解 -->
<bean id="personServiceAutoInject" class="com.persia.PersonServiceAutoInject" autowire="byName"></bean>
<bean id="personServiceAutoInject" class="com.persia.PersonServiceAutoInject" autowire="byName"></bean>
<bean id="personService" class="com.persia.PersonServiceBean">
<!-- 由spring容器去创建和维护,我们只要获取就可以了 -->
</bean>
<bean id="personService2" class="com.persia.PersonServiceBeanFactory" factory-method="createInstance" lazy-init="true"
init-method="init" destroy-method="destory">
<!-- 静态工厂获取bean -->
</bean>
init-method="init" destroy-method="destory">
<!-- 静态工厂获取bean -->
</bean>
<bean id="fac" class="com.persia.PersonServiceBeanInsFactory"></bean>
<bean id="personService3" factory-bean="fac" factory-method="createInstance" scope="prototype">
<!-- 实例工厂获取bean,先实例化工厂再实例化bean-->
</bean>
<bean id="personService3" factory-bean="fac" factory-method="createInstance" scope="prototype">
<!-- 实例工厂获取bean,先实例化工厂再实例化bean-->
</bean>
<!-- ref方式注入属性 -->
<bean id="personDao" class="com.persia.PersonDaoBean"></bean>
<bean id="personService4" class="com.persia.PersonServiceBean">
<property name="personDao" ref="personDao"></property>
</bean>
<!-- 内部bean方式注入 -->
<bean id="personService5" class="com.persia.PersonServiceBean">
<property name="personDao">
<bean class="com.persia.PersonDaoBean"></bean>
</property>
<property name="name" value="persia"></property>
<property name="age" value="21"></property>
<property name="sets">
<!-- 集合的注入 -->
<set>
<value>第一个</value>
<value>第二个</value>
<value>第三个</value>
</set>
</property>
<property name="lists">
<!-- 集合的注入 -->
<list>
<value>第一个l</value>
<value>第二个l</value>
<value>第三个l</value>
</list>
</property>
<property name="properties">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
<prop key="key3">value3</prop>
</props>
</property>
<property name="map">
<map>
<entry key="key1" value="value-1"></entry>
<entry key="key2" value="value-2"></entry>
<entry key="key3" value="value-3"></entry>
</map>
</property>
</bean>
<bean id="personService5" class="com.persia.PersonServiceBean">
<property name="personDao">
<bean class="com.persia.PersonDaoBean"></bean>
</property>
<property name="name" value="persia"></property>
<property name="age" value="21"></property>
<property name="sets">
<!-- 集合的注入 -->
<set>
<value>第一个</value>
<value>第二个</value>
<value>第三个</value>
</set>
</property>
<property name="lists">
<!-- 集合的注入 -->
<list>
<value>第一个l</value>
<value>第二个l</value>
<value>第三个l</value>
</list>
</property>
<property name="properties">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
<prop key="key3">value3</prop>
</props>
</property>
<property name="map">
<map>
<entry key="key1" value="value-1"></entry>
<entry key="key2" value="value-2"></entry>
<entry key="key3" value="value-3"></entry>
</map>
</property>
</bean>
<bean id="personService6" class="com.persia.PersonServiceBean">
<constructor-arg index="0" value="构造注入的name" ></constructor-arg>
<!-- 基本类型可以不写type -->
<constructor-arg index="1" type="com.persia.IDaoBean" ref="personDao">
</constructor-arg>
</bean>
<constructor-arg index="0" value="构造注入的name" ></constructor-arg>
<!-- 基本类型可以不写type -->
<constructor-arg index="1" type="com.persia.IDaoBean" ref="personDao">
</constructor-arg>
</bean>
</beans>2.开启AOP:
<?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/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<?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/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="myInterceptor" class="com.persia.service.MyInterceptor"></bean>
<bean id="personServiceImpl" class="com.persia.service.impl.PersonServiceImpl"></bean>
</beans>AOP的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/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<bean id="myInterceptor" class="com.persia.service.MyInterceptor"></bean>
<bean id="personServiceImpl" class="com.persia.service.impl.PersonServiceImpl"></bean>
</beans>AOP的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/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean>
<bean id="aspectBean" class="com.persia.service.MyInterceptor"></bean>
<bean id="aspectBean" class="com.persia.service.MyInterceptor"></bean>
<aop:config>
<aop:aspect id="myaop" ref="aspectBean">
<aop:pointcut id="mycut" expression="execution(* com.persia.service.impl.PersonServiceImpl.*(..))"/>
<aop:pointcut id="argcut" expression="execution(* com.persia.service.impl.PersonServiceImpl.*(..)) and args(name)"/>
<aop:before pointcut-ref="mycut" method="doAccessCheck" />
<aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
<aop:after-throwing pointcut-ref="mycut" method="doThrowing"/>
<aop:after pointcut-ref="argcut" method="doAfter" arg-names="name"/>
<aop:around pointcut-ref="mycut" method="arround"/>
</aop:aspect>
</aop:config>
<aop:aspect id="myaop" ref="aspectBean">
<aop:pointcut id="mycut" expression="execution(* com.persia.service.impl.PersonServiceImpl.*(..))"/>
<aop:pointcut id="argcut" expression="execution(* com.persia.service.impl.PersonServiceImpl.*(..)) and args(name)"/>
<aop:before pointcut-ref="mycut" method="doAccessCheck" />
<aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
<aop:after-throwing pointcut-ref="mycut" method="doThrowing"/>
<aop:after pointcut-ref="argcut" method="doAfter" arg-names="name"/>
<aop:around pointcut-ref="mycut" method="arround"/>
</aop:aspect>
</aop:config>
</beans>3.开启事务和注解:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
</bean>
<!-- 配置事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置业务bean -->
<bean id="personService" class="com.persia.service.impl.PersonServiceImpl">
<property name="ds" ref="dataSource"></property>
</bean>
<!-- 采用@Transactional注解方式来使用事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
</bean>
<!-- 配置事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置业务bean -->
<bean id="personService" class="com.persia.service.impl.PersonServiceImpl">
<property name="ds" ref="dataSource"></property>
</bean>
<!-- 采用@Transactional注解方式来使用事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
<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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置业务bean -->
<bean id="personService" class="com.persia.service.impl.PersonServiceImpl">
<property name="ds" ref="dataSource"></property>
</bean>
<!-- 使用XML来使用事务管理-->
<aop:config>
<!-- 配置一个切面,和需要拦截的类和方法 -->
<aop:pointcut id="transactionPointcut" expression="execution(* com.persia.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
</aop:config>
<!-- 配置一个事务通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 方法以get开头的,不使用事务 -->
<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>
<!-- 其他方法以默认事务进行 -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
</beans>4.SSH:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置业务bean -->
<bean id="personService" class="com.persia.service.impl.PersonServiceImpl">
<property name="ds" ref="dataSource"></property>
</bean>
<!-- 使用XML来使用事务管理-->
<aop:config>
<!-- 配置一个切面,和需要拦截的类和方法 -->
<aop:pointcut id="transactionPointcut" expression="execution(* com.persia.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
</aop:config>
<!-- 配置一个事务通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 方法以get开头的,不使用事务 -->
<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>
<!-- 其他方法以默认事务进行 -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
</beans>4.SSH:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
</bean>
<!-- 配置hibernate的sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref bean="dataSource" /></property>
<property name="mappingResources">
<list>
<value>com/persia/model/Person.hbm.xml</value>
</list>
</property>
<!-- 1.首先在sessionFactory里面配置以上3条设置 -->
<!-- 2.然后得在类路径下面添加一个ehcache.xml的缓存配置文件 -->
<!-- 3.最后在要使用缓存的实体bean的映射文件里面配置缓存设置 -->
<!--使用二级缓存-->
<!-- 不使用查询缓存,因为命中率不是很高 -->
<!-- 使用Ehcache缓存产品 -->
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=false
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
</value>
</property>
</bean>
<!-- 配置Spring针对hibernate的事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置使用注解的方式来使用事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
<tx:annotation-driven transaction-manager="txManager"/>
<!-- 使用手工配置的注解方式来注入bean -->
<context:annotation-config></context:annotation-config>
<context:annotation-config></context:annotation-config>
<!--定义要注入的业务bean -->
<bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean>
<bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean>
<!--将Struts的action交给Spring容器来管理 -->
<bean name="/person/list" class="com.persia.struts.PersonListAction">
<!--1.这里要求name和struts-config里面的action的path名称一致,因为id不允许有特殊字符-->
<!--2.还得在Struts-config文件里面添加Spring的请求处理器,该处理器会根据action的path属性到Spring容器里面寻找这个bean,若找到了则用这个bean来处理用户的请求-->
<!--3.然后去掉action的type标签和值(可选),当Spring处理器找不到该bean时,才会使用Struts的action-->
<!--4.最后在action里面使用Spring的注入方式来注入业务bean-->
</bean>
<bean name="/person/list" class="com.persia.struts.PersonListAction">
<!--1.这里要求name和struts-config里面的action的path名称一致,因为id不允许有特殊字符-->
<!--2.还得在Struts-config文件里面添加Spring的请求处理器,该处理器会根据action的path属性到Spring容器里面寻找这个bean,若找到了则用这个bean来处理用户的请求-->
<!--3.然后去掉action的type标签和值(可选),当Spring处理器找不到该bean时,才会使用Struts的action-->
<!--4.最后在action里面使用Spring的注入方式来注入业务bean-->
</bean>
<bean name="/person/manage" class="com.persia.struts.PersonManageAction"></bean>
</beans>5.SSH2:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
</beans>5.SSH2:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
</bean>
<!-- 配置hibernate的sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref bean="dataSource" /></property>
<property name="mappingResources">
<list>
<value>com/persia/model/Person.hbm.xml</value>
</list>
</property>
<!-- 1.首先在sessionFactory里面配置以上3条设置 -->
<!-- 2.然后得在类路径下面添加一个ehcache.xml的缓存配置文件 -->
<!-- 3.最后在要使用缓存的实体bean的映射文件里面配置缓存设置 -->
<!--使用二级缓存-->
<!-- 不使用查询缓存,因为命中率不是很高 -->
<!-- 使用Ehcache缓存产品 -->
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=false
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
</value>
</property>
</bean>
<!-- 配置Spring针对hibernate的事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置使用注解的方式来使用事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
<tx:annotation-driven transaction-manager="txManager"/>
<!-- 使用手工配置的注解方式来注入bean -->
<context:annotation-config></context:annotation-config>
<context:annotation-config></context:annotation-config>
<!--定义要注入的业务bean -->
<bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean>
<bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean>
<!--注入Struts 2的action -->
<bean id="personList" class="com.persia.struts2.action.PersonListAction"></bean>
</beans>6.SSJ:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
<bean id="personList" class="com.persia.struts2.action.PersonListAction"></bean>
</beans>6.SSJ:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
<!-- 使用手工配置的注解方式来注入bean -->
<context:annotation-config></context:annotation-config>
<!-- 1.配置Spring集成JPA -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="SpringJPAPU"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="SpringJPAPU"/>
</bean>
<!--2.配置Spring针对JPA的事务 -->
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!--3.开启事务注解 -->
<tx:annotation-driven transaction-manager="txManager"/>
<!--以上3个Spring集成JPA的配置,在web项目先添加Spring支持,后添加JPA支持时会自动生成 -->
<tx:annotation-driven transaction-manager="txManager"/>
<!--以上3个Spring集成JPA的配置,在web项目先添加Spring支持,后添加JPA支持时会自动生成 -->
<!-- 配置业务bean -->
<bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean>
<bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean>
<!-- 配置Struts的action -->
<bean name="/person/list" class="com.persia.struts.PersonListAction"/>
<bean name="/person/manage" class="com.persia.struts.PersonManageAction"/>
</beans>
<bean name="/person/list" class="com.persia.struts.PersonListAction"/>
<bean name="/person/manage" class="com.persia.struts.PersonManageAction"/>
</beans>
相关推荐
在Spring框架中,`applicationContext.xml`是应用上下文的核心配置文件,用于定义bean的创建、依赖关系以及各种服务的配置。这篇博文“Spring 2.5 - applicationContext.xml提示信息的配置”主要探讨了如何在Spring ...
ApplicationContext.xml 是 Spring 框架中用于配置应用程序的核心配置文件。通过该文件,可以定义 Bean、数据源、Session 工厂、 Hibernate 配置等相关信息,从而实现应用程序的自动装配和依赖注入。 一、XML 声明...
首先,`applicationContext.xml`是Spring框架的核心配置文件,它定义了Bean的定义、依赖注入关系以及服务的配置。在这个特定的场景中,`applicationContext.xml`将包含与Quartz相关的配置,如Scheduler、Job、...
在IT行业中,尤其是在Java Web开发领域,`applicationContext.xml`、`db.properties`、`log4j.properties`以及`spring-mvc.xml`等文件是非常关键的配置文件,它们各自负责不同的功能,对于一个完整的应用程序来说不...
在Spring框架中,`applicationContext.xml`是配置文件的主入口,用于定义bean的创建、依赖关系以及各种服务的提供。当我们谈论`applicationContext-dao.xml`时,我们主要关注的是数据访问对象(DAO)层的配置。 `...
"Spring配置文件spring-context.zip"包含了Spring框架中的上下文配置,这是Spring管理对象及服务的核心。 `applicationContext.xml`是Spring应用上下文的主配置文件,它定义了bean的声明、bean之间的依赖关系以及...
总结而言,`spring-context-4.2.xsd`是Spring 4.2版本Context模块的核心配置规范,它定义了Spring XML配置文件的结构,涵盖了bean定义、依赖注入、作用域、AOP、事件处理等多个方面的内容。理解并熟练运用`spring-...
用于配置acegi的xml文件,把这个文件加入项目中,然后建立里面的数据库,引入jar包就可以实现权限的管理
在实际开发中,`spring-mvc-4.2.xsd`通常被引用在Spring配置文件(如`applicationContext.xml`或`servlet-context.xml`)的顶部,通过`xsi:schemaLocation`属性来指定: ```xml <beans xmlns="http://www.spring...
在Java Web开发中,`struts.xml`, `applicationContext.xml` 和 `web.xml` 是三个至关重要的配置文件,它们各自负责不同的职责,并协同工作来构建一个完整的应用框架。以下是关于这三个配置文件的详细说明。 首先,...
在使用Struts2 Spring Plugin时,开发者需要在Struts配置文件(struts.xml)中添加必要的配置,声明插件并指定Spring应用上下文的位置。这样,当Struts2框架处理请求时,它会查找并使用Spring容器中的Action实例。 ...
ApplicationContext.xml是Spring框架中的核心配置文件,它是Spring的IOC(Inverse of Control,控制反转)容器的核心组件。该文件用于定义和配置Spring应用程序中的各种Bean,对于Spring应用程序的开发和维护起着至...
本文主要针对SSH框架中Spring部分的配置文件`applicationContext.xml`的头部文件进行深入解析。 #### 二、`applicationContext.xml`文件解析 ##### 1. 文件头部结构 在给出的部分内容中,可以看到`...
这个错误提示表明XML解析器无法找到`<beans>`元素的定义,这是一个基本的Spring配置文件结构元素,用于包裹所有的bean定义。 Spring的配置文件遵循特定的命名空间和Schema定义,这些定义在`spring-beans.xsd`文件中...
`spring-5.2.3.RELEASE-schema.zip`包含了Spring的XML配置文件的XSD(XML Schema Definition)定义。这些定义文件规定了我们在Spring XML配置文件中可以使用的元素和属性,帮助我们编写符合规范的配置。通过查看这些...
2. **ApplicationContext.xml**:这是Spring应用上下文的配置文件,它定义了bean的创建、依赖关系以及其他的配置信息。在`applicationContext.xml`中,我们可以声明bean的实例化、属性注入、bean之间的依赖关系、AOP...
3. **applicationContext.xml**:这是Spring的上下文配置文件,主要管理服务层(Service)和数据访问层(DAO)的Bean。包括Bean的定义、依赖注入(DI)、事务管理、AOP(面向切面编程)等配置。 - Bean定义:使用`...
《ApplicationContext.xml——Spring框架的核心配置文件详解》 在Java开发领域,Spring框架是不可或缺的一部分,它以其强大的依赖注入(Dependency Injection,简称DI)和面向切面编程(Aspect-Oriented ...
在`src/main/resources`下,我们会找到Spring的配置文件,如`applicationContext.xml`,它定义了Spring容器的bean,包括数据源、SqlSessionFactory、Mapper接口的扫描路径等。这里,Spring会管理MyBatis的...