Look at spring 5 annotation AOP

Change AOP in XML mode to annotation mode.
Step 1: annotation configuration business class
Annotate the ProductService class with @ Component("s")

package com.how2java.service;
 
import org.springframework.stereotype.Component;
 
@Component("s")
public class ProductService {
    public void doSomeService(){
        System.out.println("doSomeService");
    }
     
}

Step 2: annotation configuration facet
@The Aspect annotation indicates that this is a facet
@Component indicates that this is a bean managed by Spring
@Around(value = "execution(* com.how2java.service.ProductService. *) (... ))”)Represents the faceting operation of all methods in the class com.how2java.service.ProductService

package com.how2java.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
 
@Aspect
@Component
public class LoggerAspect {
     
    @Around(value = "execution(* com.how2java.service.ProductService.*(..))")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log:" + joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();
        System.out.println("end log:" + joinPoint.getSignature().getName());
        return object;
    }
}

Step 3: applicationcontext.xml
Remove the original information and add the following 3 lines

<context:component-scan base-package="com.how2java.aspect"/>
<context:component-scan base-package="com.how2java.service"/>

Scan package com.how2java.aspect and com.how2java.service to locate business class and aspect class

<aop:aspectj-autoproxy/>

Find the annotated aspect class and configure the aspect

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  
    <context:component-scan base-package="com.how2java.aspect"/>
    <context:component-scan base-package="com.how2java.service"/>
    <aop:aspectj-autoproxy/> 
   
</beans>

Step 4: run the test
package com.how2java.test;

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

import com.how2java.service.ProductService;

public class TestSpring {

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "applicationContext.xml" });
    ProductService s = (ProductService) context.getBean("s");
    s.doSomeService();
}

}

Keywords: Spring xml encoding

Added by saint959 on Thu, 31 Oct 2019 22:36:10 +0200