spring注解注入properties配置文件
spring注解注⼊properties配置⽂件
早期,如果需要通过spring读取properties⽂件中的配置信息,都需要在XML⽂件中配置⽂件读取⽅式。
基于XML的读取⽅式:
1<bean class="org.springframework.fig.PropertyPlaceholderConfigurer">
2<property name="locations">
3<list>
4<value>classpath:properties/thread-pool.properties</value>
5</list>
6</property>
7</bean>
当然,这种⽅式可以统⼀管理properties配置⽂件,也能实现代码的松耦合。但为了⽅便开发,提⾼开发
效率,spring官⽅后来提供了基于注解的配置读取⽅式。两种⽅式各有优势,可以基于对项⽬的考虑选择最合适的⽅式。接下来就介绍如何通过注解注⼊properties的配置信息。
⾸先,准备配置⽂件:
1 core.pool.size=2
2 max.pool.size=3
3 keep.alive.time=1
4 task.queue.size=3
ination.time=5
定义配置类:
fig;
2
le.gson.Gson;
4import org.springframework.beans.factory.annotation.Value;
5import t.annotation.Bean;
6import t.annotation.PropertySource;
7import t.support.PropertySourcesPlaceholderConfigurer;
8import org.springframework.stereotype.Component;
9
10/**
11 *
12*/
13 @Component
14 @PropertySource("classpath:properties/thread-pool.properties")
15public class ThreadPoolConfig {
16/**
17    * 核⼼线程个数
18*/
19    @Value("${core.pool.size}")
20private int corePoolSize;
赵容弼21/**
22    * 最⼤线程个数
23*/
24    @Value("${max.pool.size}")
25private int maxPoolSize;
26/**
27    * 保持⼼跳时间
28*/
29    @Value("${keep.alive.time}")
30private int keeAliveTime;
31/**
32    * 任务队列长度
33*/
34    @Value("${task.queue.size}")端午节手抄报内容大全
35private int taskQueueSize;
36/**
37    * 等待任务结束的时间
38*/
39    @Value("${ination.time}")
40private int awaitTerminationTime;
41
42/**
43    * 使⽤@value注解注⼊properties中的属性
44    * 1. 在类名上⾯使⽤  @PropertySource("classpath:*") 注解,*代表属性⽂件路径,可以指向多个配置⽂件路径
45    *      如果是多个配置⽂件,则是 @PropertySource({"classpath:*","classpath:*"....})
46    * 2. 在字段上直接使⽤@value注解
47    * 3. 注解内使⽤${core.pool.size}  core.pool.size 代表属性⽂件⾥⾯的key
48    * 5. 需要新增 PropertySourcesPlaceholderConfigurer 的 bean
49    * 6. 在 PropertySourcesPlaceholderConfigurer 增加@bean注解,申明返回的是⼀个bean,否则会注⼊失败
50    *
51*/
52
53
55    @Bean
56public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
57return new PropertySourcesPlaceholderConfigurer();
58    }
59
60public int getCorePoolSize() {
61return corePoolSize;
62    }
63
64public void setCorePoolSize(int corePoolSize) {
66    }
67
68public int getMaxPoolSize() {
69return maxPoolSize;
70    }
71
72public void setMaxPoolSize(int maxPoolSize) {
73this.maxPoolSize = maxPoolSize;
74    }
75
76public int getKeeAliveTime() {
77return keeAliveTime;
78    }
79
80public void setKeeAliveTime(int keeAliveTime) {
81this.keeAliveTime = keeAliveTime;
82    }
83
84public int getTaskQueueSize() {
85return taskQueueSize;
86    }
87
88public void setTaskQueueSize(int taskQueueSize) {
89this.taskQueueSize = taskQueueSize;
90    }
91
92public int getAwaitTerminationTime() {
93return awaitTerminationTime;
94    }
95
96public void setAwaitTerminationTime(int awaitTerminationTime) {
97this.awaitTerminationTime = awaitTerminationTime;
98    }
99
100    @Override
101public String toString() {
102return new Gson().toJson(this);
103    }
104 }
这⾥注⼊了⼀个 PropertySourcesPlaceholderConfigurer  bean,spring是通过 PropertySourcesPlaceholderConfigurer 的 locations 来查属性⽂件,然后再根据注解将匹配的属性set进去,下⾯通过源码来了解注解可以进⾏⼀些什么操作。
1public class PropertySourcesPlaceholderConfigurer extends PlaceholderConfigurerSupport implements EnvironmentAware {
2
3/**
4    * {@value} is the name given to the {@link PropertySource} for the set of
5    * {@linkplain #mergeProperties() merged properties} supplied to this configurer.
6*/
7public static final String LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME = "localProperties";
损益类科目
8
9/**
10    * {@value} is the name given to the {@link PropertySource} that wraps the
11    * {@linkplain #setEnvironment environment} supplied to this configurer.
12*/
13public static final String ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME = "environmentProperties";
14
15
16    @Nullable
17private MutablePropertySources propertySources;
18
19    @Nullable
20private PropertySources appliedPropertySources;
21
22    @Nullable
23private Environment environment;
下⾯代码省略。。。
上⾯源码并没能说明为什么⼀定要返回这个bean,接下来看⽗类 PlaceholderConfigurerSupport 的源码:
2 * Abstract base class for property resource configurers that resolve placeholders
3 * in bean definition property values. Implementations <em>pull</em> values from a
4 * properties file or other {@linkplain nv.PropertySource
5 * property source} into bean definitions.
6 *
7 * <p>The default placeholder syntax follows the Ant / Log4J / JSP EL style:
8 *
9 * <pre class="code">${...}</pre>
10 *
11 * Example XML bean definition:
12 *
13 * <pre class="code">
14 * <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"/>
15 *  <property name="driverClassName" value="${driver}"/>
16 *  <property name="url" value="jdbc:${dbname}"/>
17 * </bean>
18 * </pre>
19 *
20 * Example properties file:
21 *
22 * <pre class="code">sql.jdbc.Driver
23 * dbname=mysql:mydb</pre>
24 *
25 * Annotated bean definitions may take advantage of property replacement using
26 * the {@link org.springframework.beans.factory.annotation.Value @Value} annotation:
27 *
28 * <pre class="code">@Value("${person.age}")</pre>
29 *
30 * Implementations check simple property values, lists, maps, props, and bean names
31 * in bean references. Furthermore, placeholder values can also cross-reference
32 * other placeholders, like:
33 *
34 * <pre class="code">rootPath=myrootdir
35 * subPath=${rootPath}/subdir</pre>
36 *
37 * In contrast to {@link PropertyOverrideConfigurer}, subclasses of this type allow
38 * filling in of explicit placeholders in bean definitions.
39 *
40 * <p>If a configurer cannot resolve a placeholder, a {@link BeanDefinitionStoreException}
41 * will be thrown. If you want to check against multiple properties files, specify multiple
42 * resources via the {@link #setLocations locations} property. You can also define multiple
43 * configurers, each with its <em>own</em> placeholder syntax. Use {@link
44 * #ignoreUnresolvablePlaceholders} to intentionally suppress throwing an exception if a
45 * placeholder cannot be resolved.
46 *
47 * <p>Default property values can be defined globally for each configurer instance
48 * via the {@link #setProperties properties} property, or on a property-by-property basis
49 * using the default value separator which is {@code ":"} by default and
50 * customizable via {@link #setValueSeparator(String)}.
51 *
52 * <p>Example XML property with default value:
53 *
54 * <pre class="code">
55 *  <property name="url" value="jdbc:${dbname:defaultdb}"/>
56 * </pre>
57 *
58 * @author Chris Beams
59 * @author Juergen Hoeller
60 * @since 3.1
61 * @see PropertyPlaceholderConfigurer
62 * @see t.support.PropertySourcesPlaceholderConfigurer
63*/
64public abstract class PlaceholderConfigurerSupport extends PropertyResourceConfigurer
65implements BeanNameAware, BeanFactoryAware {
66
67/** Default placeholder prefix: {@value} */
68public static final String DEFAULT_PLACEHOLDER_PREFIX = "${";
69
70/** Default placeholder suffix: {@value} */
71public static final String DEFAULT_PLACEHOLDER_SUFFIX = "}";
72
73/** Default value separator: {@value} */
74public static final String DEFAULT_VALUE_SEPARATOR = ":";
75
76
77/** Defaults to {@value #DEFAULT_PLACEHOLDER_PREFIX} */
78protected String placeholderPrefix = DEFAULT_PLACEHOLDER_PREFIX;
79
80/** Defaults to {@value #DEFAULT_PLACEHOLDER_SUFFIX} */
81protected String placeholderSuffix = DEFAULT_PLACEHOLDER_SUFFIX;
82
83/** Defaults to {@value #DEFAULT_VALUE_SEPARATOR} */
84    @Nullable
85protected String valueSeparator = DEFAULT_VALUE_SEPARATOR;
86
87protected boolean trimValues = false;
88
89    @Nullable
90protected String nullValue;
91
山东医学院有哪些92protected boolean ignoreUnresolvablePlaceholders = false;
93
94    @Nullable
95private String beanName;
96
97    @Nullable
98private BeanFactory beanFactory;
下⾯代码省略。。。
类注释说明了 PlaceholderConfigurerSupport 类所起的作⽤,以及替换了XML的哪些操作,其中就描述了注⼊的bean可以利⽤ @Value 注解进⾏属性替换:
* Annotated bean definitions may take advantage of property replacement using
* the {@link org.springframework.beans.factory.annotation.Value @Value} annotation:
*
* <pre class="code">@Value("${person.age}")</pre>
属性注释:
1/** Default placeholder prefix: {@value} */
2public static final String DEFAULT_PLACEHOLDER_PREFIX = "${";
3
4/** Default placeholder suffix: {@value} */
5public static final String DEFAULT_PLACEHOLDER_SUFFIX = "}";
6
7/** Default value separator: {@value} */
8public static final String DEFAULT_VALUE_SEPARATOR = ":";
9
10
8月份的黄道吉日查询2022年
11/** Defaults to {@value #DEFAULT_PLACEHOLDER_PREFIX} */
12protected String placeholderPrefix = DEFAULT_PLACEHOLDER_PREFIX;
13
14/** Defaults to {@value #DEFAULT_PLACEHOLDER_SUFFIX} */
15protected String placeholderSuffix = DEFAULT_PLACEHOLDER_SUFFIX;
16
17/** Defaults to {@value #DEFAULT_VALUE_SEPARATOR} */
18    @Nullable
19protected String valueSeparator = DEFAULT_VALUE_SEPARATOR;
20
21protected boolean trimValues = false;
22
23    @Nullable
24protected String nullValue;
25
26protected boolean ignoreUnresolvablePlaceholders = false;
从上⾯注解可以发现,使⽤的默认前缀是:'${',⽽后缀是:'}',默认的分隔符是 ':',但是set⽅法可以替换掉默认的分隔符,
⽽  ignoreUnresolvablePlaceholders  默认为 false,表⽰会开启配置⽂件不存在,抛出异常的错误。
从上⾯就能看出这个bean所起的作⽤,就是将 @propertySource 注解的bean注⼊属性的作⽤,如果没有该bean,则不能解析${}符号。
接下来执⾏单元测试。
配置类代码:
llphone.web;
2
3import t.annotation.ComponentScan;
4import t.annotation.Configuration;
5
6/**
7 * 配置类——⽤来替换xml配置⽂件
8*/
9 @Configuration
10 @ComponentScan("fig")
11public class SpringConfig {
12 }
llphone.web;
2
fig.ThreadPoolConfig;
4import org.junit.Test;
5import org.junit.runner.RunWith;
6import org.slf4j.Logger;
7import org.slf4j.LoggerFactory;
8import org.springframework.beans.factory.annotation.Autowired;
9import st.context.ContextConfiguration;
10import st.context.junit4.SpringJUnit4ClassRunner;
11
12/**
13 * 纯注解⽅式整合Junit单元测试框架测试类
14*/
15 @RunWith(SpringJUnit4ClassRunner.class)
16 @ContextConfiguration(classes = { SpringConfig.class }) // 需要注意此处,将加载配置⽂件的注解换成加载配置类的注解
17public class ThreadPoolConfigTest {
18
19private final Logger logger = Logger(getClass());
20
21    @Autowired
22private ThreadPoolConfig threadPoolConfig;
23
24    @Test
25public void testThreadPoolConfig() {
26        logger.String());
27    }
重装机兵2重制版攻略28 }
使⽤  @PropertySource 注解需要注意以下⼏个地⽅:
1. 使⽤注解需要将类申明为⼀个bean,可以使⽤ @Component 注解;
2. @PropertySource(value = "classpath:properties/config_userbean.properties", ignoreResourceNotFound = true) 表⽰注⼊配置⽂件,并且忽略配置⽂件不存在的异常;
3. 必须返回⼀个  PropertySourcesPlaceholderConfigurer  的bean,否则会不能识别@Value("${core.pool.size}") 注解中的 ${core.pool.size}指向的value,⽽会注⼊${core.pool.size}的字符串,返回 PropertySourcesPlaceholderConfigurer 的⽅法,使⽤ @Bean 注解,表⽰返回的是个bean。
在spring 4.0以后,spring增加了 @PropertySources  注解,可以使⽤多个 @PropertySource 注解,如下:
@PropertySources(
{
@PropertySource("classpath:properties/thread-pool.properties"),
@PropertySource("classpath:properties/mysql.properties")
}
)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。