ci sono vari modi per raggiungere lo stesso, seguito sono riportati alcuni modi di uso comune a molla
- Utilizzo di PropertyPlaceholderConfigurer
- Utilizzo di Pro pertySource
- Utilizzando ResourceBundleMessageSource
Utilizzando PropertiesFactoryBean
e molti altri ........................
Supponendo ds.type
è la chiave nel file di proprietà.
Utilizzando PropertyPlaceholderConfigurer
Registrati PropertyPlaceholderConfigurer
bean-
<context:property-placeholder location="classpath:path/filename.properties"/>
o
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:path/filename.properties" ></property>
</bean>
o
@Configuration
public class SampleConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
//set locations as well.
}
}
dopo la registrazione PropertySourcesPlaceholderConfigurer
, ora è possibile accedere a valore
@Value("${ds.type}")private String attr;
Utilizzando PropertySource
Nella versione più recente primavera non c'è bisogno di registrarsi con PropertyPlaceHolderConfigurer
@PropertySource
, ho trovato un buon link per comprendere la comaptibility della versione-
@PropertySource("classpath:path/filename.properties")
@Component
public class BeanTester {
@Autowired Environment environment;
public void execute(){
String attr = this.environment.getProperty("ds.type");
}
}
Utilizzando ResourceBundleMessageSource
Registrati Bean-
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:path/filename.properties</value>
</list>
</property>
</bean>
accesso Value-
((ApplicationContext)context).getMessage("ds.type", null, null);
o
@Component
public class BeanTester {
@Autowired MessageSource messageSource;
public void execute(){
String attr = this.messageSource.getMessage("ds.type", null, null);
}
}
Utilizzando PropertiesFactoryBean
Registrati Bean-
<bean id="properties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:path/filename.properties</value>
</list>
</property>
</bean>
Proprietà Wire esempio in voi Aula
@Component
public class BeanTester {
@Autowired Properties properties;
public void execute(){
String attr = properties.getProperty("ds.type");
}
}
Questo non sembra un [Proprietà] (http : //en.wikipedia.org/wiki/.propertie s) file. – Raghuram
Se si tratta di un file di proprietà nel senso di Java - sì. Altrimenti è un formato di file personalizzato che deve essere trattato in modo diverso (e non puoi usare le linee come valori di proprietà in Primavera se non hanno una chiave). –
"Non in modo tradizionale" - cosa intendi con questo? –