读书人

Spring 引文学习手札(一) 构建简单W

发布时间: 2012-09-19 13:43:53 作者: rapoo

Spring 注解学习手札(一) 构建简单Web应用

这个系列学习笔记皆来源于大牛的博客:http://snowolf.iteye.com/blog/577989

近来工作发生了一些变化,有必要学习一下Spring注解了!Spring 引文学习手札(一) 构建简单Web应用
网上找了一些个例子,总的说来比较土,大多数是转载摘抄,按照提示弄下来根本都运行不了,索性自己趟一遍这浑水,在这里留下些个印记。
这次,先来构建一个极为简单的web应用,从controller到dao。不考虑具体实现,只是先对整体架构有一个清晰的了解。日后在分层细述每一层的细节。Spring 引文学习手札(一) 构建简单Web应用

相关参考:
Spring 注解学习手札(一) 构建简单Web应用
Spring 注解学习手札(二) 控制层梳理
Spring 注解学习手札(三) 表单页面处理
Spring 注解学习手札(四) 持久层浅析
Spring 注解学习手札(五) 业务层事务处理
Spring 注解学习手札(六) 测试


我们将用到如下jar包:

<?xml version="1.0" encoding="UTF-8"?><web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"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"><display-name>spring</display-name><!-- 应用路径 --><context-param><param-name>webAppRootKey</param-name><param-value>spring.webapp.root</param-value></context-param><!-- Log4J 配置 --><context-param><param-name>log4jConfigLocation</param-name><param-value>classpath:log4j.xml</param-value></context-param><context-param><param-name>log4fRefreshInterval</param-name><param-value>60000</param-value></context-param><!--Spring上下文 配置 --><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext.xml</param-value></context-param><!-- 字符集 过滤器 --><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- Spring 监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener><!-- Spring 分发器 --><servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/servlet.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>*.do</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></web-app>


有不少人问我,这段代码是什么:

<!-- 应用路径 --><context-param><param-name>webAppRootKey</param-name><param-value>spring.webapp.root</param-value></context-param>


这是当前应用的路径变量,也就是说你可以在其他代码中使用${spring.webapp.root}指代当前应用路径。我经常用它来设置log的输出目录。
为什么要设置参数log4jConfigLocation?

<!-- Log4J 配置 --><context-param><param-name>log4jConfigLocation</param-name><param-value>classpath:log4j.xml</param-value></context-param><context-param><param-name>log4fRefreshInterval</param-name><param-value>60000</param-value></context-param>


这是一种基本配置,spring中很多代码使用了不同的日志接口,既有log4j也有commons-logging,这里只是强制转换为log4j!并且,log4j的配置文件只能放在classpath根路径。同时,需要通过commons-logging配置将日志控制权转交给log4j。同时commons-logging.properties必须放置在classpath根路径。
commons-logging内容:

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger


最后,记得配置log4j的监听器:

<listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener>


接下来,我们需要配置两套配置文件,applicationContext.xml和servlet.xml。
applicationContext.xml用于对应用层面做整体控制。按照分层思想,统领service层和dao层。servlet.xml则单纯控制controller层。

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><importresource="service.xml" /><importresource="dao.xml" /></beans>


applicationContext.xml什么都不干,它只管涉及到整体需要的配置,并且集中管理。
这里引入了两个配置文件service.xml和dao.xml分别用于业务、数据处理。
service.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:component-scanbase-package="org.zlex.spring.service" /></beans>


注意,这里通过<context:component-scan />标签指定了业务层的基础包路径——“org.zlex.spring.service”。也就是说,业务层相关实现均在这一层。这是有必要的分层之一。
dao.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><context:component-scanbase-package="org.zlex.spring.dao" /></beans>


dao层如法炮制,包路径是"org.zlex.spring.dao"。从这个角度看,注解还是很方便的!Spring 引文学习手札(一) 构建简单Web应用
最后,我们看看servlet.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:component-scanbase-package="org.zlex.spring.controller" /><beanid="urlMapping"/><bean/></beans>


包路径配置就不细说了,都是一个概念。最重要的时候后面两个配置,这将使得注解生效!Spring 引文学习手札(一) 构建简单Web应用
“org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping”是默认实现,可以不写,Spring容器默认会默认使用该类。
“org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter”直接关系到多动作控制器配置是否可用!
简单看一下代码结构,如图:
Spring 引文学习手札(一) 构建简单Web应用
Account类是来存储账户信息,属于域对象,极为简单,代码如下所示:
Account.java

/** * 2010-1-23 */package org.zlex.spring.domain;import java.io.Serializable;/** * * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a> * @version 1.0 * @since 1.0 */public class Account implements Serializable {/** * */private static final long serialVersionUID = -533698031946372178L;private String username;private String password;/** * @param username * @param password */public Account(String username, String password) {this.username = username;this.password = password;}/** * @return the username */public String getUsername() {return username;}/** * @param username the username to set */public void setUsername(String username) {this.username = username;}/** * @return the password */public String getPassword() {return password;}/** * @param password the password to set */public void setPassword(String password) {this.password = password;}}


通常,在构建域对象时,需要考虑该对象可能需要进行网络传输,本地缓存,因此建议实现序列化接口Serializable?Spring 引文学习手札(一) 构建简单Web应用
我们再来看看控制器,这就稍微复杂了一点代码如下所示:
AccountController .java

    /** ??*?2010-1-23 ??*/??package?org.zlex.spring.controller; ?? ??import?javax.servlet.http.HttpServletRequest; ?? import?javax.servlet.http.HttpServletResponse; ?? ??import?org.springframework.beans.factory.annotation.Autowired; ?? import?org.springframework.stereotype.Controller; ?? import?org.springframework.web.bind.ServletRequestUtils; ?? import?org.springframework.web.bind.annotation.RequestMapping; ?? import?org.springframework.web.bind.annotation.RequestMethod; ?? import?org.zlex.spring.service.AccountService; ?? ??/** ?

读书人网 >Web前端

热点推荐