AOP learning record and @ Configuration annotation use

Posted by robos99 on Mon, 07 Mar 2022 21:37:33 +0100

AOP is face to face, why face to face? What operations require AOP programming?

The main purpose of Aop is to extract the aspects in the business processing process. It faces a step or stage in the processing process, so as to obtain the isolation effect of low coupling between various parts in the logical process.

For example, the most common is logging. For example, we now provide a service to query student information, but we want to record who made this query. According to the traditional OOP implementation, we have implemented a service interface (StudentInfoService) and its implementation class ( for querying student information. At the same time, in order to record, we need to add its implementation record in the implementation class (
When there are multiple services to be implemented, it is necessary to add these recording processes in each implementation class, which is very cumbersome. Moreover, each implementation class is tightly coupled with the behavior of recording service logs, which violates the object-oriented rules.

General oop mode:

Each module is intertwined with log and transaction functions and cannot be separated. When modifying the form of log, a lot of code is required.

aop mode:


The input data first passes through the log, transaction and other control modules, and then flows to the actual business module. The log and transaction control modules seem to be cut in the middle of the data flow


  1. decoupling
  2. Reuse code
  3. Easy loading and unloading

Configuration usage example

1. The @ Bean annotation is used in the configuration class to register components for the container on the method. By default, it is also single instance

2. The configuration class itself is also a component

3. Proxybean methods: proxy bean methods

 Full(proxyBeanMethods = true),[Guarantee each@Bean How many times is the method called, and the returned component is single instance]
 Lite(proxyBeanMethods = false)[each@Bean How many times is the method called and the returned component is newly created]

Component dependencies must use the Full mode default. Are other default Lite modes available

@Configuration(proxyBeanMethods = false) //Tell SpringBoot that this is a configuration class = = configuration file
public class MyConfig {


 * Full:No matter how many external calls are made to this component registration method in the configuration class, the single instance object in the previous registration container is obtained
 * @return
   @Bean //Add components to the container. Take the method name as the id of the component. The return type is the component type. The returned value is the instance of the component in the container
   public User user01(){
   User zhangsan = new User("zhangsan", 18);
   //The user component depends on the Pet component
   return zhangsan;

public Pet tomcatPet(){
    return new Pet("tomcat");

@Configuration test codes are as follows

 amount to
public class MainApplication {
public static void main(String[] args) {
    //1. Return to our IOC container
    ConfigurableApplicationContext run =, args);

    //2. View the components in the container
    String[] names = run.getBeanDefinitionNames();
    for (String name : names) {

    //3. Get component from container

    Pet tom01 = run.getBean("tom", Pet.class);

    Pet tom02 = run.getBean("tom", Pet.class);

    System.out.println("Components:"+(tom01 == tom02));
    MyConfig bean = run.getBean(MyConfig.class);

    //If @ Configuration(proxyBeanMethods = true) the proxy object calls the method. SpringBoot always checks whether the component is in the container.
    //Keep component single instance
    User user = bean.user01();
    User user1 = bean.user01();
    System.out.println(user == user1);
    User user01 = run.getBean("user01", User.class);
    Pet tom = run.getBean("tom", Pet.class);

    System.out.println("User's pet:"+(user01.getPet() == tom));

Topics: Java Spring Spring Boot