三、属性赋值

使用@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");

 

posted @ 2020-11-04 22:40  jingdy  阅读(92)  评论(0编辑  收藏  举报