当前位置: 移动技术网 > IT编程>开发语言>Java > 一起学Spring之Web基础篇

一起学Spring之Web基础篇

2019年11月10日  | 移动技术网IT编程  | 我要评论

概述

在日常的开发中web项目集成spring框架,已经越来越重要,而spring框架已经成为web开发的主流框架之一。本文主要讲解java开发web项目集成spring框架的简单使用,以及使用spring和不使用spring框架,两者之间的差异。 仅供学习分享使用,如有不足之处,还请指正。

页面访问流程图

本示例的页面访问流程图如下所示:

 

不使用spring框架的开发流程

步骤如下:

1. 新增service和dao对应的类及接口实现

如下所示:在service中对dao进行了强关联

 1 package com.hex.dao;
 2 
 3 /**
 4  * 学生dao
 5  * @author administrator
 6  *
 7  */
 8 public interface istudentdao {
 9     public string getstudentbyid(int id);
10 }
11 ////////////////////////////////////////
12 package com.hex.dao;
13 
14 /**
15  * 学生dao
16  * @author administrator
17  *
18  */
19 public class studentdaoimpl implements istudentdao {
20 
21     /**
22      * 查询学生信息
23      */
24     @override
25     public string getstudentbyid(int id) {
26         
27         return "hex";
28     }
29 
30 }
31 ////////////////////////////////////////
32 package com.hex.service;
33 
34 /**
35  * 学生服务接口
36  * @author administrator
37  *
38  */
39 public interface istudentservice {
40     public string getstudentbyid(int id);
41 }
42 ////////////////////////////////////////
43 package com.hex.service;
44 
45 import com.hex.dao.istudentdao;
46 import com.hex.dao.studentdaoimpl;
47 
48 /**
49  * 学生服务实现类
50  * @author administrator
51  *
52  */
53 public class studentserviceimpl implements istudentservice {
54 
55     private istudentdao studentdao;
56     
57     public void setstudentdao(istudentdao studentdao) {
58         this.studentdao = studentdao;
59     }
60 
61     @override
62     public string getstudentbyid(int id) {
63         //studentdao=new studentdaoimpl();
64         return studentdao.getstudentbyid(id);
65     }
66 
67 }

2. 新增homeservlet类,并需要通过new的方式声明studentservice对象

如下所示:

 1 package com.hex.servlet;
 2 
15 /**
16  * 访问servlet实现类
17  */
18 public class homeservlet extends httpservlet {
19     private static final long serialversionuid = 1l;
20     
21     private istudentservice studentservice;
22       
23 
24     /**
25      * 构造函数26      */
27     public homeservlet() {
28        
29     }
30     
31     /**
32      * 初始化时声明studentservice对象
33      */
34     @override
35     public void init() throws servletexception {
36         studentservice=new studentserviceimpl();
37     }
38 
39     /**
40      * get方法
41      */
42     protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
43         
44         string studentname=studentservice.getstudentbyid(0);
45         request.setattribute("studentname", studentname);
46         request.getrequestdispatcher("/jsp/home.jsp").forward(request, response);
47     }
48 
49     /**
50      * post方法,此处和get方法同
51      */
52     protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
53         // todo auto-generated method stub
54         doget(request, response);
55     }
56 
57 }

3. 前端页面进行访问即可

如下所示:

1 <a href="../homeservlet">点击进入</a>

4. 缺点:

此处形成了强依赖,即homeservlet需要studentserviceimpl对象。且studentserviceimpl需要studentdao的支持。

采用spring的方式进行访问

0. spring框需要的jar包

spring框架支持web项目需要的jar包共7个,如下所示:

 1 //日志包
 2 commons-logging-1.1.1.jar
 3 //spring核心包
 4 spring-aop-4.0.6.release.jar
 5 spring-beans-4.0.6.release.jar
 6 spring-context-4.0.6.release.jar
 7 spring-core-4.0.6.release.jar
 8 spring-expression-4.0.6.release.jar
 9 //web包
10 spring-web-4.0.6.release.jar

 

1. 需要在web.xml文件中配置spring对应的监听器

如下所示:

