【问题标题】:Expose Properties file to Java via Spring [duplicate]通过 Spring 将属性文件公开给 Java [重复]
【发布时间】:2013-12-21 03:14:51
【问题描述】:

我正在尝试使用 Spring 来允许我的 Java 类访问属性文件。我已经做了很多谷歌搜索,似乎有几种方法可以做到这一点。我尝试使用其中两种不同的方法,但都失败了。

尝试 1
XML

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

Java

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"/>

Java

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

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

在这两种情况下,调用“getProperty”方法时都会出现空指针异常。我是 Spring 新手,我猜我错过了一些简单的东西。除了让这些尝试发挥作用之外,我还想知道使用 Spring 公开属性文件的“最佳”方式是什么。
提前感谢您的帮助。

【问题讨论】:

  • Spring 不会自动装配静态成员。 Spring 根本不参与你正在做的事情。
  • 显然我走错了路。可以举个例子吗?
  • 注解只是元数据。他们自己什么都不做。您必须显式声明并实例化一个 ApplicationContext 实现类。
  • @Joe 用例一样,只是替换bean类。
  • @Joe 正确,PropertySourcesPlaceholderConfigurer 是 Spring 3.1 的首选方式。但是,我的答案的其余部分仍然适用,因此您可以复制我的答案的相关部分,并将 bean 声明中的 PropertyPlaceholderConfigurer 替换为相应的完全限定名称 PropertySourcesPlaceholderConfigurer

标签: java spring


【解决方案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);
    }
}

【讨论】:

  • 这正是我想要的。我见过很多获取属性的方法。尽管这是我一直在寻找的,但这是否是将来继续使用的好方法。还是其他方法被认为是“最佳实践”?
  • 使用这种方法,您并没有真正使用 Spring。如果您已经在使用 Spring,或者如果您希望属性文件的位置发生更改,则此方法可能很有用。另一种方法的优点是 1) Spring 将为您解析和验证属性(例如整数属性)。 2) 一眼就能知道系统使用了哪些属性。 3) 单元测试要容易得多,因为您不必创建虚假的属性文件。 4) 你的类甚至不知道他们正在使用属性文件并且可以专注于逻辑。
【解决方案2】:

==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);
  }
}

【讨论】:

  • 这是假设 config.properties 和 applicationContext.xml 都在类路径的根目录下。另外,我手边没有 IDE,所以这只是徒手画的,但它应该给出要点。
  • 请补充说明。
  • 谢谢你的例子,我看到了一些我缺少的东西。我要避免的是必须为我放在属性文件中的每个属性添加更多代码。如果我添加“database.username”我不想在java中创建一个成员变量并用xml单独分配它。
猜你喜欢
  • 1970-01-01
  • 2018-11-21
  • 1970-01-01
  • 2011-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
相关资源
最近更新 更多