美文网首页
spring-mvc-10-运行流程及spring与spring

spring-mvc-10-运行流程及spring与spring

作者: liangxifeng833 | 来源:发表于2018-01-22 20:25 被阅读36次

一. 整体流程

Paste_Image.png

二. Spring和SpringMVC

  • 通常情况下, 类似于数据源, 事务, 整合其他框架都是放在 Spring 的配置文件中(而不是放在 SpringMVC 的配置文件中).实际上放入 Spring 配置文件对应的 IOC 容器中的还有 Service 和 Dao等.
    比如新建配置文件名为 beans.xml
<?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-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!-- 
       spring 配置文件.
           通常情况下, 类似于数据源, 事务, 整合其他框架都是放在 Spring 的配置文件中(而不是放在 SpringMVC 的配置文件中).
           实际上放入 Spring 配置文件对应的 IOC 容器中的还有 Service 和 Dao等.
     -->    
    <!-- 配置自动扫描的包, 除了ControllerAdvice和Controller注解不初始化到Spring IOC容器, 其他均初始化 -->
    <context:component-scan base-package="com.atguigu.springmvc" use-default-filters="true">
         <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
    </context:component-scan>
    
    <!-- 配置数据源,整合其他框架,事务等 -->
</beans>
  • 配置 web.xmlContextLoaderListener 监听器,读取spring beans.xml 配置文件内容
  <!-- 配置ContextLoaderListener监听器,读取spring配置文件内容 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:beans.xml</param-value>
    </context-param>
    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  • 修改 springmvc.xml 扫描包,只扫描秒handler需要的注解 @Controller@ControllerAdvice 注解
     <!-- 配置自动扫描的包-->
    <context:component-scan base-package="com.atguigu.springmvc" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
    </context:component-scan>

注意: 如果不这样配置,则启动tomcat时候,回使同一个bean被初始化两次

  • 新建 UserService 处理业务罗辑类
package com.atguigu.springmvc;

import org.springframework.stereotype.Service;
/**
 * 初始化在Spring的IOC容器中
 * @author lxf
 */
@Service
public class UserService {
    public  UserService()
    {
        System.out.println("UserService Contructor...");
    }
}
  • 在SpringMVC的IOC容器中的HelloWorld hanlder 可以引用 Spring IOC容器中的 UserService ,反之则不行. Spring IOC 容器中的 UserService却不能来引用 SpringMVC IOC 容器中的 HelloWorld
package com.atguigu.springmvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * 测试helloworld处理器
 * @author lxf
 */
@Controller
public class HelloWorld {
    //引用Spring IOC容器中的bean
    @Autowired
    private UserService userService;
    public  HelloWorld()
    {
        System.out.println("HelloWorld contructor ... ");
    }
    @RequestMapping("/hello")
    public String hello()
    {
        System.out.println("Spring IOC userService = " + userService);
        System.out.println("hello world");
        return "success";
    }
}
Spring IOC userService = com.atguigu.springmvc.UserService@5ea925c4
hello world

点击查看代码练习

相关文章

网友评论

      本文标题:spring-mvc-10-运行流程及spring与spring

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