Hiện tại tôi có cấu hình Spring xml (Spring 4) tải một tệp thuộc tính.Trình giữ chỗ mùa xuân không giải quyết các thuộc tính trong JavaConfig
context.properties
my.app.service = myService
my.app.other = ${my.app.service}/sample
cấu hình Spring xml
<bean id="contextProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="ignoreResourceNotFound" value="true" />
<property name="fileEncoding" value="UTF-8" />
<property name="locations">
<list>
<value>classpath:context.properties</value>
</list>
</property>
</bean>
<bean id="placeholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="properties" ref="contextProperties" />
<property name="nullValue" value="@null" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
</bean>
Bean mà sử dụng các thuộc tính
@Component
public class MyComponent {
@Value("${my.app.other}")
private String others;
}
này hoạt động hoàn hảo và giá trị others
là MyService/sample
, như loại trừ. Nhưng khi tôi cố gắng thay thế cấu hình này bằng JavaConfig, thì @Value
trong thành phần của tôi không hoạt động theo cùng một cách. giá trị không phải là myService/sample
nhưng ${my.app.service}/sample
.
@Configuration
@PropertySource(name="contextProperties", ignoreResourceNotFound=true, value={"classpath:context.properties"})
public class PropertiesConfiguration {
@Bean
public static PropertyPlaceholderConfigurer placeholder() throws IOException {
PropertyPlaceholderConfigurer placeholder = new PropertyPlaceholderConfigurer();
placeholder.setNullValue("@null");
placeholder.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
return placeholder;
}
}
Tôi có bỏ lỡ điều gì đó trong chuyển đổi từ xml sang Javaconfig không?
ps: Tôi cũng cố gắng tạo nhanh PropertySourcesPlaceholderConfigurer
thay vì PropertyPlaceholderConfigurer
mà không thành công hơn.
Ngay cả khi nó không giải quyết được sự cố của bạn, tôi có thể xác nhận rằng bạn nên sử dụng 'PropertySourcesPlaceholderConfigurer', không phải' PropertyPlaceholderConfigurer' – superbob
Thuộc tính 'my.app.service' có được giải quyết không? Kiểm tra bằng '@Value (" $ {my.app.service} ")', tôi đi lang thang nếu vấn đề xuất phát từ thuộc tính lồng nhau. – superbob