读书人

Spring开发指南读书笔记(1)

发布时间: 2012-11-21 08:23:25 作者: rapoo

Spring开发指南读书笔记(一)

Spring给程序员提供了一个轻量级的构建技术框架。它主要的特性就是控制反转IOC和面向切面AOP编程。

它的作用是使用基本的Bean来完成EJB可以完成的工作,提高了代码的可重用性,简单、松耦合和可测试性。

一个简单的Spring例子:

public interface Action {

?public String execute(String str);
}

实现类:

public class LowerAction implements Action{

?private String message;

?public String getMessage() {
??return message;
?}

?public void setMessage(String message) {
??this.message = message;
?}

?public String execute(String str) {
??return (getMessage()+str).toLowerCase();
?}
}

spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
?<bean id="TheAction" class ="com.spring.LowerAction">
??<property name="message">
???<value>Hello</value>
??</property>
?</bean>
</beans>

测试:

public void testQickStart(){
??ApplicationContext ctx=new FileSystemXmlApplicationContext("bean.xml");
??Action action=(Action)ctx.getBean("TheAction");
??System.out.println(action.execute("Jeff Cheng"));
?}

在spring中的依赖注入有3种实现类型:接口注入、设值注入和构造子注入。所谓依赖注入,就是在运行期由容器将依赖关系注入到组件之中。而程序员只须负责对接口的编程而不必理会接口具体是如何实现的。

一个完整的Bean配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
?<description>Spring Bean Configuration Sample</description>
?<bean id="TheAction"
??encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
?<!-- id必须为messageSource -->
?<bean id="messageSource" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
?<description>Spring Login Action</description>
?<bean id="loginaction" class ="com.spring.LoginAction"/>
?<bean id="listener" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Definition of View Resolver -->
?<bean id="viewResolver" class ="org.springframework.web.servlet.view.InternalResourceViewResolver">
????????????????? ?????<!-- 可选.servlet.view.freemarker.FreeMarkerViewResolver
??????????????????????? ?或servlet.view.velocity.VelocityViewResolver -->
??<property name="viewClass">
???<value>org.springframework.web.servlet.view.JstlView</value>
??</property>
??
??<property name="prefix">???????????????????//设定名称前缀
???<value>WEB-INF/view/</value>
??</property>
??
??<property name="suffix">?????????????????? //设定名称后缀
???<value>.jsp</value>
??</property>
?</bean>
?
?<bean id="RegisterValidator" prefix="c"%>

? <body>
??? <p>Login Success!</p>
?<p>Current User:
??<c:out value="${logininfo.username}" /><br>
?</p>

?<p>Your current messages:</p>
?<c:forEach items="${messages}" var="item" begin="0" end="9" step="1" varStatus="var">
??<c:choose>
???<c:when test="${var.index%2==0}">
???*
???</c:when>
???<c:otherwise>
???!
???</c:otherwise>
??</c:choose>
??<c:out value="${item}" /><br>
?</c:forEach>
? </body>

register.jsp:

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>

? <body style="text-align:center">
?<form action="/SpringMVC/register.do" method="post">
??<spring:bind path="command.*">
???<font color="#FF0000">
????<c:forEach items="${status.errorMessages}" var="error">
?????<spring:message code="error_msg" /><c:out value="${error}" /><br>
????</c:forEach>
???</font>
??</spring:bind>
??<table border="0" width="450" height="101" cellspacing="0" cellpadding="0">
???<tr>
????<td height="27" width="408" colspan="2">
?????<p align="center"><b><spring:message code="register_title" /></b></td>
???</tr>

