@Value & @PropertySource
使用 @Value
可以为属性赋基本数值,也可以通过语法 @Value("#{SpEL}")
通过 SpEL 表达式赋值,还可以通过 @Value("${属性名}")
取出配置文件(环境变量)中的值,而 @PropertySource
注解就可以帮我们读取属性文件中的属性到环境变量。
示例
1、创建测试的 properties
文件:
user.testKey=testValue
2、创建测试 Bean:
// com.springanno.pojo.User
import org.springframework.beans.factory.annotation.Value;
public class User {
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
@Value("张三")
private String name;
@Value("#{19+3}")
private Integer age;
@Value("${user.testKey}")
private String testKey;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", testKey='" + testKey + '\'' +
'}';
}
}
3、注册 bean 到容器:
// com.springanno.config.MainConfig
import com.springanno.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource(value = {"classpath:user.properties"}) // 使用该注解将指定属性文件加载到环境变量
public class MainConfig {
@Bean
public User user() {
return new User();
}
}
4、测试:
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
User bean = applicationContext.getBean(User.class);
System.out.println(bean);
/*
User{name='张三', age=22, testKey='testValue'}
*/
@Profile
通过使用 @Profile
注解,Spring 可以实现根据当前环境动态的激活和切换一些列组件的功能。
示例
1、使用 @Profile
注解注册测试 bean 到容器:
// com.springanno.config.MainConfig
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class MainConfig {
/*
指定环境了的组件只有在指定环境下运行时才会注册到容器中,不指定环境的组件任何环境都能注册到容器中,默认为 default 环境
这里模拟三个环境下的数据源
*/
@Profile("dev")
@Bean("dataSource")
public String devStr(){
return "devDataSource";
}
@Profile("test")
@Bean("dataSource")
public String testStr(){
return "testDataSource";
}
@Profile("prod")
@Bean("dataSource")
public String prodStr(){
return "prodDataSource";
}
}
2、测试通过 VM-Options
来指定运行环境:
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
Object dataSource = applicationContext.getBean("dataSource");
System.out.println(dataSource);
// 通过 VM-options 来指定运行环境
// -Dspring.profiles.active=dev 时,输出 devDataSource
// -Dspring.profiles.active=test 时,输出 testDataSource
// -Dspring.profiles.active=prod 时,输出 prodDataSource
3、测试通过编码指定运行环境:
String profileStr = "test";
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
// 指定运行环境
applicationContext.getEnvironment().setActiveProfiles(profileStr);
applicationContext.register(MainConfig.class);
applicationContext.refresh();
Object dataSource = applicationContext.getBean("dataSource");
System.out.println(dataSource);
/*
编码指定运行环境
profileStr="test" 时,输出 testDataSource
profileStr="prod" 时,输出 prodDataSource
profileStr="dev" 时,输出 devDataSource
*/
评论区