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);
     }
 }
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e52326-451a9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e52326-451a9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?