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

2018-01-22  本文已影响36人  liangxifeng833

一. 整体流程

Paste_Image.png

二. Spring和SpringMVC

<?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>
  <!-- 配置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>
     <!-- 配置自动扫描的包-->
    <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被初始化两次

package com.atguigu.springmvc;

import org.springframework.stereotype.Service;
/**
 * 初始化在Spring的IOC容器中
 * @author lxf
 */
@Service
public class UserService {
    public  UserService()
    {
        System.out.println("UserService Contructor...");
    }
}
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

点击查看代码练习

上一篇 下一篇

猜你喜欢

热点阅读