如何从属性文件读取值?

问题描述:

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

some.properties ---file name. values are below. 

abc = abc 
def = dsd 
ghi = weds 
jil = sdd 

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

+6

这看起来并不像一个[属性](HTTP ://en.wikipedia.org/wiki/.propertie s)文件。 – Raghuram 2012-02-13 11:40:37

+0

如果它是Java中的属性文件 - 是的。否则,它是一种自定义文件格式,需要对待不同的处理方式(如果没有密钥,则不能在Spring中将这些行用作属性值)。 – 2012-02-13 11:50:55

+2

“不以传统方式” - 这是什么意思? – 2012-02-13 11:58:46

配置PropertyPlaceholder在上下文:

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

然后,你指的是性能在豆类:

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

编辑:更新的代码复式逗号分隔值解析属性:

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> 

和bean:

@Component 
class MyClass { 
    @Resource(name="myProperties") 
    private Properties myProperties; 

    @PostConstruct 
    public void init() { 
    // do whatever you need with properties 
    } 
} 
+0

嗨mrembisz,感谢您的回复。我已经配置了propert-placeholder以从外部属性文件中读取值。但我有一个属性文件内的资源文件夹。我需要阅读和注射。我需要将所有值注入列表。谢谢! – user1016403 2012-02-13 12:40:23

+0

@ user1016403更新了我的回答 – mrembisz 2012-02-13 12:56:18

+0

按照@Ethan的建议编辑。感谢您的更新,无法接受原创编辑,已经太晚了。 – mrembisz 2013-03-26 16:15:15

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

看详情点击这里:http://www.zparacha.com/how-to-read-properties-file-in-spring/

您可能需要修改您的属性文件有点为这件事的工作。

希望它有帮助。

在配置类

@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; 
    } 
} 
+0

在这个例子中,你是否会简单地在生产测试中使用不同的'app.properties'?换句话说,部署过程的一部分是用生产值替换'app.properties'吗? – 2015-07-15 16:54:12

+0

@KevinMeredith是的,你可以,只需通过配置文件注释拆分弹簧配置http://*.com/questions/12691812/can-propertysources-be-chosen-by-spring-profile#answer-14167357 – mokshino 2015-07-27 13:56:38

+0

@KevinMeredith我们使用外部部署战争之外的文件夹:如c:\ apps \ sys_name \ conf \ app.properties。部署过程变得简单并且不易出错。 – jpfreire 2016-02-12 21:18:18

这是一个额外的回答也是有很大的帮助,我听不懂如何加工:http://www.javacodegeeks.com/2013/07/spring-bean-and-propertyplaceholderconfigurer.html

种任何的BeanFactoryPostProcessor豆已经到了静态声明,修改

@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(); 
} 
} 
+0

没有必要使用@ @ PropertySource来显式注册'PropertySourcesPlaceholderConfigurer' Bean – 2017-01-26 21:51:32

+0

@ dubey -Harcourtians您使用哪种Spring(核心)版本?如果你使用Spring Boot,你甚至不需要'@ PropertySource'。 – 2017-01-27 12:05:41

[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> 
+0

添加一些解释 – HaveNoDisplayName 2016-08-26 07:21:48

+0

使用核心容器您无法访问外部资源属性文件,因此您需要使用像ApplicationContext这样的j2ee容器,并且您需要使用xmlns,xmlns:util,xsi:schemaLocation,xmlns:xsi等bean级别验证 – 2016-08-26 10:19:13

有多种方法来实现相同的,下面是弹簧

    一些常用方法
  1. 使用PropertyPlaceholderConfigurer
  2. 使用Pro pertySource
  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("${ds.type}")private String attr; 

使用PropertySource

在最新版本的春天无需注册PropertyPlaceHolderConfigurer@PropertySource,我找到了一个好link了解版本comaptibility-

@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> 

访问价值 -

((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> 

线性能的情况下进入你接收机类

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