@PropertySource vs @ConfigurationProperties
Trong bài này tôi sẽ hướng dẫn bạn cách tiêm giá trị từ file *.properties vào code trong spring về cơ bản là tôi sẽ tiêm các giá trị như String hoặc số int đơn giản cho bạn dễ hiểu
1. Cách thứ nhất
với @PropertySource bạn dễ dàng làm được điều này, cái tên nó đã gợi nhớ đến tính năng Nguồn thuộc tính
tôi ví dụ bạn có một file
demo.properties // đặt cùng cấp với file application.properties trong thư mục resources
nội dung file như sau
và class bên dưới được tiêm giá trị vào
2. Cách thứ 2 dùng @ConfigurationProperties
cách này bắt buộc phải có setter và getter nhé
3. Cách thứ 3
Chú ý:
Tất cả các trường hợp trên
Khi bạn khai báo như trên, thì nó sẽ tìm thuộc tính mail.port trong file application.properties trước, nếu có nó sẽ trả về giá trị trong đó luôn
Còn nếu không có thi nó sẽ tìm trong file demo.properties
1. Cách thứ nhất
với @PropertySource bạn dễ dàng làm được điều này, cái tên nó đã gợi nhớ đến tính năng Nguồn thuộc tính
tôi ví dụ bạn có một file
demo.properties // đặt cùng cấp với file application.properties trong thư mục resources
nội dung file như sau
hobby=Reading sex=male mail.from =test@test.com mail.host =test@test.com mail.port =25 mail.security.userName =test mail.security.password =test
và class bên dưới được tiêm giá trị vào
@Component @PropertySource("classpath:demo.properties") public class DemoProperties { @Value(value = "${mail.from}") String from; @Value(value = "${sex}") String sex; }
2. Cách thứ 2 dùng @ConfigurationProperties
cách này bắt buộc phải có setter và getter nhé
@Configuration @PropertySource("classpath:demo.properties") @ConfigurationProperties("mail") public class ApplicationConfigurationProp { String from; String host; int port; Security security = new Security(); public static class Security { private String userName; private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public Security getSecurity() { return security; } public void setSecurity(Security security) { this.security = security; } }
3. Cách thứ 3
@PropertySource("classpath:demo.properties") public class PersonController { @Autowired Environment env; public Greeting greeting() throws ResponseStatusException { System.out.println("==== in greeting ===="); return new Greeting(1, env.getProperty("mail.port")); } }
Chú ý:
Tất cả các trường hợp trên
Khi bạn khai báo như trên, thì nó sẽ tìm thuộc tính mail.port trong file application.properties trước, nếu có nó sẽ trả về giá trị trong đó luôn
Còn nếu không có thi nó sẽ tìm trong file demo.properties
Comments
Post a Comment