美文网首页
@Value属性赋值

@Value属性赋值

作者: JBryan | 来源:发表于2020-01-19 10:28 被阅读0次

使用@Value赋值
1.基本数值
2.可以写SPEL表达式#{}
3.可以写${},取出配置文件的值(运行环境的值)
新建Person类

package com.ljessie.bean;

import org.springframework.beans.factory.annotation.Value;

public class Person {

    /**
     * 使用@Value赋值
     *1.基本数值
     * 2.可以写SPEL表达式#{}
     * 3.可以写${},取出配置文件的值(运行环境的值)
     */
    @Value("Jessie")
    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;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", 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;
    }
}

在Resources下,新建person.properties

person.nickName=Jessie

新建MainConfigOfPropertyValues

package com.ljessie.config;

import com.ljessie.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

//使用@PropertySource读取外部配置文件的属性
@PropertySource(value = "classpath:/person.properties")
@Configuration
public class MainConfigOfPropertyValues {

    @Bean
    public Person person(){
        return new Person();
    }
}

新建测试类IOCTest_PropertyValue

package com.ljessie.test;

import com.ljessie.bean.Person;
import com.ljessie.config.MainConfigOfLifeCycle;
import com.ljessie.config.MainConfigOfPropertyValues;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

public class IOCTest_PropertyValue {

    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);


    @Test
    public void test01(){
        printBeans(annotationConfigApplicationContext);
        Person person = (Person) annotationConfigApplicationContext.getBean("person");
        System.out.println(person);

        ConfigurableEnvironment environment = annotationConfigApplicationContext.getEnvironment();
        String property = environment.getProperty("person.nickName");
        System.out.println("person.nickName:"+property);
    }


    public void printBeans(AnnotationConfigApplicationContext annotationConfigApplicationContext){
        String[] beanDefinitionNames = annotationConfigApplicationContext.getBeanDefinitionNames();
        for (String name:beanDefinitionNames) {
            System.out.println(name);
        }
    }
}

运行test01():

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='Jessie', age=18, nickName='Jessie'}
person.nickName:Jessie

相关文章

网友评论

      本文标题:@Value属性赋值

      本文链接:https://www.haomeiwen.com/subject/dbjczctx.html