簡體   English   中英

如何使用@Named批注從Spring 3.0的屬性中注入構造函數參數?

[英]How to inject constructor parameters from properties in Spring 3.0 with the @Named annotation?

我很難將所有內容放在一起:

<?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的注釋。
  • 您將如何進行這項工作?

一些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;
    }

這是@Autowired中主要功能的示例

@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"));
            }
        }
    }

}

編輯:錯過了一件重要的事情,對於頂部的屬性文件示例,您將需要在應用程序上下文文件中將它們綁在一起。 以上示例:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM