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

目录

1. 说明

2. 注解使用

3. 注解解析

4. @PropertySources注解


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

4. @PropertySources注解

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

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

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

@ComponentScan("com.yibai.spring.annotation.bean")
@PropertySources({ @PropertySource("classpath:/a.properties"), @PropertySource("classpath:b.properties") })
public class MainConfigForValue {

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值