Spring 与属性赋值相关的注解之 @Value/@PropertySources/@PropertySource
2019-04-15 16:35
579 查看
前言:
前面已经总结了 Spring 与组件注入相关的注解,现在总结一些 Spring 与属性赋值相关的注解。
准备工作:
- 以前在 xml 配置文件中对对象进行赋值是通过如下形式:
<bean id="person" class="com.uestc.auto.xiaoxie.bean.Person"> <property name="age" value="23"></property> <property name="name" value="Karolina"></property> </bean>
当然,上面的例子是最简单的一种使用方式。除了这种基本数据(数字/字符串)之外,value 属性处还可以用 SpEL 表达式或者从配置文件中读取内容。而 Spring 注解可以完成同样的工作。
- 改下之前的 Person 类,多加一个 private String email 属性。顺便修改下 setter/getter/toString 方法等等。
- 在 src/main/resources 下添加一个 person.properties 文件(待会儿就是要读取这个文件中的值的),内容如下:
person.email=xxx@qq.com
实验:
- 利用 @Value 注解进行属性的赋值:
package com.uestc.auto.xiaoxie.bean; import org.springframework.beans.factory.annotation.Value; public class Person { @Value("Adriana") // 传入基本数据(数字/字符串) private String name; @Value("#{19 + 18}") // 用 SpEL 表达式进行赋值 private int age; @Value("${person.email}") // 从配置文件中读取值 private String email; public Person() { super(); } public Person(String name, int age) { super(); this.name = name; this.age = age; } // 由于篇幅原因,省略 setter getter 的定义 @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", email=" + email + "]"; } }
- 定义配置类,在配置类上添加 @PropertySource 注解,读取 person.properties 文件中的内容。
和 @ComponentScan 注解一样,这也是一个可重复注解,即我们可以用 @PropertySources 包裹多个 @PropertySource。
@PropertySource(value={"classpath:/person.properties"}) // 注意,自己的配置文件的路径 @Configuration public class SpringConfig3 { @Bean public Person person(){ return new Person(); } }
@PropertySource(value={"classpath:/person.properties"}) 就等价于之前在 xml 配置文件中的 <context:property-placeholder location="classpath:/person.properties"/>
- 编写单元测试方法:
(略去了 AnnotationConfigApplicationContext 的创建)
@Test public void test1(){ Person person = context.getBean(Person.class); System.out.println(person); }
之前也说过,配置文件中的内容能够从当前环境中获取,所以不用 @Value 注解也能读取配置文件中的内容:
@Test public void test2(){ ConfigurableEnvironment environment = context.getEnvironment(); String email = environment.getProperty("person.email"); System.out.println(email); }
- 测试结果如下所示:
Person [name=Adriana, age=37, email=xxx@qq.com] xxx@qq.com
相关文章推荐
- Spring注解驱动开发-属性赋值@Value&@PropertySource
- spring注解 配置属性 Environment@PropertySource 配置数据源
- Spring 注解 hibernate 实体方法 <property name="packagesToScan" value="com.sise.domain"/>
- Spring注解@Value及属性加载配置文件
- SpringBoot自定义注解@YamlPropertySource加载yml或者yaml文件(扩展了@PropertySource)
- Spring3.1新属性管理API:PropertySource、Environment、Profile
- Spring3.1新属性管理API:PropertySource、Environment、Profile
- Spring3.1新属性管理API:PropertySource、Environment、Profi
- [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
- spring利用注解@Value获取properties属性为null
- Spring 注解 hibernate 实体方法 <property name="packagesToScan" value="com.sise.domain"/>
- Spring3.1新属性管理API:PropertySource、Environment、Profile
- spring属性赋值@Value赋值与@PropertySource加载外部配置文件
- Spring3.1新属性管理API:PropertySource、Environment、Profile
- Spring 注解 hibernate 实体方法 <property name="packagesToScan" value="com.sise.domain"/>
- Spring3.1新属性管理API:PropertySource、Environment、Profile
- 关于spring使用@value注解获取properties文件属性问题
- 第五章:SpringBoot配置(@PropertySource @ImportResource @Bean 注解)
- Spring注解开发之属性赋值
- spring 使用注解装配的Bean如何使用property-placeholder属性配置中的值