@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=