JSP 開發(fā)之Spring Boot 動態(tài)創(chuàng)建Bean
JSP 開發(fā)之Spring Boot 動態(tài)創(chuàng)建Bean
1、通過注解@Import導(dǎo)入方式創(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的簡單實(shí)例,如有疑問請大家留言或者到本站的社區(qū)進(jìn)行討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Spring的實(shí)例工廠方法和靜態(tài)工廠方法實(shí)例代碼
- Spring實(shí)戰(zhàn)之使用靜態(tài)工廠方法創(chuàng)建Bean操作示例
- Spring工廠方法創(chuàng)建(實(shí)例化)bean實(shí)例代碼
- Spring如何使用注解的方式創(chuàng)建bean
- Spring基于ProxyFactoryBean創(chuàng)建AOP代理
- Spring創(chuàng)建Bean的6種方式詳解
- Spring BPP中如何優(yōu)雅的創(chuàng)建動態(tài)代理Bean詳解
- 關(guān)于Spring中Bean的創(chuàng)建進(jìn)行更多方面的控制
- spring實(shí)現(xiàn)bean對象創(chuàng)建代碼詳解
- Spring Boot如何動態(tài)創(chuàng)建Bean示例代碼
- 詳解Spring Boot 使用Java代碼創(chuàng)建Bean并注冊到Spring中
- Spring實(shí)戰(zhàn)之調(diào)用實(shí)例工廠方法創(chuàng)建Bean操作示例
相關(guān)文章
ResourceBundle類在jsp中的國際化實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猂esourceBundle類在jsp中的國際化實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07JSP基于JDBC的數(shù)據(jù)庫連接類實(shí)例
這篇文章主要介紹了JSP基于JDBC的數(shù)據(jù)庫連接類,以實(shí)例形式較為詳細(xì)的分析了JDBC連接數(shù)據(jù)庫的實(shí)現(xiàn)技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-11-11jsp連接MySQL實(shí)現(xiàn)插入insert操作功能示例
本文將為大家展示下jsp連接MySQL執(zhí)行插入操作的功能,具體的示例及代碼如下,感興趣的朋友可以了解下2013-08-08