2010-12-15 6 views
2

私はすべて一緒にそれを置くのトラブルを抱えている:Spring 3.0のプロパティから@Namedアノテーションを使用してコンストラクタパラメータを挿入する方法は?

<?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" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd"> 
    <context:property-placeholder location="classpath:some-useful.properties"/> 
    <context:component-scan base-package="scan.me.scotty"/> 
</beans> 

メインはこれです:

@Named 
public class AReallyCool { 
    @Inject 
    public AReallyCool(@Named("whoAmI") final String whoAmI) { 
     // do something here 
    } 
} 

とプロパティは次のとおりです:

whoAmI=Who is anyone, really? 

@Named 
@Singleton 
public class MySpringMain { 
    @Inject 
    public MySpringMain(final AReallyCool component) { 
     component.runForAWhile(); 
    } 

    public static void main(final String... args) { 
     new ClassPathXmlApplicationContext(args); 
    } 
} 

コンポーネントがこれです

当然のことながら(私にとっては)春は死を死ぬ:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Named(value=whoAmI)} 

質問:

  • これも、合理的なアプローチですか?私はSpring固有の注釈を避けようとしています。
  • この作品をどのように作成しますか?

答えて

4

いくつかの春の具体例が役立ちます。いつものように、http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlの文書は非常に便利です。

プロパティファイルから読み取るには、@ Valueアノテーションを探します。プロパティは、あなたのアプリケーションで何かをする必要があります上部にある例を提出するために、逃した一つの重要な事柄:例:ここでは

@Component 
@Scope("prototype") 
@ImportResource("classpath:spring/app-config.xml") 
public class RancidService { 

    private String filepath; 
    private String filename; 

    /** 
    * Default constructor 
    * 
    * @param pathname 
    */ 
    @Autowired 
    public RancidService(@Value("#{ nccProperties['rancid.path']}") String filepath) { 

     this.filepath = filepath; 
    } 

@Component 
public class GetCurrentMetric { 


    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 

     ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/app-config.xml"); 

     GetCurrentMetric p = context.getBean(GetCurrentMetric.class); 
     p.start(args); 

    } 

    @Autowired 
    private WhipService service; 
    private void start(String[] args) { 

     if (args.length != 2) { 
      System.out.println("Usage: GetCurrentMetric <device> <interface> Example: GetCurrentMetric cr1.lax1 p9/2"); 
     } else { 
      String device = args[0]; 
      String iface = args[1]; 

      Map<String, String> map = service.getCurrentMetric(device, iface); 

      if (map.size() == 2) { 
       System.out.println("Level: " + map.get("level")); 
       System.out.println("Metric: " + map.get("metric")); 
      } 
     } 
    } 

} 

EDITに@Autowired主な機能の一例ですコンテキストファイルを結合します。上記の例:

<!-- define the properties file to use --> 
<util:properties id="nccProperties" location="classpath:spring/ncc.properties" /> 
+0

答えが長すぎます。 'Named'の代わりに' Value'を使うべきだと指定することができます。とにかく+1。しかし、 '@ ImportResource'を取り除いてください。これは必要ではなく、主に '@Configuration'クラスで使用されるはずです。 – Bozho

+0

@Valueでうまく動作します。 @Namedで解決策を探しています。 – binkley

+0

@Bozho私は長さについて同意しない、私は常に有用な例を見つけた。 @ImportResourceに関するコメントをいただき、ありがとうございます。 – Bill

関連する問題