JSP 開發(fā)之Spring Boot 動態(tài)創(chuàng)建Bean
更新時間:2017年07月14日 10:16:26 投稿:lqh
這篇文章主要介紹了JSP 開發(fā)之Spring Boot 動態(tài)創(chuàng)建Bean的相關資料,需要的朋友可以參考下
JSP 開發(fā)之Spring Boot 動態(tài)創(chuàng)建Bean
1、通過注解@Import導入方式創(chuàng)建
a、新建MyImportBeanDefinitionRegistrar注冊中心
Java代碼
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import web0.services.Myservice;
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
protected String BEAN_NAME = "myservice";
public void dynamicConfiguration() throws Exception {
}
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
if (!registry.containsBeanDefinition(BEAN_NAME)) {
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(Myservice.class);
beanDefinition.setSynthetic(true);
registry.registerBeanDefinition(BEAN_NAME, beanDefinition);
}
}
}
b、在配置類上加@Import引入上面的類
@Import(MyImportBeanDefinitionRegistrar.class)
public class TestConfig{
}
c、這樣操作后就可以使用spring的方式獲取該bean了
以上就是JSP 中Spring Boot 動態(tài)創(chuàng)建Bean的簡單實例,如有疑問請大家留言或者到本站的社區(qū)進行討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:
- Spring的實例工廠方法和靜態(tài)工廠方法實例代碼
- Spring實戰(zhàn)之使用靜態(tài)工廠方法創(chuàng)建Bean操作示例
- Spring工廠方法創(chuàng)建(實例化)bean實例代碼
- Spring如何使用注解的方式創(chuàng)建bean
- Spring基于ProxyFactoryBean創(chuàng)建AOP代理
- Spring創(chuàng)建Bean的6種方式詳解
- Spring BPP中如何優(yōu)雅的創(chuàng)建動態(tài)代理Bean詳解
- 關于Spring中Bean的創(chuàng)建進行更多方面的控制
- spring實現(xiàn)bean對象創(chuàng)建代碼詳解
- Spring Boot如何動態(tài)創(chuàng)建Bean示例代碼
- 詳解Spring Boot 使用Java代碼創(chuàng)建Bean并注冊到Spring中
- Spring實戰(zhàn)之調用實例工廠方法創(chuàng)建Bean操作示例
相關文章
ResourceBundle類在jsp中的國際化實現(xiàn)方法
下面小編就為大家?guī)硪黄猂esourceBundle類在jsp中的國際化實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
jsp連接MySQL實現(xiàn)插入insert操作功能示例
本文將為大家展示下jsp連接MySQL執(zhí)行插入操作的功能,具體的示例及代碼如下,感興趣的朋友可以了解下2013-08-08

