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

目录

1. 说明

2. 注解使用

3. 注解解析


1. 说明

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

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

	<bean id="address" class="com.yibai.spring.annotation.bean.Address">
		<property name="country" value="china"></property>
		<property name="province" value="zhej"></property>
		<property name="detail" value="${address.detail}"></property>
	</bean>

</beans>

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

2. 注解使用

@Value的定义信息如下:

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Value {

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

}

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

比如:

package com.yibai.spring.annotation.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

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

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

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

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

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

	public Address() {
		log.debug("构造器");
	}

}

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

@PropertySource的定义信息如下:

package org.springframework.context.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.core.io.support.PropertySourceFactory;


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {

	String name() default "";

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

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

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

	Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;

}

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

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

package com.yibai.spring.annotation.main.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;

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

}

3. 注解解析

@Value注解的解析器是org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor,通过继承

org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor类的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, bean.getClass(), pvs);
	try {
		//注入依赖,属性等
		metadata.inject(bean, beanName, pvs);
	}
	catch (BeanCreationException ex) {
		throw ex;
	}
	catch (Throwable ex) {
		throw new BeanCreationException(beanName, "Injection of autowired dependencies failed", ex);
	}
	return pvs;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章