通过Spring将属性文件暴露给Java

时间:2013-12-04 15:00:12

标签: java spring

我正在尝试使用Spring来允许我的Java类访问属性文件。我做了很多googleing,似乎有几种方法可以做到这一点。我试图使用两种不同的方法,但它们都失败了。

尝试1
XML

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="location" value=classpath:config.properties />
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>

爪哇

public class App
{
    @Autowired
    private static Environment env;

    public static void main(String[] args)
    {
        System.out.println(env.getProperty("DatabaseName"));
    }
}

尝试2
XML

<util:properties id="myProperties" location="classpath:config.properties"/>

爪哇

public class App
{
    @Resource(name="myProperties")
    private static Properties myProperties;

    public static void main(String[] args)
    {
        System.out.println(myProperties.getProperty("DatabaseName"));
    }
}

在这两种情况下,当调用“getProperty”方法时,我得到一个Null Pointer Exception。我是Spring的新手,我猜我错过了一些简单的东西。除了让这些尝试工作之外,我想知道用Spring公开属性文件的“最佳”方法是什么。
提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

== applicationContext.xml的==

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="location" value=classpath:config.properties />
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>

<bean class="my.pckg.App">
  <property name="databaseName" value="${database.name}"/>
</bean>

== config.properties ==

database.name=blah

== my.pckg.App ==

public class App {

  private String databaseName;

  public void setDatabaseName(String databaseName) {
    this.databaseName = databaseName;
  }

  public String toString() {
    return "App (databaseName=" + databaseName + ")";
  }

}

== my.pckg.Main ==

public class Main {
  public static void main(String [] args) {
    ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    App app = appContext.getBean(App.class);
    System.out.println(app);
  }
}

答案 1 :(得分:1)

这是你做的另一种方式,这似乎是你想要的。

== applicationContext.xml的==

<?xml version="1.0" encoding="UTF-8"?>
<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-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">

    <util:properties id="myProperties" location="classpath:config.properties"/>

    <bean class="my.pckg.App">
        <property name="appProperties" ref="myProperties" />
    </bean>
</beans>

== config.properties ==

database.name=blah

== my.pckg.App ==

package my.pckg;

import java.util.Properties;

public class App {

      private Properties appProperties;

      public void setAppProperties(Properties appProperties) {
          this.appProperties = appProperties;
      }

      public String toString() {
        return "App (databaseName=" + appProperties.getProperty("database.name") + ")";
      }

}

== my.pckg.Main ==

package my.pckg;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        App app = appContext.getBean(App.class);
        System.out.println(app);
    }
}
相关问题