java spring qualifier_java – Spring构造函数@Autowired和@Qualifier失...

简化代码(我使用的是Spring 3.1.4).

这是我想要自动装配的课程:

public class MyCoolClass {

@Autowired

public MyCoolClass(

@Qualifier("CoolBean1") SomeOtherClass1 foo1,

@Qualifier("CoolBean2") SomeOtherClass1 foo2

) {

this.foo1 = foo1;

this.foo2 = foo2;

}

// ...

}

这是我的spring config xml:

以下是我试图获取bean的方法:

GenericApplicationContext ctx = new GenericApplicationContext();

XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);

xmlReader.loadBeanDefinitions(new ClassPathResource("config.xml"));

ctx.refresh();

在刷新调用中,我遇到了这个:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'GreatBean' defined in class path resource [config.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [mypackage.MyCoolClass]: No default constructor found; nested exception is java.lang.NoSuchMethodException: mypackage.MyCoolClass.()

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)

at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)

at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)

at mypackage.Runner.main(Runner.java:234)

Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [mypackage.MyCoolClass: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.amazon.maxis.security.gbac.AsynchronousFolderAuthorizationManager.()

at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990)

... 11 more

Caused by: java.lang.NoSuchMethodException: mypackage.MyCoolClass.()

at java.lang.Class.getConstructor0(Class.java:2800)

at java.lang.Class.getDeclaredConstructor(Class.java:2043)

at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78)

... 12 more

问题:

>为什么我需要默认的construtor?我不希望它被称为.

>出于某种原因,我是否需要“@Component”?我看到别人这样做了

>为什么这段代码通过我的junit测试工作(据我所知)? (我如何运行它们如下所示.

这是运行单元测试的代码段:

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations={"classpath:config.xml"})

public class MyJunitTest {

//...

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值