Spring注解驱动开发——属性赋值 @Value

2021/8/3 23:06:10

本文主要是介绍Spring注解驱动开发——属性赋值 @Value,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

使用value赋值

1丶基本数值
2丶可以写SpEL; #{}
3丶可以写${};取出配置文件中的值(在运行环境变量里面的值)

 

1丶基本数值&可以写SpEL; #{}

一丶配置类

package com.mongoubiubiu.conf;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;

import com.mongoubiubiu.bean.Person;

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

二丶测试类

package com.mongougbiubiu.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Scope;

import com.mongoubiubiu.bean.Person;
import com.mongoubiubiu.conf.MainConfigOfLifeCycle;
import com.mongoubiubiu.conf.MainConfigOfPropertyValues;

public class IOCTest_propertyValues {
    
    
    @Test
    public void Test01(){
        //创建ioc 容器
        AnnotationConfigApplicationContext applica=    new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
      
        Person person=applica.getBean(Person.class);
        System.out.println(person);
        String [] af=applica.getBeanNamesForType(Person.class);
        for (String string : af) {
            System.out.println(string);
        }
    } 

}

三丶实体类

package com.mongoubiubiu.bean;

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

public class Person {
    
    
    
    
    
    
    public Person() {
        super();
        // TODO Auto-generated constructor stub
    }


    public Person(String name, Integer age) {
        super();
        this.name = name;
        this.age = age;
    }

    /**
     *使用value赋值
     *1丶基本数值
     *2丶可以写SpEL; #{}
     *3丶可以写${};取出配置文件中的值(在运行环境变量里面的值)
     */
    @Value("李四")
    private String name;
    
    @Value("#{200-99}")
    private Integer 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 + "]";
    }
    
    

    

}

四丶返回

 

 

2丶基本数值&可以写SpEL; #{}

一丶person.properties

person.name=mangoubiubiu
person.age=18

二丶实体类

package com.mongoubiubiu.bean;

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

public class Person {
    
    
    
    
    
    
    public Person() {
        super();
        // TODO Auto-generated constructor stub
    }


    public Person(String name, Integer age) {
        super();
        this.name = name;
        this.age = age;
    }

    /**
     *使用value赋值
     *1丶基本数值
     *2丶可以写SpEL; #{}
     *3丶可以写${};取出配置文件中的值(在运行环境变量里面的值)
     */
    @Value("${person.name}")
    private String name;
    
    @Value("${person.age}")
    private Integer 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 + "]";
    }
    
    

    

}

三丶配置类使用@PropertySource  注解读取 properties 配置信息

package com.mongoubiubiu.conf;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

import com.mongoubiubiu.bean.Person;

@PropertySource(value={"classpath:/person.properties"})
@Configurable
public class MainConfigOfPropertyValues {
    
    @Bean
    public Person person(){
        return new Person();
    }
}

四丶测试类

package com.mongougbiubiu.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.core.env.ConfigurableEnvironment;

import com.mongoubiubiu.bean.Person;
import com.mongoubiubiu.conf.MainConfigOfLifeCycle;
import com.mongoubiubiu.conf.MainConfigOfPropertyValues;

public class IOCTest_propertyValues {
    
    
    @Test
    public void Test01(){
        //创建ioc 容器
        AnnotationConfigApplicationContext applica=    new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
      
        Person person=applica.getBean(Person.class);
        System.out.println(person);
        
        System.out.println("环境变量获取在下面哈============");
        ConfigurableEnvironment config=applica.getEnvironment();
        String s=config.getProperty("person.name");
        System.out.println(s);
        String [] af=applica.getBeanNamesForType(Person.class);
        for (String string : af) {
            System.out.println(string);
        }
    } 

}

五丶测试

 



这篇关于Spring注解驱动开发——属性赋值 @Value的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程