传统的SSM项目中,存在着大量的类似XML这样的配置文件,这样一个项目就显得十分臃肿,其实吧,在很多框架的源码中就知道的了这些配置文件是通过类加载或者注解反射来获取里面的配置的信息的,所以说了,是可以使用java代码来替代配置文件的。
这里拿出了几处SSM中的配置信息用java代码来实现,并不是一开始上来就是SpringBoot哦!
传统的SSM
Spring容器是管理bean对象的,所以开扫描扫注解注入,或者你直接在配置文件里进行配置,也就是构造注入,属性注入,注解注入,这些注入在springboot中也一样的。
SpringMVC中要配置的就很明显了,主要的就是那个几个啦,DispatcherServlet前端控制器,
HandlerMapping处理器映射器,Handler处理器,HandlAdapter处理器适配器,ViewResolver视图解析器。
这里没有Mybatis,有点挂羊卖狗肉的感觉。
直接使用代码来对比吧
依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
</dependencies>
SSM中的Spring配置文件,applicationContext.xml
像个融合剂一样的,扫controller以外的包
Spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.zsl" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
Spring配置类
/**
* @author ZSL
* @ClassName SpringCofig
* @description:
*
* @Configuration:表示该类是个配置类
* @ComponentScan:扫描
* basePackages&