@ConfigurationProperties
model sınıfına .properties
(de desteklenir .yml
) değerleri eşlemek için kullanılabilir. Aşağıdaki Örnek dosyasını inceleyin.
.properties
cust.data.employee.name=Sachin
cust.data.employee.dept=Cricket
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@ConfigurationProperties(prefix = "cust.data.employee")
@Configuration("employeeProperties")
public class Employee {
private String name;
private String dept;
//Getters and Setters go here
}
Employee.java Şimdi özellikleri değeri aşağıdaki gibidir employeeProperties
Autowiring erişilebilir. İşte yeni özellikler ismi "otherprops.properties" ve özellik adı "myName" olduğunu file
@PropertySource(ignoreResourceNotFound = true, value = "classpath:otherprops.properties")
@Controller
public class ClassA {
@Value("${myName}")
String name;
@RequestMapping(value = "/xyz")
@ResponseBody
public void getName(){
System.out.println(name);
}
}
:
@Autowired
private Employee employeeProperties;
public void method() {
String employeeName = employeeProperties.getName();
String employeeDept = employeeProperties.getDept();
}
'daki özellik dosyasına erişmek için en basit olanıdır. En alternatif olanı, aynı zamanda "ortam" yayından da alabilir veya @ @ ConfigurationProperties' – sodik
@ sodik'in yanıtının üstüne eklemek için budur. [Ortam] 'ın nasıl alınacağını gösteren bir örnek (https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/env/Environment.html) http: // stackoverflow. com/questions/28392231/nasıl-nasıl-programlı-geçerli-aktif-profil-kullanarak-bahar-önyükleme – cristi
10'dan fazla değere erişmeniz gerekiyorsa, örnek 10 kez tekrarlamak zorunda kaldınız mı? – Jesse