Constructor-injection mit Spring-annotation @Autowired funktioniert nicht
Erstellt habe ich 2 einfache Klassen. Der Konstruktor einer Klasse versehen, die als @Autowired. Akzeptiert Sie das Objekt der anderen Klasse. Aber dieser code nicht.
Klassen :-
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) Inner.java
@Configuration
public class InnerBean {
String simpleProp1;
public String getSimpleProp1() {
return simpleProp1;
}
public void setSimpleProp1(String simpleProp1) {
this.simpleProp1 = simpleProp1;
}
}
Wenn ich versuche zu laden ApplicationConext
ApplicationContext acnxt = new AnnotationConfigApplicationContext("com.domain");
Gibt es folgende Fehler :-
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
Wenn ich Ihnen no-arg-Konstruktor in die Klasse SimpleBean. Es gibt keine Fehler.
Aber das gibt mir nicht vor-poulated Objekt SimpleBean ( wie in der XML-Konfiguration die Verwendung von < constructor-arg > ).
Also, wenn die Verwendung von Anmerkungen, ist es zwingend erforderlich, dass die no-arg-Konstruktor ?
Was ist der geeignete Weg ?
- Sind Sie versuchen, zu nennen, wie
AnnotationConfigApplicationContext("com.domain")
Paket? Setzen Sie bitte Ihre vollständigen code. - ja, diese Klassen in com.domain-Paket. Und ich bin versucht, zu rufen Datei ApplicationContext acnxt = new AnnotationConfigApplicationContext("com.domain"); Es wurde bereits erwähnt.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Aus der javadoc von
@Configuration
:Also müssen Sie einen anderen Weg finden, es zu tun, leider.
Ich glaube, Sie sind die Vermischung der
@Configuration
und@Component
annotation. Wie pro die Frühjahr docs,@ - Konfiguration
dient zum erstellen von beans mithilfe von Java-code (Methoden, annotiert mit@Bean
erstellen Sie eine bean, in der Erwägung, dass Klassen versehen mit@Komponente
werden automatisch erstellt..Ich hoffe, dass der folgende Code veranschaulicht dies:
InnerBean.java:
SimpleBean.java:
OuterBean.java
Config.java
Main.java:
Main-Klasse verwendet die
AnnotationConfigApplicationContext
Scannen für beide@Configuration
und@Component
Anmerkungen und erstellen Sie die Bohnen entsprechend.com.domain.SimpleBean$$EnhancerByCGLIB$$4bc418be.<init>()
lässt vermuten, dass es weitere proxies beteiligt.inside SimpleBean constructor inner's property is null
(mit@Component
)