0
votes

New to spring.
I have defined 2 @Beans with unique names for each but with the same method name. One of the beans are not created and No valid bean exception is thrown.

@Bean("Example.A") 
public ClassA getNewBean() {
   return new ClassA();
}




@Bean("Example.B")
public ClassA getNewBean() {
  return new ClassA();
}

The second Bean is not created and Exception is thrown as no valid bean exists ClassA.

This post has the two beans in different classes, whereas in my case both are within the same @Configuration.

1
One class cannot have multiple same name methods. Can you show your code and stack trace?DEWA Kazuyuki - 出羽和之
Can you provide stacktrace? How you inject these beans?tomeszmh

1 Answers

1
votes

When you inject by bean name, you can use @Resource:

@Controller
public class MyController {
    @Resource(name = "Example.A")
    private ClassA obj;

    // ...
}

As an aside, beans should be named by Java standard convention.