DBCP的数据库连接归还--利用 编程式AOP实现(不推荐)
说明:本过程适合练习aop。对 dao的封装 和 action对service的调用都有一定的破坏。
理解
编程式AOP 后点拦截
步骤一:dao层,添加拦截查询需要的方法。
(1)目前先使用JdbcTemplate来关闭连接。
(原理:JdbcTemplate获取数据源中SimpleJdbcTemplate创建的连接,还会给连接池。)
(2)在dao层Impl类中,添加JdbcTemplate属性,开getter、setter放法。
例如
例如
步骤二:service层不改动。
步骤三:创建aop文件。
(1)创建aop包(大型系统应该每一层一个aop包,这里就先一个)。
(2)写一个advice类,作为aop织入内容。
例如
步骤四:在applicationContext.xml配置aop相关。
(1)AOP切面 织入内容 。
例如
例如
步骤五:控制层(action)中配置相关。
(1)action不在动态获取service,所以applicationContext.xml不在为action配置service属性。
(2)action类中,删除service属性。
(3)为action类的每一个方法单独动态获取service代理来创建service(避免类里方法间有游离代码)。
(注:web服务器启动时,已经创建了ApplicationContext,所以不可再创建,只能获取。)
例如
如有好的建议,可Email--> fzb_xxzy@163.com
说明:本过程适合练习aop。对 dao的封装 和 action对service的调用都有一定的破坏。
理解
编程式AOP 后点拦截
步骤一:dao层,添加拦截查询需要的方法。
(1)目前先使用JdbcTemplate来关闭连接。
(原理:JdbcTemplate获取数据源中SimpleJdbcTemplate创建的连接,还会给连接池。)
(2)在dao层Impl类中,添加JdbcTemplate属性,开getter、setter放法。
例如
- <!-- 配置dao层 start -->
- <bean id="userDaoImpl" class="com.usertest.dao.UserDao">
- <property name="jdbcTemplate">
- <ref bean="jdbcTemplateImpl" />
- </property>
- <property name="simpleJdbcTemplate">
- <ref bean="simpleJdbcTemplateImpl" />
- </property>
- </bean>
- <!-- 配置dao层 end -->
例如
- public void closeConn() {
- try {
- this.getJdbcTemplate().getDataSource().getConnection().close();
- System.out.println("数据库连接已经归还");
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
步骤二:service层不改动。
步骤三:创建aop文件。
(1)创建aop包(大型系统应该每一层一个aop包,这里就先一个)。
(2)写一个advice类,作为aop织入内容。
例如
- public class DbCloseAdvice implements AfterReturningAdvice {
- private String methods;
- public String getMethods() {
- return methods;
- }
- public void setMethods(String methods) {
- this.methods = methods;
- }
- @Override
- public void afterReturning(Object returnValue, Method method,
- Object[] args, Object target) throws Throwable {
- // TODO Auto-generated method stub
- if (methods.indexOf(method.getName()) >= 0) {
- UserService userService = (UserService) target;// 直接使用service实现类
- userService.getUserdao().closeConn();
- }
- }
- }
步骤四:在applicationContext.xml配置aop相关。
(1)AOP切面 织入内容 。
例如
- <!-- 为web层配置动态代理获取service start -->
- <!-- AOP切面 织入内容 start -->
- <bean id = "dbCloseAdvice" class = "com.usertest.aop.DbCloseAdvice">
- <!-- 当前类的哪些方法 start -->
- <property name="methods">
- <value>chkLogin,getAllUsers,addUser,deleteUser,updateUser,getUserById</value>
- </property>
- <!-- 当前类的哪些方法 end -->
- </bean>
- <!-- AOP切面 织入内容 end -->
例如
- <!-- spring 的 动态代理模式 start -->
- <bean id ="serviceBean" class = "org.springframework.aop.framework.ProxyFactoryBean">
- <!-- 动态代理 接口 start-->
- <property name="proxyInterfaces">
- <value>com.usertest.service.inter.UserServiceInter</value>
- </property>
- <!-- 动态代理 接口 end-->
- <!-- 动态代理 实现 start-->
- <property name="target">
- <ref bean = "userServiceImpl"/>
- </property>
- <!-- 动态代理 实现 end-->
- <!-- AOP切面 织入对象 start -->
- <property name="interceptorNames">
- <list>
- <value>dbCloseAdvice</value>
- </list>
- </property>
- <!-- AOP切面 织入对象 end -->
- </bean>
- <!-- spring 的 动态代理模式 end -->
步骤五:控制层(action)中配置相关。
(1)action不在动态获取service,所以applicationContext.xml不在为action配置service属性。
(2)action类中,删除service属性。
(3)为action类的每一个方法单独动态获取service代理来创建service(避免类里方法间有游离代码)。
(注:web服务器启动时,已经创建了ApplicationContext,所以不可再创建,只能获取。)
例如
- // 获得系统启动时的ApplicationContext,不可在重新加载
- ApplicationContext applicationContext = WebApplicationContextUtils
- .getWebApplicationContext(ServletActionContext
- .getServletContext());
- UserServiceInter userService = (UserServiceInter) applicationContext
- .getBean("serviceBean");
如有好的建议,可Email--> fzb_xxzy@163.com