当前位置:首页 > JSP教程 > JSP常见问题

JSP 开发之Spring Boot 动态创建Bean

jsp 开发之spring boot 动态创建bean

1、通过注解@import导入方式创建

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 动态创建bean的简单实例,如有疑问请大家留言或者到本站的社区进行讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!