Three ways Spring Boot gets bean s! Who else won't??

Keywords: Java

Source: blog.csdn.net/showchi/article/details/97005720

Note: Callers are managed by spring

Mode 1

Comment@PostConstruct

import com.example.javautilsproject.service.AutoMethodDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * springboot Three ways to get bean s by static methods (1)
 * @author: clx
 * @version: 1.1.0
 */
@Component
public class StaticMethodGetBean_1 {

    @Autowired
    private AutoMethodDemoService autoMethodDemoService;

    @Autowired
    private static AutoMethodDemoService staticAutoMethodDemoService;

    @PostConstruct
    public void init() {
        staticAutoMethodDemoService = autoMethodDemoService;
    }

    public static String getAuthorizer() {
        return staticAutoMethodDemoService.test();
    }
}

The PostConstruct comment is used on methods that need to be executed after dependency injection is complete to perform any initialization. This method must be called before putting the class into the service.

All classes that support dependency injection must support this comment. The method annotated with PostConstruct must be called even if the class does not request injection of any resources. There is only one way to comment with this comment.

The method of applying the PostConstruct comment must comply with all of the following criteria:

  • This method must not have any parameters unless it is defined by the EJB specification in the case of an EJB interceptor, in which case it will have an InvocationContext object;
  • The return type of this method must be void;
  • The method must not throw checked exceptions;
  • PostConstruct can be applied in public, protected, package private, or private ways.
  • This method cannot be static except for the application client;
  • The method can be final;
  • If this method throws an unchecked exception, the class must not be placed in the service unless it is an EJB that can handle the exception and recover from it.

Mode 2

Start Class ApplicationContext

Implementation: In the springboot startup class, define the static variable ApplicationContext, and use the container's getBean method to get dependent objects

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
/**
 * @author: clx
 * @version: 1.1.0
 */
@SpringBootApplication
public class Application {
    public static ConfigurableApplicationContext ac;
    public static void main(String[] args) {
       ac = SpringApplication.run(Application.class, args);
    }

}

Call Method

/**
 * @author: clx
 * @version: 1.1.0
 */
@RestController
public class TestController {
    /**
     * Mode 2
     */
    @GetMapping("test2")
    public void method_2() {
        AutoMethodDemoService methodDemoService = Application.ac.getBean(AutoMethodDemoService.class);
        String test2 = methodDemoService.test2();
        System.out.println(test2);
    }
}

Mode 3

Manual Injection of ApplicationContext

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;


/**
 * springboot Three ways to obtain bean s by static methods (3)
 * @author: clx
 * @version: 1.1.0
 */
@Component
public class StaticMethodGetBean_3<T> implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        StaticMethodGetBean_3.applicationContext = applicationContext;
    }

    public static <T> T  getBean(Class<T> clazz) {
        return applicationContext != null?applicationContext.getBean(clazz):null;
    }
}

Call Method

/**
 * Mode 3
 */
@Test
public void method_3() {
    AutoMethodDemoService autoMethodDemoService = StaticMethodGetBean_3.getBean(AutoMethodDemoService.class);
    String test3 = autoMethodDemoService.test3();
    System.out.println(test3);
}

All three ways have been tested to be perfect for use.

Recent Hot Text Recommendations:

1.1,000+ Java Interview Questions and Answers (latest version 2021)

2.Stop filling the screen with if/ else and try the strategy mode. It's delicious!!

3.What the fuck! What is the new syntax for xx_null in Java?

4.Spring Boot 2.5 LB release, dark mode too deep!

5.The latest release of the Java Development Manual (Songshan Edition). Download it quickly!

Feel good, don't forget to compliment + forward!

Posted by Steven_belfast on Thu, 21 Oct 2021 11:09:38 -0700