如何使用@Named注释从Spring 3.0中的属性注入构造函数参数?

时间:2010-12-15 23:56:51

标签: java spring annotations

我无法将它们放在一起:

<?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的注释。
  • 你会如何做到这一点?

1 个答案:

答案 0 :(得分: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;
    }

以下是

中主要功能@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" />