daixinet

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1、思想:AOP的本质是在一系列纵向的控制流程中,把那些相同的子流程提取成一个横向的面。

 

 

2、实例:(用AspectJ框架)

 1)pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.daixinet</groupId>
    <artifactId>test</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.7.3</version>
        </dependency>
        
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.7.3</version>
        </dependency>

    </dependencies>
</project>

2)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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <bean id="personServiceBean" class="com.daixinet.PersonServerImpl"/>
    <bean id="myInterceptor" class="com.daixinet.AspectIntercepter"/>

</beans>

3)PersonServer接口

package com.daixinet;


public interface PersonServer {
    public void save(String name, int age);
}

4)实现类:PersonServerImpl

package com.daixinet;

public class PersonServerImpl implements PersonServer {

    public void save(String name, int age) {
        int a = 0;

//        age = age /a;
        System.out.println("come in PersonServerImpl save method...");
    }
}

5)切面类:AspectIntercepter

package com.daixinet;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class AspectIntercepter {

    @Pointcut(value = "execution(* com.daixinet.PersonServerImpl.*(..))")
    private void pointCut(){

    }

    @Around(value = "pointCut()")
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("@Around 进入环绕通知...");
        Object object = pjp.proceed();
        System.out.println(pjp.getThis() + "操作结束,退出方法;环绕【@Around】结束!。。。");
        return object;
    }

    @Before(value = "pointCut()")
    public void doAccessCheck(JoinPoint joinPoint){
        System.out.println("@Before 前置通知:" + Arrays.toString(joinPoint.getArgs()));
    }

    @AfterThrowing(value = "pointCut()",throwing = "ex")
    public void doAfterThrow(JoinPoint joinPoint, Throwable ex){
        System.out.println("@AfterThrowing例外通知(异常通知)" + Arrays.toString(joinPoint.getArgs()));
        System.out.println("@AfterThrowing异常信息:" + ex);
    }

    @After(value = "pointCut()")
    public void after(){
        System.out.println("@After后置通知。。。");
    }

    @AfterReturning(value = "pointCut()")
    public void doAfter(){
        System.out.println("@AfterReturing最终通知。。。End");
    }

}

6)测试类:SpringAopTest

 

package com.daixinet;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringAopTest {

    @Test
    public void interceptorTest() {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-aop.xml");

        PersonServer bean = (PersonServer) ctx.getBean("personServiceBean");
        bean.save("bad Mode", 23);

//        PersonServer p = new PersonServerImpl();
//        p.save("111", 22);
    }
}

 

3、源码下载:src

4、参考:

  https://www.cnblogs.com/Wolfmanlq/p/6036019.html

  https://blog.csdn.net/lp1052843207/article/details/52460601

   https://www.cnblogs.com/whgk/p/6627187.html

 

posted on 2018-07-14 10:52  daixinet.com  阅读(183)  评论(0编辑  收藏  举报