Preface
There are many ways to inject instances in Spring, but due to the different initialization order, the injection method based on the annotation is prone to failure to be correctly injected successfully.
This article will introduce a dynamically based way to extract Spring-managed beans in real projects. I won’t say much below, let’s take a look at the detailed introduction together.
1. Inject instances based on labeling
When the bean is initialized, the object it depends on must be initialized. If the injected object is initialized later than the current object, the injected object will be null.
1.1 @Autowired
Load Spring-managed beans by type. By default, its bean must exist. If its bean is null, its required property can be set to false. For specific details, please refer to the source code:
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic@interface Autowired {/** * Declares whether the annotated dependency is required. *Defaults to {@code true}.*/boolean required() default true;}If you need to inject a bean based on a command, you need to use @Qualifier to label the name. The code example is as follows:
@Autwired@Qualifier("beanName")private BeanType beanObj;Application scope: above variables, setter methods and constructors.
Source: Spring Framework
1.2 @Inject
Provided by javax.inject.Inject, it is automatically assembled based on type. If you need to transfer according to the name, you need to use @Named in conjunction with it. This usage is very similar to @Autowired provided by the Spring framework.
Application scope: variables, setter methods, constructors
Source: JSR330 specification javax extension package
Code example:
@Inject@Named("beanName")private BeanType bean;1.3 @Resource
The default is assembly injection by name. Only when the bean matching the name cannot be found will the injection by type. It is provided after JDK 1.6.
Application scope: Can be applied to variables and setter methods
Source: Provided after JDK 1.6
Code usage example:
@Resource(name="mybeanName")private BeanType bean;
2. Dynamic injection method
Idea: Use ApplicationContextAware to obtain the reference of ApplicationContext, and then dynamically obtain the object based on ApplicationContext.
The implementation code is as follows:
@Componentpublic class SpringContextUtil implements ApplicationContextAware {// Spring application context environment private static ApplicationContext applicationContext;/** Implement the callback method of the ApplicationContextAware interface and set the context environment** @param applicationContext*/public void setApplicationContext(ApplicationContext applicationContext) {SpringContextUtil.applicationContext = applicationContext;}/*** @return ApplicationContext*/public static ApplicationContext getApplicationContext() {return applicationContext;}/*** Get object** @param name* @return Object* @throws BeansException*/public static Object getBean(String name) throws BeansException {return applicationContext.getBean(name);}}After that, you can dynamically obtain the required bean instances in the code:
BeanType bean = SpringContextUtil.getBean("beanName")Isn't it very easy to use?
Summarize
Here we summarize the various ways of injecting beans into Spring, each with its advantages and disadvantages, and you can choose to use them.
Okay, the above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.