- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 @Autowired()
和 @Qualifer()
来测试依赖项的自动注入(inject)。我创建了 Engine 类、Car 类。引擎类包含两个对象 e1 和 e2。当我将 @Qualifier
与值“e2”一起使用时,它会生成错误消息:
WARNING: Exception encountered during context initialization -
cancelling refresh attempt:
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'c': Injection of autowired dependencies failed; nested
exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private com.abc.xyz.Engine
com.abc.xyz.Car.engine; nested exception is
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
qualifying bean of type [com.abc.xyz.Engine] is defined: expected single
matching bean but found 2: e1,e2
Exception in thread "main"
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'c': Injection of autowired dependencies failed; nested
exception is org.springframework.beans.factory.BeanCreationException: Could
not autowire field: private com.abc.xyz.Engine com.abc.xyz.Car.engine;
nested exception is
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
qualifying bean of type [com.abc.xyz.Engine] is defined: expected single
matching bean but found 2: e1,e2
at
org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor
.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.
bstractAutowireCapableBeanFactory.populateBean
(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.
AbstractAutowireCapableBeanFactory.doCreateBean
(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.
AbstractAutowireCapableBeanFactory.createBean
(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.
AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.
DefaultSingletonBeanRegistry.getSingleton
(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.
AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.
AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.
DefaultListableBeanFactory.preInstantiateSingletons
(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.
AbstractApplicationContext.finishBeanFactoryInitialization
(AbstractApplicationContext.java:839)
at org.springframework.context.support.
AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.context.support.
ClassPathXmlApplicationContext.<init>
(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.
ClassPathXmlApplicationContext.<init>
(ClassPathXmlApplicationContext.java:83)
at com.abc.xyz.ClientApp.main(ClientApp.java:9)
Caused by: org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private com.abc.xyz.Engine
com.abc.xyz.Car.engine; nested exception is
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
qualifying bean of type [com.abc.xyz.Engine] is defined: expected single
matching bean but found 2: e1,e2
at org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject
(AutowiredAnnotationBeanPostProcessor.java:573)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject
(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues
(AutowiredAnnotationBeanPostProcessor.java:331)
... 13 more
Caused by: org.springframework.beans.factory.
NoUniqueBeanDefinitionException: No qualifying bean of type
[com.abc.xyz.Engine] is defined: expected single matching
bean but found 2: e1,e2
at
org.springframework.beans.factory.
support.DefaultListableBeanFactory.doResolveDependency
(DefaultListableBeanFactory.java:1126)
at org.springframework.beans.factory.
support.DefaultListableBeanFactory.resolveDependency
(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.
annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.
inject(AutowiredAnnotationBeanPostProcessor.java:545)
... 15 more
上面的代码是:
引擎.java:
package com.abc.xyz;
public class Engine {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
汽车.java:
package com.abc.xyz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class Car {
@Qualifier(value="e1")
@Autowired()
private Engine engine;
public void display()
{
System.out.println("Car Engine="+engine.getName());
}
}
spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<beanclass="org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor">
</bean>
<bean id="e1" class="com.abc.xyz.Engine">
<property name="name" value="2015"></property>
</bean>
<bean id="e2" class="com.abc.xyz.Engine">
<property name="name" value="2016"></property>
</bean>
<bean id="c" class="com.abc.xyz.Car">
</bean>
</beans>
ClientApp.java:
package com.abc.xyz;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ClientApp {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext
("spring.xml");
Car c=(Car)context.getBean("c");
c.display();
}
}
最佳答案
您必须添加<context:annotation-config/>
到你的 spring.xml。这会注册必要的 bean 来处理注释,并且 @Qualifier
其中。
这就是添加命名空间后的样子:
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<context:annotation-config/>
<bean id="e1" class="com.abc.xyz.Engine">
<property name="name" value="2015"/>
</bean>
<bean id="e2" class="com.abc.xyz.Engine">
<property name="name" value="2016"/>
</bean>
<bean id="c" class="com.abc.xyz.Car"/>
</beans>
关于java - @Qualifier() 和 @Autowired() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38684278/
根据这个answer ,下面的代码应该没有错误地编译: #include namespace { struct A { int i; }; volatile A a{}; static_assert
我了解导入 Data.List。 但是 importqualified Data.List 语句中的 qualified 是什么意思? 最佳答案 合格的导入使导入的实体仅以合格的形式可用,例如 imp
我正在尝试创建一个组件,该组件将接受一个特定的 bean (fasterxml ObjectMapper)。 如果有一个名为 qualifiedObjectMapper 的限定 bean,我想使用那个
我一直在编写以下代码来支持对右值的函数调用,而不必 std::move明确地在返回值上。 struct X { X& do_something() & { // some co
我正在尝试使用 GCC 编译器编译以下代码 class Class { public: uInt16 temp; const uInt32 function() const; } inline
我试图理解为什么我的编译器警告我有关此代码: static const char *const _menuMain_Strings_1[] __ATTR_PROGMEM__ = { _menuMai
我正在尝试将 Spring 4.x.x 迁移到 Spring boot,它依赖于外部 spring 2.5 jar 中的类。我已经进行了所有 Autowiring 更改,下面是我的应用程序类 @Spr
我从 eclipse git 中查看了最新的源代码:git://git.eclipse.org/gitroot/platform/eclipse.platform.releng.aggregator.
我发现许多门户网站都解释了类似的问题。但我想这是独特的情况。我在 spring mvc 应用程序中遇到错误。 org.springframework.beans.factory.Unsatisfied
我有下面的代码: package far.botshop.backend.controller; /** */ import java.util.logging.Logger; import far
我有 3 个数据源,设置如下: @Configuration @Component public class DataSourceConfig { @Bean("foo") @Conf
如何添加限定符来区分这两个 bean?我知道我需要使用 @Qualifier 注释,但我不确定如何将它添加到 bean 中,然后如何引用适当的 bean 创建 Autowiring 对象。 @Conf
@Override @Autowired(required = true) @Qualifier("hibernateCriteriaBuilder") public void setCriteria
我有 Circle 类: public class Circle { @Autowired @Qualifier("pointA") private Point center;
我在 Java Spring 环境中工作,并且在让 @Qualifier 工作时遇到了问题。我们项目的其他部分正在使用 @Inject 获取一个 bean,但我需要同一个 bean 的两个版本,看起来
“qualifier”的含义是什么?“qualifier”和“keyword”之间的区别是什么? 对于C语言中的volatile限定词,我们可以说volatile是一个关键字,那么“qualifier
我有一个简单的宏来设置一些常见的标题和格式: Dim colString(1 To 17, 1 To 2) As String Dim i As Integer colString(1, 1) = "
我正在将 Teradata 转换为 Hive(版本 0.10.0)。 Teradata 查询: QUALIFY ROW_NUMBER() OVER (PARTITION BY ADJSTMNT,SRC
我是在 PHP 中使用命名空间的新手,它们看起来很简单。然而,当我在一个包含类、接口(interface)和闭包的大文件的顶部添加一个命名空间时,代码完全停止工作。显然,某些元素没有正确限定。 例如:
我正在尝试使用属性占位符作为@Qualifier 的属性,如下所示: @Autowired @Qualifier("${beanName}") private MyBean myBean; 但是,这不
我是一名优秀的程序员,十分优秀!