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;
}

 

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