0

首先,如果这个问题的标题不正确,我深表歉意。

目标:我正在使用maven打包一个需要读取外部属性文件的jar。jar 将被使用 tomcat7 部署的战争使用。在 tomcat 中,setenv.sh 如下所示的属性文件 (PROPERTY_MANAGER) 的位置是从 jar 将读取属性的位置设置的。我怎样才能做到这一点?

我正在使用 Apache-tomcat-7.0.32,我的 setenv.sh内容如下:

export JAVA_OPTS="-DPROPERTY_MANAGER=$CATALINA_HOME/myPropertiesDir -DCOMMON_PROPERTIES=$CATALINA_HOME/myCommonPropsDir -DAPP_ENCRYPTION_PASSWORD=pass $JAVA_OPTS"

在 Eclipse 中,我导入了一个现有的 Maven 模块。此时我的模块是一个简单的 java 类(因此我可以测试如何通过 tomcat 读取属性)。这是我的课:

Class MyClass{
 private String var1;
 private String var2;
 public MyClass(){}
 public String getVar1() { return var1; }
 public String getVar2() { return var2; }
 public String setVar1(String a) { var1 = a; }
 public String setVar2(String a) { var2 = b; }
 public static void main (String []arg){
   MyClass c = new MyClass();
   System.out.println(“var1 = “ + c.getVar1() +”, var2 = “+c.getVar2());
 }
}// end of class

这是src/main/resources 下的myprops.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:si=http://www.springframework.org/schema/integration 
       xmlns:jms=http://www.springframework.org/schema/integration/jms
       xmlns:context=http://www.springframework.org/schema/context
       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.1.xsd
               http://www.springframework.org/schema/context              
               http://www.springframework.org/schema/context/spring-context-3.1.xsd
               http://www.springframework.org/schema/util  
               http://www.springframework.org/schema/util/spring-util-3.1.xsd">                     

      <context:property-placeholder location="file:${PROPERTY_MANAGER}/myclass-props.properties" ignore-unresolvable="true"/> 
      <!-- MyClass bean -->
      <bean id="mybean" class="my.com.test.MyClass">
        <property name="var1" value="${prop.var1}" />
        <property name="var2" value="${prop.var2}" />
     </bean>
 </beans>

myclass-props位于 $CATALINA_HOME/myCommonPropsDir 下,如下所示:

prop.var1=abc
prop.var2=123

上面的类被构建为一个 maven 模块并打包为一个 jar。为了测试这一点,我启动了 tomcat,然后在 Eclipse 中尝试将主类作为 Java 应用程序运行并将其作为远程 Java 应用程序进行调试。远程的什么都不做。作为 java 应用程序运行它会给我以下输出: var1 = null, var2 = null;

显然,没有拾取属性文件。我在这里做错了什么?我确信它是我想念的小东西。提前感谢所有建议。

4

2 回答 2

0

桑托什是对的。没有什么能提出你的春天背景。

当您从 Eclipse 将其作为 java 应用程序运行时,main方法代码将被简单地执行。在main您创建实例的方法中MyClass- 这不是您创建 spring bean 的方式。Spring 不会向使用new关键字创建的类注入属性或 bean 引用。此外,当您运行 Tomcat 时,Eclipse 不会在 Tomcat 上部署您的 jar。

尝试使用包含 bean 的 spring 上下文创建一个 Web 应用程序,该 bean 将使用您的属性读取模块(如您在问题中描述的那样),然后测试它是否有效。不要使用new关键字来创建 的实例MyClass,而是注入在您的上下文中声明的 bean。您可以在您的main方法中创建上下文,但它不会在应用程序服务器上运行。

我建议稍微阅读一下Spring Framework 的文档(尤其是关于 bean 实例化和依赖注入的部分)。

于 2013-07-12T19:08:07.043 回答
0

我猜你的问题是${PROPERTY_MANAGER}没有被 sysprop 替换(maven sysprop 替换与 spring 完全不同)。如果您的目标是让 jar 读取外部属性文件而不对位置进行硬编码,请改用类路径:

<context:property-placeholder location="classpath:/myclass-props.properties" ignore-unresolvable="true"/>

然后您可以将文件放在 $TOMCAT_HOME/lib 或包含在您的类路径中的任何其他文件夹中

于 2013-07-12T15:47:00.193 回答