美文网首页我爱编程
kotlin语法-SpringBoot配置web.xml,Ser

kotlin语法-SpringBoot配置web.xml,Ser

作者: 福霜 | 来源:发表于2018-01-04 11:22 被阅读0次

1.SpringBoot用注解的方式配置web.xml

在web.xml中配置过滤器如下:

常见的过滤器配置

对应在SpingBoot中可以这样用:

SpringBoot中注解的方式配置过滤器

在SpringBoot项目中没有web.xml,需要用注解的方式配置bean,比如用到了第三方的工具包,需要配置他们的过滤器之类的。

以kotlin语法为例,新建一个类,实现ServletContextInitializer接口,重写onStartUp方法,

上图中onStartup中的配置就相当于下面的web.xml配置,

常规web.xml配置

需要配置监听,载入servlet等等,都可以通过这样的方式来配置,如下列图示:

SpringBoot配置servlet

2.Servlet中引入Spring容器中的bean

使用Mybatis+SpringBoot开发,mapper.xlm中主要是负责做数据处理的SQL语句标签,而mapper.xml被映射到一个mapper接口,被业务实现类所调用,

mapper.xml mapper接口 普通Service

这个UserService在Spring容器中,所以可以直接通过@Autowired+lateinit的方式注入bean,但是

Servlet容器中的service

在Servlet容器中的这个SyncService不能使用@Autowired+lateinit的方式注入bean,需要通过Spring容器来获取,比如新建一个Spring的工具类:

spring工具类

然后在Spring配置文件中加入对应的bean,用@Bean注解

配置bean

配置好bean后,就可以通过工具类获取到配置的bean,工具类.getBean("BeanName")

通过Spring工具类来获取bean

在kotlin中可避免异常提示:lateinit property uapuOrgMapper has not been initialized.

相关文章

网友评论

    本文标题:kotlin语法-SpringBoot配置web.xml,Ser

    本文链接:https://www.haomeiwen.com/subject/azzjxxtx.html