我的附加代码出现以下错误:
NoSuchBeanDefinitionException:没有为依赖项找到[java.lang.String]类型的限定bean
如果从另一个bean工厂方法显式调用一个bean工厂方法,为什么有任何疑问?
public class StringArguments {
static class Name {
private final String name1;
private final String name2;
private final String name3;
Name(String name1, String name2, String name3) {
this.name1 = name1;
this.name2 = name2;
this.name3 = name3;
}
@Override
public String toString() {
return "Name{" +
"name1='" + name1 + '\'' +
", name2='" + name2 + '\'' +
", name3='" + name3 + '\'' +
'}';
}
}
static class Caller {
private final Name name;
Caller(Name name) {
this.name = name;
}
public Name getName() {
return name;
}
}
@Configuration
@ComponentScan(basePackageClasses = StringArguments.class)
static class StringArgsConfig {
@Bean(name = "name1")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public Name name(String name1) {
return new Name(name1, name1 + "2", name1 + "3");
}
@Bean(name = "name3")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public Name name(String name1, String name2, String name3) {
return new Name(name1, name2, name3);
}
@Bean(name = "caller1")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public Caller caller(String name) {
return new Caller(name(name));
}
@Bean(name = "caller3")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public Caller caller(String name1, String name2, String name3) {
return new Caller(name(name1, name2, name3));
}
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(StringArgsConfig.class);
Caller caller1 = (Caller) context.getBean("caller1", "niket");
Caller caller3 = (Caller) context.getBean("caller3", "kumar");
System.out.println(caller1.getName());
System.out.println(caller3.getName());
}
}