Spring属性赋值注解之@Value @PropertySource @PropertySources

时间:2025-02-13 15:58:12

目录

1. 说明

2. 注解使用

3. 注解解析

4. @PropertySources注解


1. 说明

当组件的属性通过配置文件的方式赋值的时候,xml配置的方法如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
	xmlns:xsi="http:///2001/XMLSchema-instance"
	xsi:schemaLocation="/schema/beans /schema/beans/">

	<!-- 导入配置文件 -->
	<context:property-placeholder location="classpath:" />

	<bean  class="">
		<property name="country" value="china"></property>
		<property name="province" value="zhej"></property>
		<property name="detail" value="${}"></property>
	</bean>

</beans>

如果使用注解的方式,就是通过@Value来赋值,通过@PropertySource导入属性的配置文件;

2. 注解使用

@Value的定义信息如下:

@Target({, , , ElementType.ANNOTATION_TYPE})
@Retention()
@Documented
public @interface Value {

	/**
	 * The actual value expression: . "#{}".
	 */
	String value();

}

 使用@Value赋值;
1、基本数值
2、可以写SpEL; #{}
3、可以写${};取出配置文件【properties】中的值(在运行环境变量里面的值)

比如:

package ;

import ;
import ;

import ;
import ;
import ;
import .slf4j.Slf4j;

@Setter
@Getter
@ToString
@Slf4j
@Component
public class Address {

	@Value("china") // 基本常量
	private String country;

	@Value("${}") // 从环境中取
	private String province;

	@Value("${}") // 从配置文件中取
	private String detail;

	@Value("#{100-2}") // SpEL表达式计算
	private int distance;

	public Address() {
		("构造器");
	}

}

当使用配置文件中的值为属性赋值的时候,通过@PropertySource导入配置文件

@PropertySource的定义信息如下:

package ;

import ;
import ;
import ;
import ;
import ;
import ;

import ;


@Target()
@Retention()
@Documented
@Repeatable()
public @interface PropertySource {

	String name() default "";

	 //指定配置文件  file:/, classpath:/*.properties,...
	String[] value();

	// 是否忽略没有找到的属性
	boolean ignoreResourceNotFound() default false;

	//指定配置文件的编号方式,. UTF-8
	String encoding() default "";

	Class<? extends PropertySourceFactory> factory() default ;

}

在主配置文件通过@ProperSource导入配置文件,如下:

/**
 * Project Name:yibai-spring-annotation File Name: Package
 * Name: Date:2019年1月5日上午11:20:26 Copyright (c) 2019,
 *  All Rights Reserved.
 *
 */

package ;

import ;
import ;

@ComponentScan("")
@PropertySource(value = "classpath:META-INF/*.properties")
public class MainConfigForValue {

}

3. 注解解析

@Value注解的解析器是,通过继承

类的postProcessPropertyValues(PropertyValues, PropertyDescriptor[], Object, String)方法来解析;

在AutowiredAnnotationBeanPostProcessor中postProcessPropertyValues方法的定义如下:

@Override
public PropertyValues postProcessPropertyValues(
		PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeanCreationException {

	// 找出组件中需要注入的属性,包括@Value,@Autowired,@Resource等的注入
	InjectionMetadata metadata = findAutowiringMetadata(beanName, (), pvs);
	try {
		//注入依赖,属性等
		(bean, beanName, pvs);
	}
	catch (BeanCreationException ex) {
		throw ex;
	}
	catch (Throwable ex) {
		throw new BeanCreationException(beanName, "Injection of autowired dependencies failed", ex);
	}
	return pvs;
}

4. @PropertySources注解

注解@PropertySources就是可以指定多个@PropertySource来导入配置文件,如下:

package ;

import ;
import ;
import ;

@ComponentScan("")
@PropertySources({ @PropertySource("classpath:/"), @PropertySource("classpath:") })
public class MainConfigForValue {

}