SSM中的XML配置文件和使用JAVA代码写的配置类简单对比

本文探讨了传统SSM项目中XML配置的使用,指出这些配置可以通过Java代码实现,以减少项目的臃肿。文章通过比较Spring容器的bean管理、SpringMVC的配置,展示了如何用Java代码替代XML配置,包括DispatcherServlet、HandlerMapping等关键组件的配置。尽管文中未涉及Mybatis,但提供了SSM项目中Spring和SpringMVC的XML配置与Java配置类的示例。
摘要由CSDN通过智能技术生成

传统的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&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偷偷学习被我发现

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值