spring注解導入配置文件


@Configuration
@ImportResource("classpath:config.xml")
public class StoreConfig {
 
    @Value("${jdbc.url}")
    private String url;
     
    @Value("${jdbc.username}")
    private String username;
     
    @Value("${jdbc.password}")
    private String password;
     
    @Bean
    public MyDriverManager myDriverManager(){
        return new MyDriverManager(url,username,password);
    }
}

  @Value 不需要get、set方法

ConfigurationProperties 注解,是需要的,他能幫你自動將配置文件的對應的  prefix開頭的屬性,注入。
@ConfigurationProperties(locations = "classpath:mail.properties", 
                         ignoreUnknownFields = false, 
                         prefix = "mail")
public class MailProperties { 
  public static class Smtp {  
    private boolean auth;  
    private boolean starttlsEnable;  
    // ... getters and setters 
  }
  @NotBlank private String host;
  private int port;  
  private String from; 
  private String username;
  private String password; 
  @NotNull private Smtp smtp; 
  // ... getters and setters
}

  

mail.host=localhost
mail.port=25
mail.smtp.auth=false
mail.smtp.starttls-enable=false
mail.from=me@localhost
mail.username=
mail.password=

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM