Spring与SpringMVC的整合

1、Spring和SpringMVC需不需要整合

1、不整合
   1)将所有的配置文件中都配置到springmvc.xml中
   2)将其他配置文件通过import标签导入到SpringMVC的配置文件中

2、整合
    1)Spring的IOC容器负责配置Dao、Service、数据源、事务、以及与其他框架的整合
    2)SpringMVC负责配置Handler、视图解析器等
   问题1、IOC容器如何初始化
              java工程:new ClassPathXmlApplicationContext("beans.xml")
              web工程:在web.xml中配置ContextLoaderListener监听器
   问题2、handler和service被创建了两次
              Spring不扫描Hanler
              SpringMVC只扫描hanler

2、整合步骤

2.1、在resources里面配置beans.xml和springmvc.xml文件

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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	
	<!-- 设置自动扫描的包 -->
	<context:component-scan base-package="afei.ss" use-default-filters="false">
		<!-- 设置不扫描handler -->
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>

</beans>

springmvc.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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	
	<!-- 设置自动扫描的包 -->
	<context:component-scan base-package="afei.ss">
		<!-- 设置只扫描的包 -->
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 配置视图解析器 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

2.2、配置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_2_5.xsd" id="WebApp_ID" version="2.5">
	<!-- 配置前端控制器:DispatcherServlet 快捷键 alt + / 倒数第二项 -->
	<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!-- 配置 SpringMVC配置文件的路径 -->
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<!-- 配置ContextLoaderListener,用来初始化Spring的配置文件,快捷键alt +  / ,倒数第三个 -->
	<!-- needed for ContextLoaderListener -->
	<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>
	

</web-app>

2.3、pom.xml配上基础的环境依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>afei.s</groupId>
  <artifactId>SpringBoot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <dependencies>
		<!-- context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>
		<!-- webmvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>
	</dependencies>
</project>

2.4、handler包

package afei.ss.handler;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import afei.ss.entities.Employee;
import afei.ss.service.EmployeeService;


@Controller
public class EmployeeHandler {
    //从IOC容器中自动装配的employeeService实现类
	@Autowired
	private EmployeeService employeeService;
	
	public EmployeeHandler() {
		System.out.println("EmployeeHandler is created!");
	}
	
	@RequestMapping("/getEmp")
	public String getEmployee(Map<String,Object> map) {
		//调用EmployeeService中获取一个Employee的方法
		Employee employee = employeeService.getEmployee();
		//将employee对象发到map中
		map.put("emp", employee);
		return "input";
	}
}

2.5、service包

一个接口EmployeeService

package afei.ss.service;

import afei.ss.entities.Employee;

public interface EmployeeService {
	public Employee getEmployee();
}

实现接口的子包impl

package afei.ss.service.impl;

import org.springframework.stereotype.Service;

import afei.ss.entities.Employee;
import afei.ss.service.EmployeeService;

@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService{
	
	public EmployeeServiceImpl() {
		System.out.println("EmployeeServiceImpl is created!");
	}
	//正常来说Service应该调用Dao来实现service方法
//	@Autowired
//	private EmployeeDao employeeDao;
	@Override
	public Employee getEmployee() {
		return new Employee("afei",1,null);
	}
		
}

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yongfeicao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值