例:连接JDBC和管理事务
1,beans的声明处添加相应的命名空间的schema定义文件的说明,这样在配置文件中就可以使用对应空间下的配置标签了。
<beans xmlns="
http://www.springframework.org/schema/beans"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xmlns:p=" http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xmlns:p=" http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
2,将
注解的类自动转化Bean,同时完成Bean的注入 -->
<context:component-sc
项目包名添加到上下文扫描路径,以便包中类里的Spring注解生效。
<!-- 扫描类包,把标注Springan base-package="com.baobaotao.dao" />
<context:component-scan base-package="com.baobaotao.service" />
3-1,定义一个使用DBCP实现的数据源
<!-- 定义一个使用DBCP实现的数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/sampledb"
p:username="root"
p:password="root" />
3-2,定义JDBC模版bean
<!-- 定义jdbc模版Bean -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
p:dataSource-ref="dataSource" />
3-3,配置事务管理器,负责声明式事务的管理,需引用dataSource Bean
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />
4,通过aop和tx命名空间的语法,以AOP的方式为项目包下所有类的所有方法都添加事务增强,即它们都将工作在事务环境中。
<!-- 通过AOP配置提供事务增强,让service包下所有bean的所有方法拥有事务 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="serviceMethod"
expression="execution(*com.baobaotao.service..*(..))" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
例:连接JDBC和管理事务
1,beans的声明处添加相应的命名空间的schema定义文件的说明,这样在配置文件中就可以使用对应空间下的配置标签了。
<beans xmlns="
http://www.springframework.org/schema/beans"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xmlns:p=" http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xmlns:p=" http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
2,将
注解的类自动转化Bean,同时完成Bean的注入 -->
<context:component-sc
项目包名添加到上下文扫描路径,以便包中类里的Spring注解生效。
<!-- 扫描类包,把标注Springan base-package="com.baobaotao.dao" />
<context:component-scan base-package="com.baobaotao.service" />
3-1,定义一个使用DBCP实现的数据源
<!-- 定义一个使用DBCP实现的数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/sampledb"
p:username="root"
p:password="root" />
3-2,定义JDBC模版bean
<!-- 定义jdbc模版Bean -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
p:dataSource-ref="dataSource" />
3-3,配置事务管理器,负责声明式事务的管理,需引用dataSource Bean
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />
4,通过aop和tx命名空间的语法,以AOP的方式为项目包下所有类的所有方法都添加事务增强,即它们都将工作在事务环境中。
<!-- 通过AOP配置提供事务增强,让service包下所有bean的所有方法拥有事务 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="serviceMethod"
expression="execution(*com.baobaotao.service..*(..))" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">