spring@value loading timing

@value is an annotation used to inject a member variable of a class in spring. It injects the configured value from the configuration file. It needs to be injected into the class PropertyPlaceholderConfigurer under the configuration in spring. There are many ways to enter and exit, such as:

 

<bean id="propertyConfigurer" class="PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath*:config/*.properties</value>
			</list>
		</property>
		<property name="ignoreUnresolvablePlaceholders" value="true" />
		<property name="fileEncoding" value="UTF-8"/>
	</bean>
 @Value injection is injected after spring injects the default constructor of the class.

 


Use the public static <T> T instantiateClass(Constructor<T> ctor, Object... args) of org.springframework.beans.BeanUtils to instantiate the @Component class. Use this bean factory DefaultListableBeanFactory.

Use org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) to create an instance of this bean. The mbd contains some data like this:

scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null

By default spring managed beans are singletons.
After the instance is constructed, the @Value annotation will be triggered at this time, org.springframework.beans.factory.annotation. The inject(Object bean, String beanName, PropertyValues ​​pvs) method of AutowiredAnnotationBeanPostProcessor will inject the properties of the bean. After the injection properties are completed (line 557 in the figure below), spring loads other beans. @Value actually uses the mechanism of @AutoWire.

 Since spring only loads the constructor of this class during initialization, if you need to modify the value in the configuration file, you can get the bean instance through the spring context and use the set method to modify the value. When @Value loads Chinese, garbled characters appear. You can configure the encoding format in PropertyPlaceholderConfigurer. The above code has been adjusted. You can also change Chinese to unicode encoding.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326523802&siteId=291194637