Şu anda bir özellik dosyası yükleyen bir Spring xml yapılandırması (Yay 4) kullanıyorum. özellikleriniSpring yer tutucusu, JavaConfig içindeki özellikleri çözmez
@Component
public class MyComponent {
@Value("${my.app.other}")
private String others;
}
Bu mükemmel çalışıyor ve hariç olarak others
değeri, MyService/sample
olduğunu kullanmak
context.properties
my.app.service = myService
my.app.other = ${my.app.service}/sample
Bahar xml yapılandırma
<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. Ancak bu yapılandırmayı bir JavaConfig ile değiştirmeye çalıştığımda, bileşenimdeki @Value
aynı şekilde çalışmıyor. Değer myService/sample
değil, ${my.app.service}/sample
değil.
@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;
}
}
Xml'den Javaconfig'e dönüştürme işleminde bir şeyi özledim mi?
ps: Ayrıca bir PropertyPlaceholderConfigurer
yerine PropertySourcesPlaceholderConfigurer
'u daha fazla başarıya ulaştırmaya çalışıyorum. Yapılandırma PropertySourcesPlaceholderConfigurer
'u kullanmak üzere güncelleştirmek için güncelle
Sorununuzu çözmese bile, 'PropertySourcesPlaceholderConfigurer' kullanmanız gerektiğini onaylayabilirim, 'PropertyPlaceholderConfigurer' değil, – superbob
' my.app.service' özelliği düzeltilsin mi? '@Value (" $ {my.app.service} ")' ile kontrol edin, sorun özellik kümesinden geliyorsa dolaşırım. – superbob