我正在将一个单独的bean的@Qualified集合注入到构造函数中。使用的许多bean都有一个@Autowired参数化构造函数,没有默认构造函数。但是,Spring似乎总是希望始终使用默认构造函数,从而导致NoSuchMethodException。这是Spring 3.2.9的限制吗?我在文档中找不到任何表明它无效的内容。
以下是已删除的编辑代码,它声明了一个由其公共接口发现的bean:
@IsPersistence
@Component("SomeComponent")
@Qualifier("category")
public class SomeComponent extends CommonBase implements CommonInterface {
@Autowired
public SomeComponent (
@Qualifier("SomeOtherComponent") SomeOtherComponent component) {
super(component);
}
}
使用此bean的编辑配置如下:
@Configuration
@ComponentScan(basePackages = {"some.package"}})
public class SomeConfiguration {
@Bean(name = "AnotherBean")
@Autowired
public AnotherBean anotherBean(
@Qualifier("category") final List<CommonInterface > objects) {
return new AnotherBean(objects);
}
}
在连接此类时,将生成以下堆栈跟踪:
2018-01-31 07:19:28,618 ERROR err:105 main - java.lang.NoSuchMethodException: some.package.SomeComponent .<init>()
2018-01-31 07:19:28,618 ERROR err:105 main - at java.lang.Class.getConstructor0(Class.java:2715)
2018-01-31 07:19:28,618 ERROR err:105 main - at java.lang.Class.getDeclaredConstructor(Class.java:1987)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1030)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:983)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:917)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:818)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:775)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:817)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:745)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:469)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1055)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:951)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
2018-01-31 07:19:28,618 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:917)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:860)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:775)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:817)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:745)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:469)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1055)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:951)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1121)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:674)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:120)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
2018-01-31 07:19:28,634 ERROR err:105 main - at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
2018-01-31 07:19:28,650 ERROR err:105 main - at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
2018-01-31 07:19:28,650 ERROR err:105 main - at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
2018-01-31 07:19:28,650 ERROR err:105 main - at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
2018-01-31 07:19:28,650 ERROR err:105 main - at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
2018-01-31 07:19:28,650 ERROR err:105 main - at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
2018-01-31 07:19:28,650 ERROR err:105 main - at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
2018-01-31 07:19:28,650 ERROR err:105 main - at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
2018-01-31 07:19:28,650 ERROR err:105 main - at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
2018-01-31 07:19:28,650 ERROR err:105 main - at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
2018-01-31 07:19:28,650 ERROR err:105 main - at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
2018-01-31 07:19:28,650 ERROR err:105 main - at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
2018-01-31 07:19:28,650 ERROR err:105 main - at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
2018-01-31 07:19:28,650 ERROR err:105 main - at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
2018-01-31 07:19:28,650 ERROR err:105 main - at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
2018-01-31 07:19:28,650 ERROR err:105 main - at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
2018-01-31 07:19:28,650 ERROR err:105 main - at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
2018-01-31 07:19:28,650 ERROR err:105 main - at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
2018-01-31 07:19:28,650 ERROR err:105 main - at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
2018-01-31 07:19:28,650 ERROR err:105 main - at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
2018-01-31 07:19:28,650 ERROR err:105 main - at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
2018-01-31 07:19:28,650 ERROR err:105 main - at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
2018-01-31 07:19:28,650 ERROR err:105 main - at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
2018-01-31 07:19:28,650 ERROR err:105 main - at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
至于为什么不使用当前版本,这是一个生产中的系统,其声誉很慢,采用较新版本的Spring。解决这个问题,但问题是立竿见影的,采用将花费更多的时间。
我通过在配置上以工厂方法编码bean构造来解决这个问题。但是,有很多这些bean,最好通过注释而不是直接在配置类上处理它。
答案 0 :(得分:2)
在@Autowired
类上使用@Configuration
时, Spring版本3.x 存在一些限制。查看docs
。
@Configuration类必须具有default / no-arg构造函数,并且不能使用@Autowired构造函数参数。
除了@Autowired
之外,注释只能使用Spring v4.3
及更高版本的版本应用于类构造函数。
事实上,如果只定义了一个构造函数,它甚至不是必需的。
从Spring Framework 4.3开始,如果目标bean只定义了一个开头的构造函数,则不再需要在这样的构造函数上使用@Autowired注释。但是,如果有几个构造函数可用,则必须注释至少一个构造函数,以便教导容器使用哪个。
因此,您的代码的问题是旧的Spring版本,它不会让@Autowired
对类结构符号感兴趣。
希望这有帮助。