???<tr>
????<td height="23" width="104"><spring:message code="username_label" />:</td>
????<td height="23" width="450">
?????<spring:bind path="command.username">
??????<input type="text"
???????value="<c:out value='${status.value}'/>"?
???????name="<c:out value='${status.expression}'/>">
??????(<spring:message code="less4chars" />)
??????<br>
??????<c:if test="${status.error}">
???????<font color="#FF0000">
????????<spring:message code="error_msg" />
????????<c:forEach items="${status.errorMessages}" var="error">
?????????<c:out value="${error}"/>
????????</c:forEach>
???????</font>
??????</c:if>
?????</spring:bind>
????</td>
????
???</tr>

???<tr>
????<td height="23" width="104"><spring:message code="password_label" />:</td>
????<td height="23" width="450">
????<spring:bind path="command.password1">
??????<input type="password"
???????value="<c:out value='${status.value}'/>"?
???????name="<c:out value='${status.expression}'/>">
??????(<spring:message code="less6chars" />)
??????<br>
??????<c:if test="${status.error}">
???????<font color="#FF0000">
????????<spring:message code="error_msg" />
????????<c:forEach items="${status.errorMessages}" var="error">
?????????<c:out value="${error}"/>
????????</c:forEach>
???????</font>
??????</c:if>
?????</spring:bind>
????</td>
???</tr>

???<tr>
????<td height="23" width="104"><spring:message code="rep_pass_label" />:</td>
????<td height="23" width="450">
????<spring:bind path="command.password2">
??????<input type="password"
???????value="<c:out value='${status.value}'/>"?
???????name="<c:out value='${status.expression}'/>">
??????<br>
??????<c:if test="${status.error}">
???????<font color="#FF0000">
????????<spring:message code="error_msg" />
????????<c:forEach items="${status.errorMessages}" var="error">
?????????<c:out value="${error}"/>
????????</c:forEach>
???????</font>
??????</c:if>
?????</spring:bind>
????</td>
???</tr>

??</table>
?<p>
??<input type="submit" value="提交" name="B1">
??<input type="reset" value="重置" name="B2">
?</p>
?</form>???
? </body>

RegisterSuccess.jsp:

<body>
?<p align="center">
??<c:out value="${logininfo.username}"/>注册成功!
?</p>
? </body>

showError.jsp:

<body>
?<%Exception ex=(Exception)request.getAttribute("Exception"); %>
?<h2>Exception:<%ex.getMessage(); %></h2>
?<p/>
?<%ex.printStackTrace(new java.io.PrintWriter(out)); %>
? </body>

模版技术:

XSLT:基于XML的表现层模版技术,移植性强,界面开发难度大,语法复杂,调试难度大,性能比较低下,内存占用较大
Velocity:最为成熟的模版技术,得到广泛的应用。
FreeMarker:表现逻辑和业务逻辑划分严格,模板不允许对Servlet API直接操作,禁止对HttpServletRequest对象直接访问,保证了层次间的清晰。提供了对JSP Tag的良好的支持,在生产效率和学习成本上具有优势。

下面用FreeMarker来改造上面的例子:

Config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
?<!-- Definition of View Resolver -->
?<bean id="viewResolver" class ="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
??<property name="viewClass">
???<value>org.springframework.web.servlet.view.freemarker.FreeMarkerView</value>
??</property>
??
??<property name="cache">
???<value>false</value>
??</property>
??
??<property name="suffix">
???<value>.ftl</value>
??</property>
?</bean>
?
?<bean id="freemarkerConfig" class="com.action.LoginAction">
??<property name="commandClass">
???<value>com.action.LoginInfo</value>
??</property>
??
??<property name="fail_view">
???<value>loginfail</value>
??</property>
??
??<property name="success_view">
???<value>main</value>
??</property>
?</bean>
</beans>

main.ftl:

<html>
<head>
?<title>Welcome!</title>
</head>
<body>
?<h1>Current User:${logininfo.username}</h1>
?<#list messages as msg>
??<#if msg_index%2=0>
???*
??<#else>
???!
??</#if>
??${msg}<br>
?</#list>
</body>
</html>


读书人网 >软件架构设计

热点推荐