多种方式实现AOP

Posted 1314justin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多种方式实现AOP相关的知识,希望对你有一定的参考价值。

一、使用代理工厂完成声明式增强

1.创建业务接口

public interface IdoSomeService {
    public void doSomething();
}

 

2.创建接口实现类

public class IdoSomeServiceImpl implements  IdoSomeService{
    @Override
    public void doSomething() {
        System.out.println("真实业务");
    }
}

 

3.创建切面类

/**
 * 切面
 */
public class MyBeforeAdvice implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("前置增强");
    }
}

 

4.编写applicationContext.xml配置文件

<!--注入业务Bean-->
    <bean id="idoSomeService" class="cn.spring.proxyfactory.IdoSomeServiceImpl"></bean>
    <!--增强:切面-->
    <bean id="myBeforeAdvice" class="cn.spring.proxyfactory.MyBeforeAdvice"></bean>
    <!--使用代理工厂实现增强-->
    <bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--将增强和业务织入 到一起 -->
        <property name="target" ref="idoSomeService"></property>
        <!--拦截增强类-->
        <property name="interceptorNames" value="myBeforeAdvice"></property>
     </bean>

 

 

5.创建测试类

    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取代理工厂
        IdoSomeService  idoSomeService=(IdoSomeService)context.getBean("proxyFactory");
        idoSomeService.doSomething();
    }
}

 

二、使用代理工厂完成环绕增强

1.创建业务接口

public interface IdoSomeService {
    public void doSomething();
}

 

2.创建业务接口实现类

public class IdoSomeServiceImpl implements  IdoSomeService{
    @Override
    public void doSomething() {
        System.out.println("真实业务");
    }
}

 

 

3.创建切面类

public class MyAroundAdvice implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("环绕前");

        //调用核心业务方法也可以获取方法内的参数 也可以获取目标对象
        Object proceed = methodInvocation.proceed();
        Object aThis = methodInvocation.getThis();
        System.out.println(aThis);

        System.out.println("环绕后");
        return proceed;
    }
}

 

4.编写applicationContext.xml配置文件

    环绕增强
    <bean id="idoSomeService"  class="cn.spring.around.IdoSomeServiceImpl"></bean>
    <bean id="myAroundAdvice"  class="cn.spring.around.MyAroundAdvice"></bean>
    <bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--将增强和业务织入到一起-->
        <property name="target" ref="idoSomeService"></property>
        <property name="interceptorNames" value="myAroundAdvice"></property>
        <!--更换代理方式    proxyTargetClass默认值为false   默认是jdk动态代理,但是当目标对象没有接口时,自动改为cglib-->
        <property name="proxyTargetClass" value="true"></property>
     </bean>

 

 

4.创建测试类

public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取代理工厂
        IdoSomeService idoSomeService=(IdoSomeService)context.getBean("proxyFactory");
        idoSomeService.doSomething();
    }

 

三、使用工厂代理工厂完成异常增强

1.创建业务接口

public interface IdoSomeService {
    public void doSomething() throws Exception;
}

 

2.创建业务接口实现类

public class IdoSomeServiceImpl implements IdoSomeService {
    @Override
    public void doSomething() throws Exception{
        int result=5/0;
        System.out.println("真实业务");
    }
}

 

3.创建切面类

public class MyThrowAdvice{
   public void afterThrowing(Exception ex){
       System.out.println("=====发生了异常,执行增强操作===============");
   }
}

 

4.编写applicationContext.xml配置文件

<!--环绕增强-->
    <bean id="idoSomeService"  class="cn.spring.around.IdoSomeServiceImpl"></bean>
    <bean id="myAroundAdvice"  class="cn.spring.around.MyAroundAdvice"></bean>
    <bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--将增强和业务织入到一起-->
        <property name="target" ref="idoSomeService"></property>
        <property name="interceptorNames" value="myAroundAdvice"></property>
        <!--更换代理方式    proxyTargetClass默认值为false   默认是jdk动态代理,但是当目标对象没有接口时,自动改为cglib-->
        <property name="proxyTargetClass" value="true"></property>
     </bean>

 

 

5.创建测试类

 public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取代理工厂
        IdoSomeService idoSomeService=(IdoSomeService)context.getBean("idoSomeService");
        try{
            idoSomeService.doSomething();

        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("2222222222222");
    }

 

 

四、使用代理工厂实现最终增强

1.创建业务接口

public interface IdoSomeService {
    public void doSomething() throws Exception;
}

 

2.创建业务接口实现类

public class IdoSomeServiceImpl implements IdoSomeService {
    @Override
    public void doSomething() throws Exception{
        int result=5/0;
        System.out.println("真实业务");
    }
}

 

 

3.创建切面类

public class MyThrowAdvice{
    public void afterAdvice(){
        System.out.println("======执行最终异常===============");
    }
}


4.编写applicationContext.xml配置文件

<bean id="idoSomeService" class="cn.spring.throwadvice.IdoSomeServiceImpl"></bean>
    <bean id="myAdvice" class="cn.spring.throwadvice.MyThrowAdvice"></bean>
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* *..throwadvice.*.*(..))"/>
        <aop:aspect ref="myAdvice">
            <aop:after-throwing method="afterThrowing" throwing="ex" pointcut-ref="pointcut"></aop:after-throwing>
            <aop:after method="afterAdvice" pointcut-ref="pointcut"></aop:after>
        </aop:aspect>
    </aop:config>

 

5.创建测试类

public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取代理工厂
        IdoSomeService idoSomeService=(IdoSomeService)context.getBean("idoSomeService");
        try{
            idoSomeService.doSomething();

        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("2222222222222");
    }

 

以上是关于多种方式实现AOP的主要内容,如果未能解决你的问题,请参考以下文章

Spring实现AOP的多种方式

IOC和AOP使用扩展 多种方式实现依赖注入

JAVA之AOP

Java实现AOP的几种方式

AOP的实现方式有哪几种?

AOP的实现方式有哪几种?