[java]@PropertySource和@ConfigurationProperties区别


@PropertySource

@PropertySource 是spring的注解
加载指定的属性文件的配置到 Spring 的 Environment 中。可以配合 @Value 和 @ConfigurationProperties 使用。
用法:

@Configuration+ @PropertySource+Environment
@Configuration+ @PropertySource+@Value
@Configuration+ @PropertySource+@ConfigurationProperties

@Configuration本质也是告诉spring这是一个bean.

my.name=helloworld
my.age=8

@Configuration+ @PropertySource+Environment

@Configuration
@PropertySource("classpath:hellword.properties")
public class HelloWorldConfig {
   @Autowired
    private Environment env;
}

@Configuration+ @PropertySource+@Value

@Configuration
@PropertySource("classpath:hellword.properties")
public class HelloWorldConfig {

  @Value(${my.name})
  private String name;
}

@Configuration+ @PropertySource+@ConfigurationProperties

@PropertySource指定加载哪个文件,@ConfigurationProperties指定加载文件中的哪一类属性。
@PropertySource+@ConfigurationProperties在一起解决了@ConfigurationProperties只能加载主文件内属性问题。

@Configuration
@PropertySource("classpath:hellword.properties")
@ConfigurationProperties(prefix = "my")
public class HelloWorldConfig {
  private String name;
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM