Das ist Klasse A
Code: Select all
package com.pm.spring;
public class A {
public A() {
System.out.println("Default Constructor in A");
}
}
Code: Select all
package com.pm.spring;
public class B {
A a;
public B(A a) {
super();
System.out.println("Parameterized Constructor in B");
this.a = a;
}
}
Code: Select all
package com.pm.spring;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean(name = "Bean-A")
public A createA() {
A a = new A();
return a;
}
@Bean(name = "Bean-B", autowire = Autowire.BY_NAME)
public B createB(A a) {
B b = new B(a);
return b;
}
}
Code: Select all
Default Constructor in A
Jan 08, 2025 12:52:58 PM org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'Bean-B' defined in com.pm.spring.AppConfig: Unsatisfied dependency expressed through method 'createB' parameter 0: Ambiguous argument values for parameter of type [com.pm.spring.A] - did you specify the correct bean references as arguments?
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'Bean-B' defined in com.pm.spring.AppConfig: Unsatisfied dependency expressed through method 'createB' parameter 0: Ambiguous argument values for parameter of type [com.pm.spring.A] - did you specify the correct bean references as arguments?
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:785)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.(AnnotationConfigApplicationContext.java:89)
at com.pm.spring.Demo.main(Demo.java:10)
Wenn ich jedoch autowire = Autowire.BY_NAME entferne, wird es ausgeführt normalerweise.
Code: Select all
package com.pm.spring;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean(name = "Bean-A")
public A createA() {
A a = new A();
return a;
}
@Bean(name = "Bean-B")
public B createB(A a) {
B b = new B(a);
return b;
}
}
Code: Select all
Default Constructor in A
Parameterized Constructor in B