gpt4 book ai didi

spring - 使用 Spring 注解 @Autowired 的构造函数注入(inject)不起作用

转载 作者:行者123 更新时间:2023-12-01 18:16:25 24 4
gpt4 key购买 nike

我创建了 2 个简单的类。一个类的构造函数被注释为@Autowired。它接受另一个类的对象。但这段代码失败了。

类(class):-1)SimpleBean.java

@Configuration
public class SimpleBean {
InnerBean prop1;

public InnerBean getProp1() {
return prop1;
}

public void setProp1( InnerBean prop1) {
System.out.println("inside setProp1 input inner's property is "
+ prop1.getSimpleProp1());
this.prop1 = prop1;
}

@Autowired(required=true)
public SimpleBean(InnerBean prop1) {
super();
System.out.println("inside SimpleBean constructor inner's property is "
+ prop1.getSimpleProp1());
this.prop1 = prop1;
}
}

2) 内部.java

@Configuration
public class InnerBean {
String simpleProp1;

public String getSimpleProp1() {
return simpleProp1;
}

public void setSimpleProp1(String simpleProp1) {
this.simpleProp1 = simpleProp1;
}

}

当我尝试加载 ApplicationConext 时

ApplicationContext acnxt = new AnnotationConfigApplicationContext("com.domain");

它给出以下错误:-

Exception in thread "main" org.springframework.beans.factory.BeanCreationException:         Error creating bean with name 'simpleBean' defined in file [C:\Users\owner\Documents\Java Project\MyWorkSpace\springMVCSecond\WebContent\WEB-INF\classes\com\domain\SimpleBean.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.domain.SimpleBean$$EnhancerByCGLIB$$4bc418be]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.domain.SimpleBean$$EnhancerByCGLIB$$4bc418be.<init>()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
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:293)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:75)
at com.test.SpringAnnotationTest.main(SpringAnnotationTest.java:12)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.domain.SimpleBean$$EnhancerByCGLIB$$4bc418be]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.domain.SimpleBean$$EnhancerByCGLIB$$4bc418be.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:70)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:958)
... 12 more
Caused by: java.lang.NoSuchMethodException: com.domain.SimpleBean$$EnhancerByCGLIB$$4bc418be.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getDeclaredConstructor(Unknown Source)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:65)
... 13 more

如果我在 SimpleBean 类中引入无参数构造函数。它不会给出错误。但这并没有给我预先填充的 SimpleBean 对象(如在 XML 配置中使用 )。那么使用注解时,是否必须有无参数构造函数?正确的出路是什么?

最佳答案

来自@Configuration的javadoc:

 Configuration is meta-annotated as a {@link Component}, therefore Configuration
classes are candidates for component-scanning and may also take advantage of
{@link Autowired} at the field and method but not at the constructor level.

不幸的是,您必须找到其他方法来做到这一点。

关于spring - 使用 Spring 注解 @Autowired 的构造函数注入(inject)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10242004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com