How to inject the bean? of other @ Configuration annotations in the class of the same @ Configuration annotation in the method of @ Bean

for example

 @Configuration
 public class MyConfig{
  
     @Resource(name = "ds1")
     DataSource ds1;   
 
     @Bean(name="ds2")
     DataSource getDataSource(){
        ...
     }
     
     @bean(name= "session")
     Session getSession(){
        DataSource ds;
        if(..){
            ds = ds1;
        }else{
            ds = ds2;
        }
        return new Session(ds);
     }
     
     @bean("mananger")
     Manager getManager(){
         return new Manager(ds2);
     }
 }

now I want to use ds2 in the bean of "session" and "mananger". How to inject the bean,?

Jun.11,2021

just call the method directly. The method annotated by @ Bean will be rewritten by Spring, and multiple calls will return the same Bean object


like this

 @Configuration
 public class MyConfig{
  
     @Resource(name = "ds1")
     DataSource ds1;   
 
     @Bean(name="ds2")
     DataSource getDataSource(){
        ...
     }
     
     @bean(name= "session')
     Session getSession(DataSource ds2){
        DataSource ds;
        if(..){
            ds = ds1;
        }else{
            ds = ds2;
        }
        return new Session(ds);
     }
     
     @bean("mananger")
     Manager getManager(DataSource ds2){
         return new Manager(ds2);
     }
 }
Menu