smalllove 2019-11-09
在日常的开发中Web项目集成Spring框架,已经越来越重要,而Spring框架已经成为web开发的主流框架之一。本文主要讲解Java开发Web项目集成Spring框架的简单使用,以及使用Spring和不使用Spring框架,两者之间的差异。 仅供学习分享使用,如有不足之处,还请指正。
本示例的页面访问流程图如下所示:

步骤如下:
如下所示:在Service中对Dao进行了强关联
package com.hex.Dao;
/**
* 学生Dao
* @author Administrator
*
*/
public interface IStudentDao {
public String GetStudentById(int id);
}
////////////////////////////////////////
package com.hex.Dao;
/**
* 学生Dao
* @author Administrator
*
*/
public class StudentDaoImpl implements IStudentDao {
/**
* 查询学生信息
*/
@Override
public String GetStudentById(int id) {
return "hex";
}
}
////////////////////////////////////////
package com.hex.Service;
/**
* 学生服务接口
* @author Administrator
*
*/
public interface IStudentService {
public String GetStudentById(int id);
}
////////////////////////////////////////
package com.hex.Service;
import com.hex.Dao.IStudentDao;
import com.hex.Dao.StudentDaoImpl;
/**
* 学生服务实现类
* @author Administrator
*
*/
public class StudentServiceImpl implements IStudentService {
private IStudentDao studentDao;
public void setStudentDao(IStudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public String GetStudentById(int id) {
//studentDao=new StudentDaoImpl();
return studentDao.GetStudentById(id);
}
}如下所示:
package com.hex.servlet;
/**
* 访问Servlet实现类
*/
public class HomeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private IStudentService studentService;
/**
* 构造函数26 */
public HomeServlet() {
}
/**
* 初始化时声明studentService对象
*/
@Override
public void init() throws ServletException {
studentService=new StudentServiceImpl();
}
/**
* Get方法
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String studentName=studentService.GetStudentById(0);
request.setAttribute("studentName", studentName);
request.getRequestDispatcher("/jsp/Home.jsp").forward(request, response);
}
/**
* Post方法,此处和Get方法同
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}如下所示:
1 <a href="../HomeServlet">点击进入</a>
此处形成了强依赖,即HomeServlet需要StudentServiceImpl对象。且StudentServiceImpl需要StudentDao的支持。
Spring框架支持web项目需要的Jar包共7个,如下所示:
//日志包 commons-logging-1.1.1.jar //spring核心包 spring-aop-4.0.6.RELEASE.jar spring-beans-4.0.6.RELEASE.jar spring-context-4.0.6.RELEASE.jar spring-core-4.0.6.RELEASE.jar spring-expression-4.0.6.RELEASE.jar //web包 spring-web-4.0.6.RELEASE.jar
如下所示:
applicationContext.xml 位于src目录,所以需要加上classpath,是Spring容器的配置文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml
</param-value>
</context-param>
<!-- 配置spring-web.jar对应的监听器 ,Tomcat启动时,自动初始化IOC容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>如下所示:依赖引用对象属性采用ref方式,如果是值对象,则采用value方式。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Dao依赖于数据库的底层操作,本示例不予深入 -->
<bean id="studentDao" class="com.hex.Dao.StudentDaoImpl"></bean>
<!-- Service层依赖于StudentDao,采用set的方式注入 -->
<bean id="studentService" class="com.hex.Service.StudentServiceImpl">
<property name="studentDao" ref="studentDao"></property>
</bean>
</beans>如下所示:其他方法保持不变,增加studentService对象的getter和setter方法,然后通过容器注入的声明方式产生对象。在StudentServiceImpl中对StudengDao的依赖采用同样方法进行注入。
package com.hex.servlet;
/**
* Servlet实现类
*/
public class HomeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private IStudentService studentService;
public IStudentService getStudentService() {
return studentService;
}
public void setStudentService(IStudentService studentService) {
this.studentService = studentService;
}
/**
* 初始化时获取Sping的IOC容器中的bean对象
*/
@Override
public void init() throws ServletException {
//Web项目获取Spring上下文对象。
ApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
studentService=(IStudentService)context.getBean("studentService");
}
}此方式将Servlet和Service及Dao之间进行了解耦,灵活扩展性大大增强。
如果Spring的IOC容器文件有多个,可以采用Import的方式进行引入,如下所示:
1 <!-- 第二种方式,采用import方式引入其他容器文件 --> 2 <import resource="applicationContext2.xml"/>
在web.xml中配置servlet的方式,如下所示:
<?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>FirstWebSpring</display-name>
<servlet>
<description>
</description>
<display-name>HomeServlet</display-name>
<servlet-name>HomeServlet</servlet-name>
<servlet-class>com.hex.servlet.HomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HomeServlet</servlet-name>
<url-pattern>/HomeServlet</url-pattern>
</servlet-mapping>
<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>
<!-- 配置容器地址 -->
<!-- 第一种方式如果要加载多个配置文件,可以写多个,如下所示:
<param-value>
classpath:applicationContext.xml,
classpath:applicationContext2.xml
</param-value>
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<!-- 配置spring-web.jar对应的监听器 ,Tomcat启动时,自动初始化IOC容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>绳锯木断,水滴石穿。