如何从属性文件中读取值?

我正在使用spring。 我需要从属性文件中读取值。 这是内部属性文件而不是外部属性文件。 属性文件可以如下。

some.properties ---file name. values are below. abc = abc def = dsd ghi = weds jil = sdd 

我需要从传统的方式读取属性文件中的值。 如何实现它? Spring 3.0有什么最新的方法吗?

在您的上下文中configurationPropertyPlaceholder:

 <context:property-placeholder location="classpath*:my.properties"/> 

然后你引用你的bean中的属性:

 @Component class MyClass { @Value("${my.property.name}") private String[] myValues; } 

编辑:更新代码parsing与多个逗号分隔值的属性:

 my.property.name=aaa,bbb,ccc 

如果这不起作用,你可以定义一个bean的属性,手动注入和处理:

 <bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath*:my.properties</value> </list> </property> </bean> 

和豆:

 @Component class MyClass { @Resource(name="myProperties") private Properties myProperties; @PostConstruct public void init() { // do whatever you need with properties } } 

在configuration类中

 @Configuration @PropertySource("classpath:/com/myco/app.properties") public class AppConfig { @Autowired Environment env; @Bean public TestBean testBean() { TestBean testBean = new TestBean(); testBean.setName(env.getProperty("testbean.name")); return testBean; } } 

这是一个额外的答案,这也是帮助我了解它是如何工作的: http : //www.javacodegeeks.com/2013/07/spring-bean-and-propertyplaceholderconfigurer.html

任何BeanFactoryPostProcessor bean都必须用静态修饰符声明

 @Configuration @PropertySource("classpath:root/test.props") public class SampleConfig { @Value("${test.prop}") private String attr; @Bean public SampleService sampleService() { return new SampleService(attr); } @Bean public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } } 

有很多方法可以实现,下面是一些常用的方法,

  1. 使用PropertyPlaceholderConfigurer
  2. 使用PropertySource
  3. 使用ResourceBundleMessageSource
  4. 使用PropertiesFactoryBean

    还有很多……………………

假设ds.type是你的属性文件的关键。


使用PropertyPlaceholderConfigurer

注册PropertyPlaceholderConfigurer bean-

 <context:property-placeholder location="classpath:path/filename.properties"/> 

要么

 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath:path/filename.properties" ></property> </bean> 

要么

 @Configuration public class SampleConfig { @Bean public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); //set locations as well. } } 

注册PropertySourcesPlaceholderConfigurer ,现在您可以访问value-

 @Value("${ds.type}")private String attr; 

使用PropertySource

在最新的spring版本中,不需要使用@PropertySource注册PropertyPlaceHolderConfigurer ,我发现了一个理解版本可互换性的好链接 –

 @PropertySource("classpath:path/filename.properties") @Component public class BeanTester { @Autowired Environment environment; public void execute(){ String attr = this.environment.getProperty("ds.type"); } } 

使用ResourceBundleMessageSource

注册Bean-

 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>classpath:path/filename.properties</value> </list> </property> </bean> 

Access Value-

 ((ApplicationContext)context).getMessage("ds.type", null, null); 

要么

 @Component public class BeanTester { @Autowired MessageSource messageSource; public void execute(){ String attr = this.messageSource.getMessage("ds.type", null, null); } } 

使用PropertiesFactoryBean

注册Bean-

 <bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:path/filename.properties</value> </list> </property> </bean> 

Wire Properties实例导入你的class-

 @Component public class BeanTester { @Autowired Properties properties; public void execute(){ String attr = properties.getProperty("ds.type"); } } 

您需要在应用程序上下文中放置一个PropertyPlaceholderConfigurer bean并设置其位置属性。

在这里看到细节: http : //www.zparacha.com/how-to-read-properties-file-in-spring/

你可能不得不修改你的财产文件有点这个东西的工作。

希望它有帮助。

  [project structure]: http://i.stack.imgur.com/RAGX3.jpg ------------------------------- package beans; import java.util.Properties; import java.util.Set; public class PropertiesBeans { private Properties properties; public void setProperties(Properties properties) { this.properties = properties; } public void getProperty(){ Set keys = properties.keySet(); for (Object key : keys) { System.out.println(key+" : "+properties.getProperty(key.toString())); } } } ---------------------------- package beans; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ap = new ClassPathXmlApplicationContext("resource/spring.xml"); PropertiesBeans p = (PropertiesBeans)ap.getBean("p"); p.getProperty(); } } ---------------------------- - driver.properties Driver = com.mysql.jdbc.Driver url = jdbc:mysql://localhost:3306/test username = root password = root ---------------------------- <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <bean id="p" class="beans.PropertiesBeans"> <property name="properties"> <util:properties location="classpath:resource/driver.properties"/> </property> </bean> </beans>