三、属性赋值

使用@value的写法

  1. 基本数据

  2. SpEL #{}

  3. ${},取出配置文件中的值

  • Bean
public class Book {
    @Value("数据结构")
    private String name;
    @Value("#{20-3}")
    private Double price;
    @Value("${writer}")
    private String writer;
}
  • 主配置类
@PropertySource(value={"classpath:/application.properties"})
@Configuration
public class MyConfigOfPropertyValues {
    @Bean
    public Book book(){
        return new Book();
    }
}
  • application.properties
writer=jdy

 也可以使用环境变量对象获取

ConfigurableEnvironment environment = context.getEnvironment();
String writer = environment.getProperty("writer");
原文地址:https://www.cnblogs.com/jdy1022/p/13929179.html