SpEL java.util.Properties不起作用。 Maven Tomcat 8

时间:2015-01-06 07:51:14

标签: java spring maven tomcat

我尝试在我的applicationContext.xml中使用spEL和java.util.Properties。否则我使用maven tomcat插件。

我的班级:

public class FieldClass{
 private Properties pr;
 public Properties getProperties(){
  return this.pr;
 }
 public void setProperties(Properties properties){
  this.pr = properties;
 }  
}

我的xml:

<bean id="field" class="com.myproject.app.FieldClass">
        <property name="pr" value="#{systemProperties['APP.LOG_PATH']}" />
    </bean>

然后我在命令行中运行mvn -DAPP.LOG_PATH=/Users/wsaryada/tmp tomcat7:redeploy。但我编译失败了。

我也尝试用这种方式在pom.xml中指定属性:

      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <configuration>
          <url>http://localhost:8080/manager/text</url>
          <server>tomcatserver</server>
          <systemProperties>
            <APP.LOG_PATH>/home/user/tmp</APP.LOG_PATH>
          </systemProperties>
        </configuration>
      </plugin>

但它也不起作用。 我也试过用<property name="pr" value="#{systemEnvironment['APP.LOG_PATH']}" /> 但这里有同样的问题

  

失败 - 在上下文路径/应用程序但上下文中部署应用程序   无法启动

问题出在哪里???

我评论bean所有工作成功后

更新

我在类中更改了Spring而不是属性,并且部署错误消失了,但pr的值变为空

1 个答案:

答案 0 :(得分:0)

Java中的Properties对象本质上是键 - 值对的集合。

在spring中,当您注入属性时,在xml文件中引用其键,并将值注入代码中。因此,java代码中的对象类型应该反映值的数据类型(string,int等等)。

此外,当您使用Spring注入属性时,有一些规则: 1)xml中属性的名称应与java代码中属性的名称相同。 2)您的java属性应该存在Getter和Setter。 3)Getters和Setters的名称应该与您的属性具有相同的名称,但是get或set添加到前面,例如getPr(),setPr()。

让我知道这是否有帮助。