Aynı şeyi başarmanın çeşitli yolları vardır. Aşağıda baharda yaygın olarak kullanılan bazı yollar bulunmaktadır.
PropertyPlaceholderConfigurer'ı kullanma
PropertySource'u kullanma
ResourceBundleMessageSource'u kullanma
PropertiesFactoryBean'i kullanma
ve daha fazlası........................
ds.typeMülk dosyanızın anahtarı olduğunu varsayarsak .
kullanma PropertyPlaceholderConfigurer
Kayıt PropertyPlaceholderConfigurerfasulyesi
<context:property-placeholder location="classpath:path/filename.properties"/>
veya
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:path/filename.properties" ></property>
</bean>
veya
@Configuration
public class SampleConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
//set locations as well.
}
}
Kayıt olduktan sonra PropertySourcesPlaceholderConfigurer, değere erişebilirsiniz-
@Value("${ds.type}")private String attr;
kullanma PropertySource
Son bahar sürümünde kayıt gerekmez PropertyPlaceHolderConfigurerile @PropertySource, iyi bir buldum linki versiyonu compatibility- anlamak
@PropertySource("classpath:path/filename.properties")
@Component
public class BeanTester {
@Autowired Environment environment;
public void execute() {
String attr = this.environment.getProperty("ds.type");
}
}
kullanma ResourceBundleMessageSource
Bean adına kaydolun-
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:path/filename.properties</value>
</list>
</property>
</bean>
Erişim Değeri
((ApplicationContext)context).getMessage("ds.type", null, null);
veya
@Component
public class BeanTester {
@Autowired MessageSource messageSource;
public void execute() {
String attr = this.messageSource.getMessage("ds.type", null, null);
}
}
kullanma PropertiesFactoryBean
Bean adına kaydolun-
<bean id="properties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:path/filename.properties</value>
</list>
</property>
</bean>
Özellikler örneğini sınıfınıza bağlayın-
@Component
public class BeanTester {
@Autowired Properties properties;
public void execute() {
String attr = properties.getProperty("ds.type");
}
}