整合Spring和SpringMVC

1.Spring容器和SpringMVC容器的关系

Spring容器是一个父容器,SpringMVC容器是一个子容器,它继承自Spring容器。因此,在SpringMVC容器中,可以访问到Spring容器中定义的Bean,而在Spring容器中,无法访问SpringMVC容器中定义的Bean。在Web开发中,Controller全部在SpringMVC中扫描,除了Controller之外的Bean,全部在Spring容器中扫描(Service、Dao),按这种方式扫描,扫描完完成后,Controller可以访问到Service。

1.为什么不全部都在Spring中扫描
因为处理器映射器只会去SpringMVC中查找到Controller,如果没有,就找不到,不会去Spring中找,这就决定了,Controller必须在SpringMVC中扫描。
2.为什么不全部在SpringMVC中扫描
在SSM整合或者Spring+SpringMVC+JdbcTemplate中,可以全部在SpringMVC中扫描,但是,在SSH整合中,这种方式不允许。

最佳实践:
1.Controller在SpringMVC中扫描,视图解析器等在SpringMVC容器中配置
2.Spring中扫描Service、Dao已经其他组件,事务定义、数据源定义都在Spring容器中配置

2.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
     <!-- 在Spring容器中,扫描除了Controller之外的其他组件 -->
    <context:component-scan base-package="com.dpb"
        use-default-filters="true">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
</beans>

3.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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
      <!-- SpringMVC容器只扫描Controller -->
    <context:component-scan base-package="com.dpb"
        use-default-filters="false">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
    <mvc:annotation-driven />
</beans>

4.web.xml文件中

分别加载spring和SpringMVC的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>test</display-name>
    <!-- 这里配置Spring配置文件的位置,param-name是固定的,
    param-value是文件位置 这个配置可以省略,如果省略,
    系统默认去/WEB-INF/目录下查找applicationContext.xml作为Spring的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

5.案例实现

SpringMVC配置文件

<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">    

    <!-- 在Spring容器中,扫描除了Controller之外的其他组件 -->
    <context:component-scan base-package="com.dpb"
        use-default-filters="true">
        <!-- 不扫描Controller注解 -->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <!-- 配置一个数据源 -->
    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="ds">
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="username" value="pms"/>
        <property name="password" value="pms"/>
    </bean>

    <!-- 配置JdbcTemplate实例 -->
    <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
        <!-- 通过构造注入的方式关联数据源 -->
        <constructor-arg name="dataSource" ref="ds"/>
    </bean>
</beans>

Spring配置文件

<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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        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-4.3.xsd">
    <!-- 开启注解 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!-- 开启扫描 -->
    <context:component-scan base-package="com.dpb.*">
        <!-- 只扫描特定的注解 -->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>

web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>test</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

    <!-- 这里配置Spring配置文件的位置,param-name是固定的,
        param-value是文件位置 这个配置可以省略,如果省略,
        系统默认去/WEB-INF/目录下查找applicationContext.xml作为Spring的配置文件
    -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 配置前端控制器 -->
    <!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等) 如果不配置contextConfigLocation,
        默认加载的是/WEB-INF/servlet名称-serlvet.xml(springmvc-servlet.xml) -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 防止资源文件被spring MVC拦截 -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpg</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>
</web-app>

Dao代码

/**
 *
 * @author dpb【波波烤鸭】
 *
 */
public interface IUserDao {

    public List<User> query();
}
/**
 * dao层实现
 * @author dpb【波波烤鸭】
 *
 */
@Repository
public class UserDaoImpl implements IUserDao {

    @Resource
    JdbcTemplate jdbcTemplate;

    @Override
    public List<User> query() {
        String sql = "select * from t_sysuser ";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
    }
}

Service代码

/**
 *
 * @author dpb【波波烤鸭】
 *
 */
public interface IUserService {

    public List<User> query();
}
/**
 *
 * @author dpb【波波烤鸭】
 *
 */
@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private IUserDao dao;

    @Override
    public List<User> query() {
        // TODO Auto-generated method stub
        return dao.query();
    }
}

controller代码

