Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
此处不允许元素:用于IntelliJ中SpringBean定义中的限定符_Spring_Intellij Idea_Javabeans_Autowired - Fatal编程技术网

此处不允许元素:用于IntelliJ中SpringBean定义中的限定符

此处不允许元素:用于IntelliJ中SpringBean定义中的限定符,spring,intellij-idea,javabeans,autowired,Spring,Intellij Idea,Javabeans,Autowired,这是文件,我正在尝试以下示例: 这是加载xml文件的测试栏 import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestFooBar { public static void main(String[] args) throws InterruptedException { ClassPathXmlApplicationContext applicationC

这是文件,我正在尝试以下示例:

这是加载xml文件的测试栏

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestFooBar {

public static void main(String[] args) throws InterruptedException {
    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
            "test2.xml");
    Bar bar = applicationContext.getBean("bar", Bar.class);
    bar.printFooName();
    System.out.println(bar.toString());
    /*
     * if a single definition of a class type exists, then u can get the
     * instance by this way also. No need to specify Id
     */
    Foo foo = applicationContext.getBean(Foo.class);
    System.out.println(foo.getName());
}

}
运行应用程序时,堆栈跟踪如下所示:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bar': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private annotation.Foo annotation.Bar.foo; nested exception is     org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [annotation.Foo] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=anotherFoo)}
at   org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at annotation.TestFooBar.main(TestFooBar.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private annotation.Foo annotation.Bar.foo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [annotation.Foo] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=anotherFoo)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
... 18 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [annotation.Foo] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=anotherFoo)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:920)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:789)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
... 20 more
线程“main”org.springframework.beans.factory.BeanCreationException中的异常:创建名为“bar”的bean时出错:自动连线依赖项的注入失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:无法自动关联字段:private annotation.Foo annotation.Bar.Foo;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项类型为[annotation.Foo]的匹配bean:至少需要1个符合此依赖项autowire候选项条件的bean。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true),@org.springframework.beans.factory.annotation.Qualifier(value=anotherFoo)} 位于org.springframework.beans.factory.annotation.AutoWiredNotationBeanPostProcessor.postProcessPropertyValues(AutoWiredNotationBeanPostProcessor.java:285) 位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074) 位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) 位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 位于org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) 位于org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 位于org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) 位于org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) 位于org.springframework.beans.factory.support.DefaultListableBeanFactory.PreInstanceSingleton(DefaultListableBeanFactory.java:580) 位于org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) 位于org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) 位于org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139) 位于org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83) main(TestFooBar.java:20) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中 位于java.lang.reflect.Method.invoke(Method.java:606) 位于com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) 原因:org.springframework.beans.factory.BeanCreationException:无法自动关联字段:private annotation.Foo annotation.Bar.Foo;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项类型为[annotation.Foo]的匹配bean:至少需要1个符合此依赖项autowire候选项条件的bean。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true),@org.springframework.beans.factory.annotation.Qualifier(value=anotherFoo)} 位于org.springframework.beans.factory.annotation.AutoWiredNotationBeanPostProcessor$AutoWiredFeldElement.inject(AutoWiredNotationBeanPostProcessor.java:502) 位于org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) 位于org.springframework.beans.factory.annotation.AutoWiredNotationBeanPostProcessor.postProcessPropertyValues(AutoWiredNotationBeanPostProcessor.java:282) ... 还有18个 原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖项类型为[annotation.Foo]的匹配bean:至少需要1个符合此依赖项autowire候选项条件的bean。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true),@org.springframework.beans.factory.annotation.Qualifier(value=anotherFoo)} 位于org.springframework.beans.factory.support.DefaultListableBeanFactory.RaiseNouchBeanDefinitionException(DefaultListableBeanFactory.java:920) 位于org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:789) 位于org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703) 位于org.springframework.beans.factory.annotation.AutoWiredNotationBeanPostProcessor$AutoWiredFeldElement.inject(AutoWiredNotationBeanPostProcessor.java:474) ... 20多
谢谢您提供的额外详细信息,您想做什么还不清楚

@Qualifier
注释的使用方式与
@Resource
不同。指定元数据
元素时,将覆盖解析
@限定符时使用的标识符。在这种情况下,
标识符
“secondaryFoo”
将替换
标识符
“anotherFoo”
。如果您将您的声明更改为

@Autowired
@Qualifier(value = "secondaryFoo")
private Foo foo;
它会自动连接正确。但是,您仍然可以使用

Foo anotherFoo = (Foo) applicationContext.getBean("anotherFoo");
要获取具有
id的
“anotherFoo”
。虽然这会失败

Foo anotherFoo = (Foo) applicationContext.getBean("secondaryFoo");
如果您正在寻找如何基于唯一id自动连线,则应使用
@Resource
。各国:

如果您打算用名称表示注释驱动的注入,请不要 主要使用@Autowired,即使在技术上能够引用 通过@Qualifier值创建一个bean名称。相反,使用JSR-250 @决议
Foo anotherFoo = (Foo) applicationContext.getBean("anotherFoo");
Foo anotherFoo = (Foo) applicationContext.getBean("secondaryFoo");