DBCP的数据库连接归还--利用 编程式AOP实现(不推荐)

来源:互联网 发布:电磁仿真软件 编辑:程序博客网 时间:2024/04/30 10:22
DBCP的数据库连接归还--利用 编程式AOP实现(不推荐)


说明:本过程适合练习aop。对 dao的封装 和 action对service的调用都有一定的破坏。


理解
编程式AOP 后点拦截


步骤一:dao层,添加拦截查询需要的方法。
(1)目前先使用JdbcTemplate来关闭连接。
(原理:JdbcTemplate获取数据源中SimpleJdbcTemplate创建的连接,还会给连接池。)
(2)在dao层Impl类中,添加JdbcTemplate属性,开getter、setter放法。
例如
  1. <!-- 配置dao层 start -->
  2. <bean id="userDaoImpl" class="com.usertest.dao.UserDao">
  3. <property name="jdbcTemplate">
  4. <ref bean="jdbcTemplateImpl" />
  5. </property>
  6. <property name="simpleJdbcTemplate">
  7. <ref bean="simpleJdbcTemplateImpl" />
  8. </property>
  9. </bean>
  10. <!-- 配置dao层 end -->
(3)在dao层Impl类中,写关闭方法。
例如 
  1. public void closeConn() {
  2. try {
  3. this.getJdbcTemplate().getDataSource().getConnection().close();
  4. System.out.println("数据库连接已经归还");
  5. } catch (Exception e) {
  6. // TODO Auto-generated catch block
  7. e.printStackTrace();
  8. }
  9. }


步骤二:service层不改动。


步骤三:创建aop文件。
(1)创建aop包(大型系统应该每一层一个aop包,这里就先一个)。
(2)写一个advice类,作为aop织入内容。
例如
  1. public class DbCloseAdvice implements AfterReturningAdvice {


  2. private String methods;


  3. public String getMethods() {
  4. return methods;
  5. }


  6. public void setMethods(String methods) {
  7. this.methods = methods;
  8. }


  9. @Override
  10. public void afterReturning(Object returnValue, Method method,
  11. Object[] args, Object target) throws Throwable {
  12. // TODO Auto-generated method stub


  13. if (methods.indexOf(method.getName()) >= 0) {
  14. UserService userService = (UserService) target;// 直接使用service实现类
  15. userService.getUserdao().closeConn();
  16. }


  17. }


  18. }


步骤四:在applicationContext.xml配置aop相关。
(1)AOP切面  织入内容 。
例如
  1. <!-- 为web层配置动态代理获取service  start -->
  2. <!-- AOP切面  织入内容  start -->
  3. <bean id = "dbCloseAdvice" class = "com.usertest.aop.DbCloseAdvice">
  4. <!-- 当前类的哪些方法  start -->
  5. <property name="methods">
  6. <value>chkLogin,getAllUsers,addUser,deleteUser,updateUser,getUserById</value>
  7. </property>
  8. <!-- 当前类的哪些方法  end -->
  9. </bean>
  10. <!-- AOP切面  织入内容  end -->
(2)为web层配置动态代理获取service。
例如
  1. <!-- spring 的 动态代理模式  start -->
  2. <bean id ="serviceBean" class = "org.springframework.aop.framework.ProxyFactoryBean">
  3. <!-- 动态代理 接口  start-->
  4. <property name="proxyInterfaces">
  5. <value>com.usertest.service.inter.UserServiceInter</value>
  6. </property>
  7. <!-- 动态代理 接口  end-->
  8. <!-- 动态代理 实现  start-->
  9. <property name="target">
  10. <ref bean = "userServiceImpl"/>
  11. </property>
  12. <!-- 动态代理 实现  end-->
  13. <!-- AOP切面  织入对象  start -->
  14. <property name="interceptorNames">
  15. <list>
  16. <value>dbCloseAdvice</value>
  17. </list>
  18. </property>
  19. <!-- AOP切面  织入对象  end -->
  20. </bean>
  21. <!-- spring 的 动态代理模式  end -->


步骤五:控制层(action)中配置相关。

(1)action不在动态获取service,所以applicationContext.xml不在为action配置service属性。
(2)action类中,删除service属性。
(3)为action类的每一个方法单独动态获取service代理来创建service(避免类里方法间有游离代码)。
(注:web服务器启动时,已经创建了ApplicationContext,所以不可再创建,只能获取。)
例如
  1. // 获得系统启动时的ApplicationContext,不可在重新加载
  2. ApplicationContext applicationContext = WebApplicationContextUtils
  3. .getWebApplicationContext(ServletActionContext
  4. .getServletContext());


  5. UserServiceInter userService = (UserServiceInter) applicationContext
  6. .getBean("serviceBean");




如有好的建议,可Email-->fzb_xxzy@163.com



0 0