java注⼊配置⽂件_spring注解注⼊properties配置⽂件
spring⾃3.1版本后,增加了新的注解@PropertySource,⽤于注解注⼊配置⽂件的属性
以前,我们配置读取配置⽂件,⼀般都是在XML⽂件⾥⾯配置,其实,这不是很利于维护,毕竟要去XML⾥⾯配置,还需要把对象注册为bean,让xml显得过于臃肿,如下就是以前读取xml读取properties⽂件的配置,相信不少同学都知道.
classpath:properties/config_userbean.properties
classpath:properties/config_mysql.properties
但是现在我们有注解的形式来配置了,让我们先来看⼀段源码,是关于注解@PropertySource注解的
从上⾯的注释,可以发现,@propertySource注解是⾃spring 3.1版本开始有的,是⼀个配置注解,⽤于注
⼊properties⽂件的属性的.下⾯,开始上测试代码吧.
--------------------------------我是分割线--------------------------------------
先看配置⽂件⾥⾯的内容:
userBean.name=hexiaowu
userBean.sex=男
userBean.isflag=true
然后看测试注解注⼊的类:
@Component
@PropertySource(value = "classpath:properties/config_userbean.properties",ignoreResourceNotFound = true)
public class DemoAnnotation {
//注⼊peoperties⽂件⾥⾯的属性
@Value("${userBean.name}")
private String propertie_name;
/**
* 使⽤@value注解注⼊properties中的属性
* 1.在类名上⾯使⽤ @PropertySource("classpath:*") 注解,*代表属性⽂件路径,可以指向多个配置⽂件路径
* 如果是多个配置⽂件,则是 @PropertySource({"classpath:*","classpath:*"....})
* 2.ignoreResourceNotFound=true 表⽰如果配置⽂件不存在,则忽略报错
* 3.在字段上直接使⽤@value注解
* 4.注解内使⽤${userBean.name} userBean.name 代表属性⽂件⾥⾯的key
* 5.需要新增 PropertySourcesPlaceholderConfigurer 的bean
* 6.在 PropertySourcesPlaceholderConfigurer 增加@bean注解,申明返回的是⼀个bean,否则会注⼊不成功
*
*/
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
return new PropertySourcesPlaceholderConfigurer();compare和compareto
}
public String getPropertie_name() {
return propertie_name;
}
public void setPropertie_name(String propertie_name) {
this.propertie_name = propertie_name;
}
@Override
public String toString() {
return "DemoAnnotation{" +
", propertie_name='" + propertie_name + '\'' +
'}';
有同学会很纳闷了,为什么要返回⼀个 PropertySourcesPlaceholderConfigurer 的bean呢?让我们来看看源码吧.
PS:因为前⾯本⼈对 PropertySourcesPlaceholderConfigurer 理解有误,导致下⾯解释的模棱两可,因为spring是通过PropertySourcesPlaceholderConfigurer 内locations来查属性⽂件,然后在根据注解将匹配的属性set进去,⽽下⾯的注释解释,是表⽰⽤注解可以做⼀些什么操作..
public class PropertySourcesPlaceholderConfigurer extends PlaceholderConfigurerSupport
implements EnvironmentAware {
/**
* {@value} is the name given to the {@link PropertySource} for the set of
* {@linkplain #mergeProperties() merged properties} supplied to this configurer.
*/
public static final String LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME = "localProperties";
/**
* {@value} is the name given to the {@link PropertySource} that wraps the
* {@linkplain #setEnvironment environment} supplied to this configurer.
dz插件*/
public static final String ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME = "environmentProperties";
private MutablePropertySources propertySources;
private PropertySources appliedPropertySources;
private Environment environment;
下⾯代码省略.....
上⾯源码,并没能看出为什么⼀定要返回这个bean,那么我看就看看他的⽗类 PlaceholderConfigurerSupport 吧.以下是⽗类的源码/**
* Abstract base class for property resource configurers that resolve placeholders
* in bean definition property values. Implementations pull values from a
* properties file or other {@linkplain nv.PropertySource
* property source} into bean definitions.
*
*
The default placeholder syntax follows the Ant / Log4J / JSP EL style:
*
*
${...}
*
* Example XML bean definition:
*
*
{@code
*
*
*
*
*}
*
* Example properties file:
*
*
sql.jdbc.Driver
* dbname=mysql:mydb
*
* Annotated bean definitions may take advantage of property replacement using
* the {@link org.springframework.beans.factory.annotation.Value @Value} annotation:
*
*
@Value("${person.age}")
*
* Implementations check simple property values, lists, maps, props, and bean names
* in bean references. Furthermore, placeholder values can also cross-reference
* other placeholders, like:
*
properties文件用什么打开
滑轨结构图>delphi7 gettickcount*
rootPath=myrootdir
*subPath=${rootPath}/subdir
*
* In contrast to {@link PropertyOverrideConfigurer}, subclasses of this type allow
* filling in of explicit placeholders in bean definitions.
*
*
If a configurer cannot resolve a placeholder, a {@link BeanDefinitionStoreException}
* will be thrown. If you want to check against multiple properties files, specify multiple
* resources via the {@link #setLocations locations} property. You can also define multiple * configurers, each with its own placeholder syntax. Use {@link
* #ignoreUnresolvablePlaceholders} to intentionally suppress throwing an exception if a * placeholder cannot be resolved.
*
*
Default property values can be defined globally for each configurer instance
* via the {@link #setProperties properties} property, or on a property-by-property basis * using the default value separator which is {@code ":"} by default and
* customizable via {@link #setValueSeparator(String)}.
*
*
Example XML property with default value:
*
*
{@code
*
*}
*
* @author Chris Beams
* @author Juergen Hoeller
* @since 3.1
* @see PropertyPlaceholderConfigurer
* @see t.support.PropertySourcesPlaceholderConfigurer
*/
public abstract class PlaceholderConfigurerSupport extends PropertyResourceConfigurer
implements BeanNameAware, BeanFactoryAware {
/** Default placeholder prefix: {@value} */
public static final String DEFAULT_PLACEHOLDER_PREFIX = "${";
/** Default placeholder suffix: {@value} */
public static final String DEFAULT_PLACEHOLDER_SUFFIX = "}";
liability/** Default value separator: {@value} */
public static final String DEFAULT_VALUE_SEPARATOR = ":";
/** Defaults to {@value #DEFAULT_PLACEHOLDER_PREFIX} */
protected String placeholderPrefix = DEFAULT_PLACEHOLDER_PREFIX;
/** Defaults to {@value #DEFAULT_PLACEHOLDER_SUFFIX} */
protected String placeholderSuffix = DEFAULT_PLACEHOLDER_SUFFIX;
/** Defaults to {@value #DEFAULT_VALUE_SEPARATOR} */
protected String valueSeparator = DEFAULT_VALUE_SEPARATOR;
protected boolean ignoreUnresolvablePlaceholders = false;
protected String nullValue;
private BeanFactory beanFactory;
private String beanName;
类注释表⽰的是,该类所起的作⽤,替代了xml⽂件的哪些操作,我们只需要看下⾯定义的⼀个常量注解就能⼤概知道,为什么需要返回这么⼀个bean了.
类注释上有这么⼀句话:表⽰可以⽤带注释的bean,可以利⽤属性符号进⾏替换
* Annotated bean definitions may take advantage of property replacement using
* the {@link org.springframework.beans.factory.annotation.Value @Value} annotation:
*
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论