2010-12-15 49 views
2

我無法把他們放在一起:如何使用@Named註解從Spring 3.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" 
     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 
@Singleton 
public class MySpringMain { 
    @Inject 
    public MySpringMain(final AReallyCool component) { 
     component.runForAWhile(); 
    } 

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

組件是這樣的:

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

和屬性是:

whoAmI=Who is anyone, really? 

當然(對我來說)春死的死

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

一些Spring的具體例子可能會有所幫助。和往常一樣,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")); 
      } 
     } 
    } 

} 

編輯@Autowired主函數的例子:漏掉了一個重要的事情,對於性能在你需要的東西在你的應用程序中的頂級文件示例上下文文件將其綁定在一起。以上示例:

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

答案太長。你可以指定他應該使用'Value'而不是'Named'。無論如何。但要擺脫'@ ImportResource'。它不是必需的,並且應該主要用於'@ Configuration'類。 – Bozho 2010-12-16 06:43:44

+0

它與@Value正常工作。我正在尋找@Named的解決方案。 – binkley 2010-12-16 14:57:45

+0

@Bozho我對長度不以爲然,我總是發現有用的例子。感謝您與@ImportResource的評論,點好了。 – Bill 2010-12-16 15:30:08

相關問題