一、思想:AOP的本質是在一系列縱向的控制流程中,把那些相同的子流程提取成一個橫向的面。html
二、實例:(用AspectJ框架)java
1)pom.xmlspring
<?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.xmlapache
<?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接口app
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)切面類:AspectIntercepterssh
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)測試類:SpringAopTestmaven
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); } }
三、源碼下載:src測試
四、參考:spa
https://www.cnblogs.com/Wolfmanlq/p/6036019.html
https://blog.csdn.net/lp1052843207/article/details/52460601
https://www.cnblogs.com/whgk/p/6627187.html