This is the 17th day of my participation in the More text Challenge. For more details, see more text Challenge


Related articles

Spring series: Spring series


precondition

(1) into the jar

<dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
    </dependencies>
Copy the code

② Configuration files enable aop header reference constraints


      
<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"
       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.xsd">

</beans>
Copy the code

③ Abstract interface

public interface UserService {
    void add(a);
    void delete(a);
    void update(a);
    void selete(a);
}
Copy the code

(4) Implementation class:

public class UserServiceImpl implements UserService {
    @Override
    public void add(a) {
        System.out.println("Add users");
    }

    @Override
    public void delete(a) {
        System.out.println("Delete user");
    }

    @Override
    public void update(a) {
        System.out.println("Update user");
    }

    @Override
    public void selete(a) {
        System.out.println("Query User"); }}Copy the code
First, through the Spring API implementation

① Add the pre – and post-setting methods

public class AopBeforeConfigLog implements MethodBeforeAdvice {

    /** * method: method of the target object to execute * args: parameters of the method to be called * target: target object */
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass()+"= = = ="+method.getName()+"It was executed."); }}Copy the code
public class AopAfterConfigLog implements AfterReturningAdvice {

    /** * method: method of the target object to execute * args: parameters of the method to be called * result: return value * target: target object to be called */

    @Override
    public void afterReturning(Object result, Method method, Object[] args, Object target){
        System.out.println(target.getClass()+"= = = ="+method.getName()+"It was executed."+"=== Return value"+result); }}Copy the code

(2) beans. XML

	<! Registered bean -- -- -- >
    <bean id="userService" class="service.UserServiceImpl"/>
    <bean id="afterLog" class="aoptest.AopAfterConfigLog"/>
    <bean id="beforeAop" class="aoptest.AopBeforeConfigLog"/>

    <aop:config>
        <! -- Pointcut expression: expression matches the method to be executed -->
        <aop:pointcut id="cut" expression="execution(* service.UserServiceImpl.*(..) )"/>

        <! -- Perform surround; Pointcut -ref -->
        <aop:advisor advice-ref="afterLog" pointcut-ref="cut"/>
        <aop:advisor advice-ref="beforeAop" pointcut-ref="cut"/>
    </aop:config>
Copy the code

③ Test class:

public class TestAop {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        UserService userService = (UserService) Context.getBean("userService"); userService.add(); userService.selete(); userService.update(); userService.delete(); }}Copy the code

④ Execution result:

(5) conclusion:

  • Cross-section: a point of cross-cutting
  • Notice: The work done by the section is actually a method
  • Target: The object to be notified
  • expression="execution(* service.UserServiceImpl.*(..) )"
    • This is a set of fixed formulas
    • *On behalf of all
    • All methods under service.UserServiceImpl are covered!
Two, custom class to achieve

(1) Customize the entered class

/* * Custom class * */
public class MyDIYAopCut {
    public void before(a){
        System.out.println("Method execution fore fore fore fore fore fore fore fore fore fore fore fore fore fore fore fore fore fore fore fore fore);
    }
    public void after(a){
        System.out.println("After after after after after after after after after after after after after after"); }}Copy the code

(2) beans. XML

    <! Registered bean -- -- -- >
    <bean id="userService" class="service.UserServiceImpl"/>
    <bean id="Mydiycut" class="diyaop.MyDIYAopCut"/>

    <aop:config>
        <! Where ref specifies which class to enter -->
        <aop:aspect ref="Mydiycut">
            <! -- Entry point -->
            <aop:pointcut id="cut" expression="execution(* service.UserServiceImpl.*(..) )"/>

            <! The method name is the same as the method name in our custom class -->
            <aop:before method="before" pointcut-ref="cut"/>
            <! The method name is the same as the method name in our custom class -->
            <aop:before method="after" pointcut-ref="cut"/>
        </aop:aspect>
    </aop:config>
Copy the code

③ The test class is unchanged, and the execution result is:

(4) conclusion:

  • The method of the test class is the name of the METHOD in XML

  • See the comments in XML for more!
Use annotations

① Custom enhanced annotation implementation class

@Aspect
public class MyAnnotationAopCut {
    @Before("execution(* service.UserServiceImpl.*(..) )"
    public void before(a){
        System.out.println("Before method execution");
    }
    @After("execution(* service.UserServiceImpl.*(..) )"
    public void after(a){
        System.out.println("After the method is executed");
    }
    @Around("execution(* service.UserServiceImpl.*(..) )"
    public void around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("Before surround =="+point.getSignature());
        / / execution
        Object proceed = point.proceed();
        System.out.println("After surround =="+point.getSignature()); }}Copy the code

(2) the XML

	<! Registered bean -- -- -- >
    <bean id="userService" class="service.UserServiceImpl"/>
    <bean id="myAnnotationAopCut" class="diyaop.MyAnnotationAopCut"/>
    <! -- Declare to automatically create proxies for beans in the Spring container that configure the @AspectJ aspect, weaving the aspect -->
    <aop:aspectj-autoproxy proxy-target-class="false"/>
Copy the code

(3) The test classes are consistent, and the execution result is consistent

(4) conclusion:

  • proxy-target-class="false"
    • False implements dynamic proxies in JDK
    • True implements dynamic proxies in CGLib
    • We usually use the default mechanism for this thing, understand, know such a thing!
  • @Aspect: Identifies the current class as an aspect for the container to read
  • @Before: Before the method is executed
  • @After: after the method is executed
  • @Around: Execute in surround mode
    • ProceedingJoinPoint: Wrap notification = pre + target method execution + Post notification. The proceed method is used to start the target method execution
    • Proceed (), which is the midpoint, must be used so that the program knows which is before and which is after!

I see no ending, but I will search high and low

If you think I blogger writes good! Writing is not easy, please like, follow, comment to encourage the blogger ~hahah