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