• 项目中出现Spring和SpringMVC时,如何进行配置?


    绝美风景

    09310db7ba57488c9e91ed05cb828e3c.png

     

    前面在讲解什么是SpringMVC时,并没有spring和web项目是需要进行运行的,但是在开发过程中Spring和SpringMVC这两种框架是需要分开进行配置的还是以前面那个文章的hello案例为例,讲解一下如何分开将spring和springmvc分开进行配置

    那么为什么要将这两种语言分开进行配置吗?

    因为spring和SpringMVC是两种不同的容器,Spring容器是通过ContextLoaderListener类来进行实现的,而SpringMVC则是通过DispatcherServlet来对SpringMVC项目进行加载的,两个容器并不一样。

    当请求到达服务器时,后台会找DispatcherServlet去处理,而DispatcherServlet只会到SpringMVC项目中进行加载,那么有人就会问为什么要把Spring和SpringMVC两个项目的配置文件分开?其实在简单的项目开发时,简单的配置并不会导致项目出现问题,有两种原因导致Spring和SpringMVC的配置文件不放在一起

    1:为了后期的管理

    2:在Spring+SpringMVC+Hibernate组合开发中并不支持这两种的框架放在一个配置文件中。

     

    通过代码去实现Spring框架与SpringMVC框架分开进行配置,还是以hello案例为基础,在实现过程中添加对Spring的控制。

    创建一个Spring类:

    1. import org.springframework.stereotype.Service;
    2. @Service //为了后期,通过不同的配置文件进行调用:此类的标记为@Service
    3. public class contro {
    4. public void play(){
    5. System.out.println("看看我输出了吗");
    6. }
    7. }

    创建SpringMVC中控制器类:

    1. package demo1;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.web.servlet.ModelAndView;
    4. import org.springframework.web.servlet.mvc.Controller;
    5. import javax.servlet.http.HttpServletRequest;
    6. import javax.servlet.http.HttpServletResponse;
    7. @org.springframework.stereotype.Controller (value = "/hell") //这个类使用的标记为@Controller 与上一个Spring创建的类区分开来
    8. public class controller implements Controller{
    9. @Autowired
    10. contro contro; //调用Spring创建的类,看内容是否进行输出
    11. public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    12. //request:表示前端发来的请求
    13. //reponse:表示服务器给前端的响应
    14. //ModeAndVier:数据模型视图 就是前端能够展示的内容
    15. contro.play();//调用spring框架的类,看代码是否的到运行
    16. ModelAndView mc = new ModelAndView("hello");
    17. mc.addObject("aa","schoenberg"); //创建一个属性,并未这个属性注入值,在网页文件里调用这个属性,并且在网页上进行设置内容的输出
    18. System.out.println("看看我执行了吗");
    19. return mc;
    20. }
    21. }

    因为类一是属于Spring框架的类,所以我们需要通过Spring配置文件的方法,对类一进行对象的创建

    Spring框架配置文件:

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    6. <context:component-scan base-package="demo1" use-default-filters="true">
    7. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    8. context:component-scan>
    9. beans>

      配置SpringMVC框架的配置文件

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    6. <context:component-scan base-package="demo1" use-default-filters="true">
    7. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    8. context:component-scan>
    9. <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" id="handlerMapping">
    10. <property name="beanName" value="/hell">property>
    11. bean>
    12. <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" id="handlerAdapter">bean>
    13. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="resourceViewResolver">
    14. <property name="prefix" value="/jsp/">property>
    15. <property name="suffix" value=".jsp">property>
    16. bean>
    17. beans>

    配置web.xml配置文件:实现两个配置文件的加载

    1. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    4. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    5. <display-name>Archetype Created Web Applicationdisplay-name>
    6. <servlet>
    7. <servlet-name>springmvcservlet-name>
    8. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    9. <init-param>
    10. <param-name>contextConfigLocationparam-name>
    11. <param-value>classpath:hello-servlet.xmlparam-value>
    12. init-param>
    13. servlet>
    14. <servlet-mapping>
    15. <servlet-name>springmvcservlet-name>
    16. <url-pattern>/url-pattern>
    17. servlet-mapping>
    18. <context-param>
    19. <param-name>contextConfigLocationparam-name>
    20. <param-value>classpath:spring_demo1.xmlparam-value>
    21. context-param>
    22. web-app>

    代码配置完成:

    浏览器实现SpringMVC框架所要实现的内容

    2a213e63cc5b49519040cc58a8b66115.png

     控制器结果输出:输出Spring框架所要输出的内容69489a482d154ceeac1b3d18cd4b99f8.png

     两种结果的显示表明,现已通过配置实现两种框架的分开进行执行

     

     

  • 相关阅读:
    TIDB - 分布式数据库
    【SQL性能优化】Hash索引的底层原理是什么?
    iOS应用加固方案解析:ipa加固安全技术全面评测
    企业工程项目管理系统源码+spring cloud +spring boot+项目模块功能清单
    深入理解JVM虚拟机第二十一篇:详解JVM当中的操作数在栈以及分析操作数栈与字节码指令和执行引擎的关系图解
    一键让你使用录屏
    CodeQL使用流程
    http的get与post
    【Spring】Spring MVC
    数商云B2B电商系统商品管理功能剖析,助力家用电器企业业务提效
  • 原文地址:https://blog.csdn.net/m0_52479012/article/details/127577797