/**
 * 案例
 *
 * @author dpb【波波烤鸭】
 *
 */
@Controller
public class UserController {

    @Resource
    private IUserService userService;

    @RequestMapping("/query")
    public String query(Model model){
        List<User> list = userService.query();
        model.addAttribute("list", list);
        return "/user.jsp";
    }

}

jsp页面代码

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

    <c:forEach items="${list }" var="user">
        ${user.id }--${user.uname }--${user.nickname }<br>
    </c:forEach>
</body>
</html>

查询结果

案例代码:链接:https://pan.baidu.com/s/1J26hh7FL1s2HuJJnxkUIaw
提取码:l1cs
上一篇:SpringMVC教程4

原文地址:https://www.cnblogs.com/dengpengbo/p/10308105.html

时间: 2024-11-09 00:38:40

整合Spring和SpringMVC的相关文章

整合Spring、SpringMVC、MyBatis

spring+springmvc+mybatis集成 一个核心:将对象交给spring管理. 1新建web项目 2添加项目jar包 spring包见上一篇博客 3建立项目的目录结构 4完成Mapper的集成 和mybatis进行集成,交给spring产生Mapper接口的代理对象. 4.1建立Mapper接口 1 package org.guangsoft.mapper; 2 3 import java.util.List; 4 5 import org.guangsoft.pojo.Privi

SSM整合(spring、springMVC、mybatis)

需要用的包: 包括:spring的包.springMVC的包.mybatis的包.数据库驱动包.json相关的包 配置如下,首先是mybatis的配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/myba

整合spring,springmvc和mybatis

我创建的是maven项目,使用到的依赖架包有下面这些: <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.2.RELEASE</version> </dependency> <dependency> <

搭建Spring + SpringMVC + Mybatis框架之三(整合Spring和Mybatis)

整合Spring和SpringMVC 之前已经整合了spring和mybatis,现在在此基础上整合SSM. 项目目录: 思路:SpringMVC的配置文件独立,然后在web.xml中配置整合. (1)配置spring-mvc.xml 主要是自动扫描控制器,视图模式. <?xml version="1.0" encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/sch

搭建Spring + SpringMVC + Mybatis框架之三(整合Spring、Mybatis和Spring MVC)

目录 整合Spring和SpringMVC 整合Spring和SpringMVC 之前已经整合了spring和mybatis,现在在此基础上整合SSM.项目目录: 思路:SpringMVC的配置文件独立,然后在web.xml中配置整合. (1)配置spring-mvc.xml 主要是自动扫描控制器,视图模式. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.s

Spring、SpringMVC、Hibernate整合

1.整合Spring.SpringMVC 2.整合Spring.Hernate 3.配置web.xml文件 4.配置Tomcat,并且配置为热部署 5.在此不过多介绍Spring.SpringMVC整合 6.Spring与Hibernate整合关键代码如下 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <pr

整合Spring时Service层为什么不做全局包扫描详解

转载:http://blog.csdn.net/s740556472/article/details/54879954 一.Spring和SpringMVC的父子容器关系 1.讲问题之前要先明白一个关系 一般来说,我们在整合Spring和SpringMVC这两个框架中,web.xml会这样写到: <!-- 加载spring容器 --> <!-- 初始化加载application.xml的各种配置文件 --> <context-param> <param-name&

spring和springMVC整合注解版helloworld

整合的之前需要从官网上下载spring完整的jar包,我下载的是spring-framework-3.2.2.RELEASE.整合步骤如下: 1.在eclipse中新建一个web项目,将下载的spring的jar包拷入lib目录下,但是spring启动的时候依赖一个commons-logging-1.1.jar的jar包,你需要额外的下载. 2.编写web.xml,配置spring的分发器和spring配置文件的位置.具体内容如下: <servlet> <servlet-name>

搭建Spring + SpringMVC + Mybatis框架之二(整合Spring和Mybatis)

整合Spring和Mybatis 首先给出完整的项目目录: (1)引入项目需要的jar包 使用http://maven.apache.org作为中央仓库即可. Spring核心包,mybatis核心包,json数据需要的包[经常需要用到],log4j日志管理包. 1 <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&q