绝美风景

前面在讲解什么是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组合开发中并不支持这两种的框架放在一个配置文件中。
- import org.springframework.stereotype.Service;
-
- @Service //为了后期,通过不同的配置文件进行调用:此类的标记为@Service
- public class contro {
- public void play(){
- System.out.println("看看我输出了吗");
- }
- }
- package demo1;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.servlet.ModelAndView;
- import org.springframework.web.servlet.mvc.Controller;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- @org.springframework.stereotype.Controller (value = "/hell") //这个类使用的标记为@Controller 与上一个Spring创建的类区分开来
- public class controller implements Controller{
- @Autowired
- contro contro; //调用Spring创建的类,看内容是否进行输出
-
- public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
- //request:表示前端发来的请求
- //reponse:表示服务器给前端的响应
- //ModeAndVier:数据模型视图 就是前端能够展示的内容
- contro.play();//调用spring框架的类,看代码是否的到运行
- ModelAndView mc = new ModelAndView("hello");
- mc.addObject("aa","schoenberg"); //创建一个属性,并未这个属性注入值,在网页文件里调用这个属性,并且在网页上进行设置内容的输出
- System.out.println("看看我执行了吗");
- return mc;
- }
- }
因为类一是属于Spring框架的类,所以我们需要通过Spring配置文件的方法,对类一进行对象的创建
- "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 https://www.springframework.org/schema/context/spring-context.xsd">
- <context:component-scan base-package="demo1" use-default-filters="true">
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- context:component-scan>
- beans>
-
- "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 https://www.springframework.org/schema/context/spring-context.xsd">
-
-
-
- <context:component-scan base-package="demo1" use-default-filters="true">
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- context:component-scan>
- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" id="handlerMapping">
- <property name="beanName" value="/hell">property>
- bean>
-
- <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" id="handlerAdapter">bean>
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="resourceViewResolver">
- <property name="prefix" value="/jsp/">property>
- <property name="suffix" value=".jsp">property>
- bean>
-
-
- beans>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <display-name>Archetype Created Web Applicationdisplay-name>
- <servlet>
- <servlet-name>springmvcservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:hello-servlet.xmlparam-value>
- init-param>
- servlet>
- <servlet-mapping>
- <servlet-name>springmvcservlet-name>
- <url-pattern>/url-pattern>
- servlet-mapping>
- <context-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:spring_demo1.xmlparam-value>
- context-param>
- web-app>


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