spring_aop_proxyInterfaces

来源:互联网 发布:mac pro2018款 编辑:程序博客网 时间:2024/06/05 20:38

demo

description:接口方式的aop编程,这里是使用实现接口的类,对实现类进行aop编程

首先准备spring aop所需要的jar包:


Aspect.java

/** *切面 */public class Aspect {        private Logger logger = Logger.getLogger(Aspect.class);        /**     * 前置通知     */    public void doBefore(JoinPoint jp){        logger.debug("before "+jp.getTarget().getClass().getName()+"."+jp.getSignature().getName()+" execute");    }        /**     * 后置通知     */    public void doAfter(JoinPoint jp){        logger.debug("finally after "+jp.getTarget().getClass().getName()+"."+jp.getSignature().getName()+" execute");    }        /**     * 环绕通知     */    public void doAround(ProceedingJoinPoint pjp) throws Throwable{        logger.debug("around before "+pjp.getTarget().getClass().getName()+"."+pjp.getSignature().getName()+" execute");        pjp.proceed();        logger.debug("around after "+pjp.getTarget().getClass().getName()+"."+pjp.getSignature().getName()+" execute");    }        /**     * 后置返回通知     */    public void doAfterRetuning(JoinPoint jp){        logger.debug("after return "+jp.getTarget().getClass().getName()+"."+jp.getSignature().getName()+" execute");    }        /**     * 抛出异常后通知     */    public void doThrowing(JoinPoint jp, Throwable e){        logger.debug("after throwing "+jp.getTarget().getClass().getName()+"."+jp.getSignature().getName()+" execute");        logger.debug(e.getMessage());    }    }

Dao.java

public interface Dao<T> {        void save(T t);        void select(T t);        void delete(T t);}

DaoImpl.java

public class DaoImpl implements Dao<Mp3>{    @Override    public void save(Mp3 t) {        if(t.getId().equals(1)){            throw new RuntimeException("save() 运行时异常");        }        System.out.println("execute save()");    }    @Override    public void select(Mp3 t) {        System.out.println("execute select()");    }    @Override    public void delete(Mp3 t) {        System.out.println("execute delete()");    }}

Mp3.java

public class Mp3 implements Serializable{    private static final long serialVersionUID = 1L;    private Integer id;        private String url;    public Mp3() {    }    public Mp3(Integer id, String url) {        this.id = id;        this.url = url;    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getUrl() {        return url;    }    public void setUrl(String url) {        this.url = url;    }        }

applicationContext-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:p="http://www.springframework.org/schema/p"    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-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/aop               http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"    default-autowire="byName">        <!-- 切面 -->    <bean id="aspect" class="com.asarja.aop.Aspect"/>        <!-- aop接口代理 -->    <bean name="daoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">    <property name="proxyInterfaces" value="com.asarja.aop.Dao"/>    <property name="target">    <bean class="com.asarja.aop.DaoImpl" />    </property>    </bean>        <aop:config>    <aop:aspect ref="aspect">    <aop:pointcut expression="execution(* com.asarja.aop.*.*(..))" id="daoservice"/>    <aop:before method="doBefore" pointcut-ref="daoservice"/>    <aop:around method="doAround" pointcut-ref="daoservice"/>    <aop:after-returning method="doAfterRetuning" pointcut-ref="daoservice"/>    <aop:after-throwing method="doThrowing" pointcut-ref="daoservice" throwing="e"/>    <aop:after method="doAfter" pointcut-ref="daoservice"/>    </aop:aspect>    </aop:config></beans>

Test.java

public class Test {    public static void main(String[] args) {        ApplicationContext context = new ClassPathXmlApplicationContext("com/asarja/aop/applicationContext-aop.xml");        Dao<Mp3> dao = (Dao<Mp3>)context.getBean("daoProxy");        dao.delete(new Mp3(2,"http://,,,,,"));        dao.save(new Mp3(1,"http://,,,,,"));        dao.select(new Mp3(1,"http://,,,,,"));

结果:

2013-04-09 16:01:52,566 [main] DEBUG com.asarja.aop.Aspect - before $Proxy0.delete execute
2013-04-09 16:01:52,566 [main] DEBUG com.asarja.aop.Aspect - around before $Proxy0.delete execute
2013-04-09 16:01:52,567 [main] DEBUG com.asarja.aop.Aspect - before com.asarja.aop.DaoImpl.delete execute
2013-04-09 16:01:52,567 [main] DEBUG com.asarja.aop.Aspect - around before com.asarja.aop.DaoImpl.delete execute
execute delete()
2013-04-09 16:01:52,567 [main] DEBUG com.asarja.aop.Aspect - around after com.asarja.aop.DaoImpl.delete execute
2013-04-09 16:01:52,567 [main] DEBUG com.asarja.aop.Aspect - after return com.asarja.aop.DaoImpl.delete execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - finally after com.asarja.aop.DaoImpl.delete execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - around after $Proxy0.delete execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - after return $Proxy0.delete execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - finally after $Proxy0.delete execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - before $Proxy0.save execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - around before $Proxy0.save execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - before com.asarja.aop.DaoImpl.save execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - around before com.asarja.aop.DaoImpl.save execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - after throwing com.asarja.aop.DaoImpl.save execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - save() 运行时异常
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - finally after com.asarja.aop.DaoImpl.save execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - after throwing $Proxy0.save execute
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - save() 运行时异常
2013-04-09 16:01:52,568 [main] DEBUG com.asarja.aop.Aspect - finally after $Proxy0.save execute

从输出结果可以看出,在抛出异常之后,环绕后方法不执行