Spring AOP(2)- 后置增强 实现

来源:互联网 发布:淘宝ugg授权书怎么弄 编辑:程序博客网 时间:2024/06/11 10:00

实现原理:

前置增强完成功能:在目标类的方法执行之后嵌入增强逻辑.
实现原理:
1. 实现接口 AfterReturningAdvice,重写函数 afterReturning ,在函数afterReturning中实现需要插入目标方法之后的逻辑代码
2. 利用ProxyFactoryBean代理类调用目标类方法
3.配置Spring XML

定义接口类

package com.advice;import com.smart.domain.User;import java.sql.SQLException;/** * @author Duoduo * @version 1.0 * @date 2017/4/22 18:58 */public interface IUserDao {    int addUser(User user);    int updateUser(User user);    void deleteUser(User user);    void removeUser(User user) throws SQLException;}

接口实现类

package com.advice;import com.smart.domain.User;import java.sql.SQLException;/** * @author Duoduo * @version 1.0 * @date 2017/4/22 19:00 */public class UserDaoImpl implements IUserDao {    @Override    public int addUser(User user) {        System.out.println("Add user");        return 0;    }    @Override    public int updateUser(User user) {        System.out.println("Update user");        return 0;    }    @Override    public void deleteUser(User user) {        //throw new RuntimeException("exception test");    }    @Override    public void removeUser(User user) throws SQLException {        //throw new SQLException("SQLException Test");    }}

定义后置增强类

package com.advice;import com.smart.domain.User;import org.springframework.aop.AfterReturningAdvice;import java.lang.reflect.Method;/** * 后置增强:在目标类的方法执行之后嵌入增强逻辑 * @author Duoduo * @version 1.0 * @date 2017/4/22 19:04 */public class AddUserAfterAdvice implements AfterReturningAdvice {    @Override    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {        User user = (User)args[0];        System.out.println("After Add User for user name :"+ user.getUserName());    }}

Spring XML 配置

特别注意:代理工厂是 ProxyFactoryBean 不是 ProxyFactory

<!-- 代理设置 --><bean id="userDaoImpl" class="com.advice.UserDaoImpl"/><bean id="addUserBeforAdvice" class="com.advice.AddUserBeforAdvice"/><bean id="addUserAfterAdvice" class="com.advice.AddUserAfterAdvice"/><bean id="addUserProxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean"      p:proxyInterfaces="com.advice.IUserDao"      p:interceptorNames="addUserAfterAdvice"      p:target-ref="userDaoImpl"/>

如果需要配置多个增强,则配置如下:

 <!-- 代理设置 --><bean id="userDaoImpl" class="com.advice.UserDaoImpl"/><bean id="addUserBeforAdvice" class="com.advice.AddUserBeforAdvice"/><bean id="addUserAfterAdvice" class="com.advice.AddUserAfterAdvice"/><bean id="addUserProxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean"      p:proxyInterfaces="com.advice.IUserDao"      p:target-ref="userDaoImpl">    <property name="interceptorNames">        <list>            <value>addUserBeforAdvice</value>            <value>addUserAfterAdvice</value>        </list>    </property></bean>

两种调用方式:
1.根据XML配置来进行调用测试
2.自己创建代理来进行测试

另种方法都需要注意:此处代理获取的是代理Bean,指向的是Interface的代理。

@Testpublic  void addUserAfterAdivceTest(){    System.out.println("************ addUserAfterAdivceTest ************");    /*IUserDao userDao = new UserDaoImpl();    AfterReturningAdvice afterAdvice = new AddUserAfterAdvice();    //创建代理工厂    ProxyFactory proxyFactory = new ProxyFactory();    //设置代理目标类    proxyFactory.setTarget(userDao);    //为代理添加增强    proxyFactory.addAdvice(afterAdvice);    //生成代理实例    IUserDao proxyUserDao = (IUserDao)proxyFactory.getProxy();    proxyUserDao.addUser(user);*/    System.out.println("************ addUserAfterAdivceTest ************");    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-another-context.xml");    //代理为指向Interface的代理    IUserDao userDaoProxyFactory = (IUserDao) context.getBean("addUserProxyFactory");    userDaoProxyFactory.addUser(user);}

测试结果

************ addUserAfterAdivceTest ************四月 23, 2017 3:29:22 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@14d3bc22: startup date [Sun Apr 23 15:29:22 CST 2017]; root of context hierarchy四月 23, 2017 3:29:22 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [spring-another-context.xml]Add userAfter Add User for user name :Test User Name
0 0
原创粉丝点击