2017-05-30 16 views
0

我有一個java配置文件(具有@configuration註解的類)。它有一個使用@Bean註解的方法,我想根據一些參數來實例化這個bean。換句話說,我想通過名稱獲得一個bean(通過參數傳遞)並實例化這個bean。
是否有可能在@configuration類中做到這一點?
getBean在java配置文件中的名稱

@Configuration 
public class ApplicationConfig { 
    @Resource 
    private Config config; 
    @Bean 
    public Object application() throws ParseException { 
     return new SampleApp(/*get the bean by name*/); 
    } 
} 

配置包含了爭吵,我想用這種說法,並以該名稱獲取豆。

回答

2

像這樣的東西應該工作:

@Configuration 
public class ApplicationConfig { 
    @Resource 
    private Config config; 
    @Autowired 
    private ApplicationContext appContext; 

    @Bean 
    public Object application() throws ParseException { 
     return new SampleApp(
       (appContext.getBean("beanNameFromConfig")); 
    } 
} 
相關問題