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

不使用Spring框架的开发流程
步骤如下:
1. 新增Service和Dao对应的类及接口实现
如下所示:在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);
}
}2. 新增HomeServlet类,并需要通过new的方式声明studentService对象
如下所示:
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);
}
}3. 前端页面进行访问即可
如下所示:
1 <a href="../HomeServlet">点击进入</a>
4. 缺点:
此处形成了强依赖,即HomeServlet需要StudentServiceImpl对象。且StudentServiceImpl需要StudentDao的支持。
采用Spring的方式进行访问
0. Spring框需要的Jar包
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
1. 需要在web.xml文件中配置Spring对应的监听器
如下所示:
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>2. 配置Spring的IOC容器
如下所示:依赖引用对象属性采用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>3. 在Servlet中,引入ApplicationContext对象,将Tomcat容器和Spring的IOC容器进行关联
如下所示:其他方法保持不变,增加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");
}
}4. 优势:
此方式将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>备注
绳锯木断,水滴石穿。
相关推荐
与卿画眉共浮生 2020-11-13
smalllove 2020-11-03
hellowordmonkey 2020-11-02
丽丽 2020-10-30
周太郎 2020-10-28
greensomnuss 2020-10-27
职业炮灰 2020-10-16
与卿画眉共浮生 2020-10-14
feinifi 2020-10-14
feinifi 2020-10-13
yangjinpingc 2020-10-09
davis 2020-09-29
RickyIT 2020-09-27
lisongchuang 2020-09-27
tangxiong0 2020-09-03
meleto 2020-08-17
幸运小侯子 2020-08-14
YangHuiLiang 2020-08-06