1) 需要进行 Spring 整合 SpringMVC 吗 ?
2) 还是否需要再加入 Spring 的 IOC 容器 ?
3) 是否需要在web.xml 文件中配置启动 Spring IOC 容器的 ContextLoaderListener ?
需要: 通常情况下, 类似于数据源, 事务, 整合其他框架都是放在 Spring 的配置文件中(而不是放在 SpringMVC 的配置文件中).。实际上放入 Spring 配置文件对应的 IOC 容器中的还有 Service 和 Dao.。
不需要::都放在 SpringMVC 的配置文件中.。也可以分多个 Spring 的配置文件, 然后使用 import 节点导入其他的配置文件 。
1) 监听器配置
在web.xml文件中:
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>springDispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springDispatcherServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:beans.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
web-app>
2) 创建Spring的bean的配置文件:beans.xml
<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="com.learn.ss">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
context:component-scan>
beans>
在HelloWorldHandler、UserService类中增加构造方法,启动服务器,查看构造器执行情况。
问题: 若 Spring 的 IOC 容器和 SpringMVC 的 IOC 容器扫描的包有重合的部分, 就会导致有的 bean 会被创建 2 次
解决:
使 Spring 的 IOC 容器扫描的包和 SpringMVC 的 IOC 容器扫描的包没有重合的部分.
使用 exclude-filter 和 include-filter 子节点来规定只能扫描的注解
beans.xml文件已经设置好了,代码在上边。
springmvc.xml:
<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="com.learn.ss" use-default-filters="false">
<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>
项目实例的其他部分:
项目实体类:
Department.java:
package com.learn.ss.entities;
public class Department {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Department [id=" + id + ", name=" + name + "]";
}
}
Employee.java:
package com.learn.ss.entities;
public class Employee {
private Integer id;
private String lastName;
private String email;
private Department dept;
public Employee() {
super();
}
public Employee(Integer id, String lastName, String email, Department dept) {
super();
this.id = id;
this.lastName = lastName;
this.email = email;
this.dept = dept;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", dept=" + dept + "]";
}
}
Service接口和实现:
EmployeeService.java接口:
package com.learn.ss.service;
import com.learn.ss.entities.Employee;
public interface EmployeeService {
Employee getEmployee();
}
EmployeeServiceImpl.java实现类:
package com.learn.ss.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.learn.ss.entities.Employee;
import com.learn.ss.service.EmployeeService;
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {
public EmployeeServiceImpl() {
System.out.println("EmployeeServiceImpl对象被创建!");
}
// 正常来说Service应该调用Dao来实现Service中的方法
// @Autowired
// private EmployeeDao employee;
@Override
public Employee getEmployee() {
return new Employee(1, "张三", "[email protected]", null);
}
}
Handler:
EmployeeHandler.java:
package com.learn.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 com.learn.ss.entities.Employee;
import com.learn.ss.service.EmployeeService;
@Controller
public class EmployeeHandler {
public EmployeeHandler() {
System.out.println("EmployeeHandler对象被创建了!");
}
@Autowired
private EmployeeService employeeService;
/*
* Spring和SpringMVC需不需要整合?
* 1.不整合
* 1).将所有的配置都配置到SpringMVC的配置文件中
* 2).将其他配置文件通过import标签导入到SpringMVC的配置文件中
* 2.整合
* Spring的IOC容器负责配置Dao、Service、数据源、事物以及其它框架的整合
* SpringMVC负责配置Handler、视图解析器、国际化等
* 问题一:IOC容器如何初始化
* Java工程:new ClassPathXmlApplicationContext("bean.xml")
* web工程:在web.xml中配置ContextLoaderListener监听器
* 问题二:Handler和Service对象创建了两次
* Spring不扫描Handler
* SpringMVC只扫描Handler
*/
@RequestMapping("/getEmp")
public String getEmployee(Map<String , Object> map) {
//调用EmployeeService中获取一个Employee的方法
Employee employee = employeeService.getEmployee();
//将employee对象发到map中
map.put("emp", employee);
return "input";
}
}
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/getEmp">获取一个员工</a>
</body>
</html>
相应页面input.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>员工的信息是:${requestScope.emp }</h1>
</body>
</html>
pom.xml文件同样是配置了spring和springmvc的依赖类,与请几篇博客中的相同。
引用自尚硅谷课件,代码由博主手工编写