Explain three ways to obtain bean by SpringBoot static method in detail

  • 2021-11-30 00:01:13
  • OfStack

Directory Mode 1 Annotation @ PostConstruct Mode 2 Startup Class ApplicationContext Mode 3 Manual injection of ApplicationContext

Mode 1 annotation @ PostConstruct


import com.example.javautilsproject.service.AutoMethodDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
 
/**
 * springboot Static method acquisition  bean  Adj. 3 A kind of way (1)
 * @author: clx
 * @date: 2019/7/23
 * @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();
    }
}

Note @ PostConstruct Description

PostConstruct annotations are used on methods that need to be executed after dependency injection is complete to perform any initialization. This method must be called before the class is put into the service. All classes that support dependency injection must support this annotation. Methods annotated with PostConstruct must be called even if the class does not request any resource injection. Only 1 method can be annotated with this annotation.

The method of applying PostConstruct annotations must comply with all the following standards:

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

Mode 2 Startup Class ApplicationContext

Implementation: In the startup class of springboot, define the static variable ApplicationContext, and use the getBean method of the container to obtain the dependent object


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

Calling mode


/**
 * @author: clx
 * @date: 2019/7/23
 * @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

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 Static method acquisition  bean  Adj. 3 A kind of way (3)
 * @author: clx
 * @date: 2019/7/23
 * @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;
    }
}

Calling mode


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

The above three ways have been tested by the landlord and can be used perfectly


Related articles: