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);
}
}