Spring注解[email protected]、@PropertySource
程序员文章站
2022-05-02 12:13:06
...
Bean
package pers.zhang.bean;
import org.springframework.beans.factory.annotation.Value;
public class Person {
//使用@Value赋值
//1. 基本数值
//2. SpEL: #{}
//3. ${}:取出配置文件中的值(在运行环境中的值)
@Value("张三")
private String name;
@Value("#{20 - 2}")
private Integer age;
@Value("${person.nickName}")
private String nickName;
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public Person() {
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
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 "Person{" +
"name='" + name + '\'' +
", age=" + age +
", nickName='" + nickName + '\'' +
'}';
}
}
主配置文件
package pers.zhang.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import pers.zhang.bean.Person;
//加载外部配置文件保存到运行的环境变量中,参数为String[]
@PropertySource(value = {"classpath:/person.properties"})
//一次指定多个PropertySource
//@PropertySources()
@Configuration
public class MainConfigOfPropertyValues {
@Bean
public Person person(){
return new Person();
}
}
配置文件:
person.nickName=nikie
测试:
@Test
public void test01(){
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
String[] names = ac.getBeanDefinitionNames();
for(String name : names){
System.out.println(name);
}
System.out.println("****************************************");
Person person = (Person) ac.getBean("person");
System.out.println(person);
System.out.println("****************************************");
//通过环境变量获取配置文件中的值
String s = ac.getEnvironment().getProperty("person.nickName");
System.out.println(s);
}
输出:
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfigOfPropertyValues
person
****************************************
Person{name='张三', age=18, nickName='nikie'}
****************************************
nikie
推荐阅读
-
Spring boot中PropertySource注解的使用方法详解
-
SpringMVC常用注解[email protected]
-
spring boot中关于获取配置文件注解的使用@ConfigurationProperties、@Value、@PropertySource
-
Spring注解开发—— 5、组件注册[email protected]设置组件作用域
-
Spring注解开发——2、组件注册[email protected]&@Bean给容器中注册组件
-
Spring注解开发——7、组件注册[email protected]按照条件注册bean
-
Spring注解开发——12、生命周期[email protected]指定初始化和销毁方法
-
Spring4.0系列[email protected]
-
Spring注解[email protected]注解
-
spring_注解[email protected][email protected][email protected]_ @Lazy