The basic content related to IoC comes to an end. This time I introduce the second feature of Spring, AOP, aspect-oriented programming. The terminology sounds relatively difficult to understand, it doesn’t matter, everything is in an example, let us look at a simple example, understand.
First look at the overall structure
There are several categories in it,
First, pom.xml needs to reference jar inside,
Second: define an interface Hellointerface to implement the interface: UserServiceImpl
Third: add a root-context.xml
Fourth: Add Aop class TimeMonitor
Fifth: springaopapplication test class
The first step is to create an interface. This is a normal class and has nothing to do with AOP thinking.
Create interface HelloInterface
package springaop.aopservice; public interface HelloInterface { void sayHello(); }
Step 2: Create the implementation class UserServiceImpl
package springaop.aopservice.impl; import org.springframework.stereotype.Service; import springaop.aopservice.HelloInterface; @Service public class UserServiceImpl implements HelloInterface { public void sayHello() { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("userHello"); } }
Step 3: Create root-context.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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <context:component-scan base-package="springaop"></context:component-scan> </beans>
Step 4: Add in pom.xml
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency>
Step 5: Add aop class
Here you need to understand the 5 annotations of aop
@after notification method will be called after the target method returns or throws an exception
@afterreturning notification method will be called after returning
@AfterThrowing notification method will be called after throwing an exception
The @Around notification method encapsulates the target method, that is, executes both before and after the method. Use Object re = pjp.proceed(); to distinguish before and after
@Before notification method will be executed before the target method is called
package springaop.aopservice.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Service; @Service @Aspect public class TimeMonitor { @Pointcut("execution(* springaop.aopservice.impl.UserServiceImpl.sayHello(..))") public void performance(){} @Around("performance()") public void monitorAround(ProceedingJoinPoint pjp) throws Throwable { System.out.println("method start time:" + System.currentTimeMillis()); Object re = pjp.proceed(); System.out.println("method end time:" + System.currentTimeMillis()); } @Before("performance()") public void tekeSeats(){ System.out.println("我来测试一下,看看能不能先执行method end time:" + System.currentTimeMillis()); } @AfterReturning("performance()") public void afterSeats(){ System.out.println("我来测试一下,看看能不能后执行method end time:" + System.currentTimeMillis()); } }
The sixth step:
test
package springaop.springaop; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.support.ClassPathXmlApplicationContext; import springaop.aopservice.HelloInterface; @SpringBootTest class SpringaopApplicationTests { ///通过XMl的方式实现 aop @Test void contextLoads() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("root-context.xml"); HelloInterface userService = context.getBean(HelloInterface.class); userService.sayHello(); context.close(); } }