这篇文章主要介绍了jsp 开发之spring boot 动态创建bean的相关jsp的资料,对jsp感兴趣的朋友可以参考下本篇文章
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的简单实例,如有疑问请大家留言或者到本站的社区进行讨论!!
相关推荐:
详细介绍spring boot 添加jsp支持配置的实例
关于jsp页面跳转的详细介绍
在jsp提交表单的参数封装到一个方法里
以上就是jsp 开发之spring boot 动态创建bean的详细内容。