applicationcontext.xml 位于src目录,所以需要加上classpath,是spring容器的配置文件

1 <context-param>
2     <param-name>contextconfiglocation</param-name>
3     <param-value>classpath:applicationcontext.xml
4     </param-value>
5 </context-param>
6 <!-- 配置spring-web.jar对应的监听器 ,tomcat启动时,自动初始化ioc容器 -->
7 <listener>
8     <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
9 </listener>

2. 配置spring的ioc容器

如下所示:依赖引用对象属性采用ref方式,如果是值对象,则采用value方式。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 4 xsi:schemalocation="http://www.springframework.org/schema/beans
 5  http://www.springframework.org/schema/beans/spring-beans.xsd">
 6      <!-- dao依赖于数据库的底层操作,本示例不予深入 -->
 7      <bean id="studentdao" class="com.hex.dao.studentdaoimpl"></bean>
 8      <!-- service层依赖于studentdao,采用set的方式注入 -->
 9      <bean id="studentservice" class="com.hex.service.studentserviceimpl">
10          <property name="studentdao" ref="studentdao"></property>
11      </bean>
12 </beans>

3. 在servlet中,引入applicationcontext对象,将tomcat容器和spring的ioc容器进行关联

如下所示:其他方法保持不变,增加studentservice对象的getter和setter方法,然后通过容器注入的声明方式产生对象。在studentserviceimpl中对studengdao的依赖采用同样方法进行注入。

 1 package com.hex.servlet;
 2 
 3 /**
 4  * servlet实现类
 5  */
 6 public class homeservlet extends httpservlet {
 7     private static final long serialversionuid = 1l;
 8     
 9     private istudentservice studentservice;
10        
11     public istudentservice getstudentservice() {
12         return studentservice;
13     }
14 
15     public void setstudentservice(istudentservice studentservice) {
16         this.studentservice = studentservice;
17     }
18     
19     /**
20      * 初始化时获取sping的ioc容器中的bean对象
21      */
22     @override
23     public void init() throws servletexception {
24         //web项目获取spring上下文对象。
25         applicationcontext context=webapplicationcontextutils.getwebapplicationcontext(this.getservletcontext());
26         studentservice=(istudentservice)context.getbean("studentservice");
27     }
28 }

4. 优势:

此方式将servlet和service及dao之间进行了解耦,灵活扩展性大大增强。

小知识

如果spring的ioc容器文件有多个,可以采用import的方式进行引入,如下所示:

1 <!-- 第二种方式,采用import方式引入其他容器文件 -->
2 <import resource="applicationcontext2.xml"/>

在web.xml中配置servlet的方式,如下所示:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <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">
 3     <display-name>firstwebspring</display-name>
 4     <servlet>
 5         <description>
 6         </description>
 7         <display-name>homeservlet</display-name>
 8         <servlet-name>homeservlet</servlet-name>
 9         <servlet-class>com.hex.servlet.homeservlet</servlet-class>
10     </servlet>
11     <servlet-mapping>
12         <servlet-name>homeservlet</servlet-name>
13         <url-pattern>/homeservlet</url-pattern>
14     </servlet-mapping>
15     <welcome-file-list>
16         <welcome-file></welcome-file>
17         <welcome-file>index.htm</welcome-file>
18         <welcome-file>index.jsp</welcome-file>
19         <welcome-file>default.html</welcome-file>
20         <welcome-file>default.htm</welcome-file>
21         <welcome-file>default.jsp</welcome-file>
22     </welcome-file-list>
23     <!-- 配置容器地址 -->
24     <!-- 第一种方式如果要加载多个配置文件,可以写多个,如下所示:
25         <param-value>
26             classpath:applicationcontext.xml,
27             classpath:applicationcontext2.xml
28         </param-value>
29      -->
30     <context-param>
31         <param-name>contextconfiglocation</param-name>
32         <param-value>
33             classpath:applicationcontext.xml
34         </param-value>
35     </context-param>
36     <!-- 配置spring-web.jar对应的监听器 ,tomcat启动时,自动初始化ioc容器 -->
37     <listener>
38         <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
39     </listener>
40 </web-app>

备注

绳锯木断,水滴石穿。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网