Java 如何使用@Named注释从Spring3.0中的属性中注入构造函数参数?

Java 如何使用@Named注释从Spring3.0中的属性中注入构造函数参数?,java,spring,annotations,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

我很难把所有的东西放在一起:

<?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?
(对我来说)春天自然会死去:

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的示例可能会有所帮助。与往常一样,在上的文档非常有用

      要从属性文件中读取,请查找@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 in的示例

      @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"));
                  }
              }
          }
      
      }
      
      @组件
      公共类GetCurrentMetric{
      /**
      *@param args
      */
      公共静态void main(字符串[]args){
      ApplicationContext context=new ClassPathXmlApplicationContext(“classpath:spring/app config.xml”);
      GetCurrentMetric p=context.getBean(GetCurrentMetric.class);
      p、 启动(args);
      }
      @自动连线
      私人鞭子服务;
      私有void开始(字符串[]args){
      如果(参数长度!=2){
      System.out.println(“用法:GetCurrentMetric示例:GetCurrentMetric cr1.lax1 p9/2”);
      }否则{
      字符串设备=args[0];
      字符串iface=args[1];
      Map Map=service.getCurrentMetric(设备,iface);
      如果(map.size()==2){
      System.out.println(“级别:”+map.get(“级别”);
      System.out.println(“公制:”+map.get(“公制”);
      }
      }
      }
      }
      
      编辑:遗漏了一件重要的事情,对于顶部的属性文件示例,您需要在应用程序上下文文件中添加一些内容来将其绑定在一起。上述示例:

      <!-- define the properties file to use --> 
      <util:properties id="nccProperties" location="classpath:spring/ncc.properties" />
      
      
      
      答案太长。您可以指定他应该使用
      Value
      而不是
      Named
      +1无论如何。但要摆脱
      @ImportResource
      。它不是必需的,应该主要用于
      @Configuration
      类。它可以与@Value配合使用。我正在寻找一个使用@Named的解决方案。@Bozho我不同意长度,我总是发现示例很有用。谢谢你们对@ImportResource的评论,这一点很好。@binkley并不想争论,只是好奇,使用@Named作为替代品会带来什么价值?我希望尽可能接近JSR330,避免spring特定的注释。我们的目标是使代码在Spring和Guice上都可以运行,以便我公司的其他团队使用。
      <!-- define the properties file to use --> 
      <util:properties id="nccProperties" location="classpath:spring/ncc.properties" />