2

Spring 3.1.1を使用していくつかのフィールドにプロパティ値を入力したいのですが、フィールドは常にnullのままです。

applicationContext.xmlに追加されました

  http://www.springframework.org/schema/util 
  http://www.springframework.org/schema/util/spring-util-3.0.xsd">

  <util:properties id="props" location="classpath:application.properties" />

application.properties:

myProp=value

javaクラス:

  @Value("#{props[myProp]}")
  private String myField;

ただし、Beanの作成時に、myFieldはプロパティファイルの「値」で埋められませんが、nullのままです。

また試してみました(成功しませんでした):

  @Value("#{props.myProp}")
  private String myField;

  @Value("#{myProp}")
  private String myField;

移動した後に「ファイルが見つかりません」というメッセージが表示されたため、application.propertiesファイルが見つかりました。

ここにスタックトレースがあります:http://pastebin.com/5A8i5gF8

何を変更する必要がありますか?

4

5 に答える 5

0

新しい回答を投稿して申し訳ありませんが、コメントではコードが正しくフォーマットされていません。

@Repository
@Qualifier(value = "myStrategy")
public class MyClass implements MyInterface {

    @Value("${prop1}")
    private String prop1;

    MyClass(){
        <prop1 is passed to a method to set a config.>
    }
于 2012-08-21T13:43:47.340 に答える
0

myField クラス変数を持つクラスを applicationContext.xml に宣言または構成しましたか?

于 2012-08-21T11:14:40.560 に答える
0

# の代わりに char $ を試してみてください:

 @Value("${myProp}")
  private String myField;
于 2012-08-21T11:17:38.800 に答える
0

欠落している<context:component-scan/><context:annotation-config/>、アプリケーションコンテキストにある可能性があります-AutowiredAnnotationBeanPostProcessor実際に @Value タグを処理する呼び出された Bean を登録します。そこにない場合は、2 つのうちの 1 つを追加するだけで機能します。

于 2012-08-21T12:09:46.960 に答える
0
<?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:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://cxf.apache.org/core
      http://cxf.apache.org/schemas/core.xsd
      http://cxf.apache.org/jaxws
      http://cxf.apache.org/schemas/jaxws.xsd
      http://www.springframework.org/schema/util 
      http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <context:annotation-config />   
    <context:component-scan base-package="my.base.package" /> 
    <util:properties id="props" location="classpath:application.properties" />


    ... <some beans (transactions, persistence, ws-endpoints) not used by the test class > ....

        <cxf:bus>
        <cxf:features>
            <cxf:logging />
        </cxf:features>
    </cxf:bus>

</beans>
于 2012-08-21T13:08:07.760 に答える