欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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