SpringAOP Oriented Programming (Aspect Oriented Programming)
aop effect:
Project demo:
Code:
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AppConfig {
@Pointcut("execution(* com.spring.demo.service.*.*(..))")
public void businessService() {}
@Before(value = "businessService()")
public void doBefore() {
System.out.println("----------------- doBefore -----------------");
}
@AfterReturning(value = "businessService()")
public void doAfterReturning() {
System.out.println("----------------- doAfterReturning -----------------");
}
@After(value = "businessService()")
public void doAfter() {
System.out.println("----------------- doAfter -----------------");
}
}
import com.spring.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @description:
* @author: yz
* @create: 2019/5/27 15:33
*/
@RestController
public class TestController {
@Autowired
UserService userService;
@RequestMapping("/test")
public String test(){
return userService.query();
}
}
public interface UserService {
String query();
}
@Service("userService")
public class UserServiceImpl implements UserService {
@Override
public String query() {
System.out.println("------------ yz ------------");
return "------------ yz ------------";
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Request url: HTTP: // localhost: 8080 / the Test
Source code debugging, write the main method, using the annotation mode:
import com.spring.demo.service.UserService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
/**
* @description:
* @author: yz
* @create: 2019/5/27 15:23
*/
@ComponentScan("com.spring.demo")
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Main.class);
UserService userService = applicationContext.getBean(UserService.class);
// UserService userService = (UserService) applicationContext.getBean("userService");
userService.query();
}
}
Breakpoints found to get the service object has been through a proxy Aop CGLIB object, main method for printing information after aop proxy service:
Acting time is likely to occur in the first step, may also occur in the second step, this time assuming that occurred in the second step, breakpoint hit getBean track:
SingletonObjects found a map
Since there singletonObjects get, then there put, this time to find an entry point this.singletonObjects.put
2 search method using troubleshooting jvm runtime stack, in particular a method which is userService aop agent.
NOTE : At this point has not been performed to UserService userService = applicationContext.getBean (UserService.class);
That spring aop proxy proxy object created in the first step is performed:
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Main.class);
We will hit the breakpoint doGetBean of getSingleton method, plus conditional breakpoints, this is a Lambda expressions
Rerun, enter getSingleton method
Thus Spring Aop source code debugging.
Spring Aop official website information:
https://docs.spring.io/spring/docs/5.2.0.BUILD-SNAPSHOT/spring-framework-reference/core.html#aop
其他资料:https://blog.csdn.net/u010890358/article/details/80640433