[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