JBuilder开发Spring实例
2008-01-05 20:35:20 来源:WEB开发网
1. 下载sPRing包,网址如下
http://www.springframework.org/download.Html
解压后的目录中包含了dist、lib等子目录
2. 在JBuilder2005中增加spring库,选择菜单Tools-Configure-Libraries,在弹出的对话框中点击New按钮,输入spring库的名称:spring,点击Add按钮,将dist目录中的所有jar文件增加到spring库中
3. 新建工程文件,选菜单File-New Project,给工程文件取名为myProject
设置工程文件的属性,选菜单Project-Project Properties,选择Tomcat作为服务器,将spring库加入Path/Required Libraries。因为例子中用到了log4j,将包含了log4j的库加入Path/Required Libraries,注重到lib目录下有两个子目录log4j和jakarta-commons,它们的用法是不同的,如下面的代码片断所示:
log4j
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class SpringappController implements Controller {
/** Logger for this class and subclasses */
static Logger logger = Logger.getLogger(SpringappController.class);
jakarta-commons
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SpringappController implements Controller {
/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());
4. 新建Web模块,选菜单File-New,给Web模块取名为springapp
5. 新建hello.jsp文件如下:
<%@ page contentType="text/html; charset=Big5" %>
<html>
<head><title>Example :: Spring application</title></head>
<body>
<h1>Hello - Spring Application</h1>
<p>Greetings.</p>
</body>
</html>
6. 新建类文件SpringappController.java如下:
package spring;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class SpringappController implements Controller {
/** Logger for this class and subclasses */
// protected final Log logger = LogFactory.getLog(getClass());
static Logger logger = Logger.getLogger(SpringappController.class);
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
logger.info("SpringappController - returning hello view");
return new ModelAndView("hello.jsp");
}
}
7. 修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
8. 在web-inf目录中新建springapp-servlet.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--
- Application context definition for "springapp" DispatcherServlet.
-->
<beans>
<bean id="springappController" class="spring.SpringappController"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.htm">springappController</prop>
</props>
</property>
</bean>
</beans>
9. 在web-inf目录中新建log4j.properties文件如下:
log4j.rootCategory=INFO, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=springapp.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files
log4j.appender.logfile.MaxBackupIndex=3
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
#Pattern to output : date priority [category] - <message>line_separator
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - <%m>%n
10. 设置web模块springapp的属性Content,加入文件log4j.properties,因为classes目录是自动产生的,这一步是为了把文件log4j.properties加到classes目录。
11. 设置Run Configuration,选菜单Run-Configurations,新建一个Run Configuration,Type选Server,Launch URI设置为:/springapp/hello.htm,给它取名为hello
12. 运行,你会在messages窗口中看到如下信息:
資訊: Server startup in 9253 ms
2004-11-05 15:05:00,585 INFO [spring.SpringappController] - <SpringappController - returning hello view>
2004/11/5 下午 03:05:00 org.springframework.web.servlet.view.AbstractCachingViewResolver resolveViewName
資訊: Cached view 'hello.jsp'
在myProject/Tomcat/springapp.log文件中增加了一行如下所示:
2004-11-05 15:11:32,348 INFO [spring.SpringappController] - <SpringappController - returning hello view>
这说明你已成功建立了基本的spring应用。
13. 增加对jstl的支持,设置工程文件的属性,选菜单Project-Project Properties,将jstl库加入Path/Required Libraries。
14. 改进前面的例子,改进的文件如下:
增加“header”文件include.jsp,这是一些jsp文件的公用部分,这样可以使得开发和维护更轻易。所有jsp文件放在web-inf/jsp目录下,为的是只有Controller可以访问View。
springapp/war/WEB-INF/jsp/include.jsp
<%@ page session="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
利用JSTL,<c:redirect>将页面重定向到Controller,这样就建立了index.jsp和应用架构的联系。
springapp/war/index.jsp
<%@ include file="/WEB-INF/jsp/include.jsp" %>
<%-- Redirected because we can't set the welcome page to a virtual URL. --%>
<c:redirect url="/hello.htm"/>
利用JSTL的<c:out>标记,把从Model提取的,传递给View的当前日期和时间作为输出。
springapp/war/WEB-INF/jsp/hello.jsp
<%@ include file="/WEB-INF/jsp/include.jsp" %>
<html>
<head><title>Hello :: Spring Application</title></head>
<body>
<h1>Hello - Spring Application</h1>
<p>Greetings, it is now <c:out value="${now}"/>
</p>
</body>
</html>
在SpringappController.java中增加包含了当前日期和时间的string作为Model
springapp/src/SpringappController.java
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SpringappController implements Controller {
/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String now = (new java.util.Date()).toString();
logger.info("returning hello view with " + now);
return new ModelAndView("WEB-INF/jsp/hello.jsp", "now", now);
}
}
15. 浏览改进后的结果http://localhost:8080/springapp,首先访问index.jsp,然后重定向到hello.htm,接著把控制交给Controller,Controller把日期和时间传送给View。
只能在浏览器这样测试。在JBuilder2005中设置Run Configuration不能访问http://localhost:8080/springapp,但可以访问http://localhost:8080/springapp/index.jsp
在上面的例子中,Controller指定了View的完全路径,使得Controller和View之间存在不必要的依靠。为了移出这种依靠,我们可以用类ResourceBundleViewResolver 和 a SimpleUrlHandlerMapping在属性文件中定义这种依靠,对于简单的情形,我们也可以利用InternalResourceViewResolver设置前缀和后缀,下面就用第二种方法。修改文件springapp-servlet.xml包含ViewResolver设置,我们还选择使用JstlView,这使得我们可以结合使用JSTL和message resource bundles,因此可以支持国际化。
springapp/war/WEB-INF/springapp-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--
- Application context definition for "springapp" DispatcherServlet.
-->
<beans>
<bean id="springappController" class="SpringappController"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.htm">springappController</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
<property name="prefix"><value>/WEB-INF/jsp/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
</beans>
现在,我们就可以移出Controller中View名称的前缀和后缀了。
springapp/src/SpringappController.java
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SpringappController implements Controller {
/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String now = (new java.util.Date()).toString();
logger.info("returning hello view with " + now);
return new ModelAndView("hello", "now", now);
}
}
这样修改后的例子应该仍能运行。
16. 增加商业逻辑类
为了分开Web逻辑和商业逻辑,我们建立两个不同的包,web和bus
springapp/src/bus/ProdUCt.java
package bus;
import java.io.Serializable;
public class Product implements Serializable {
private String description;
private Double price;
public void setDescription(String s) {
description = s;
}
public String getDescription() {
return description;
}
public void setPrice(Double d) {
price = d;
}
public Double getPrice() {
return price;
}
}
springapp/src/bus/ProductManager.java
package bus;
import java.io.Serializable;
import java.util.List;
public class ProductManager implements Serializable {
private List products;
public void setProducts(List p) {
products = p;
}
public List getProducts() {
return products;
}
}
修改SpringappController.java
springapp/src/web/SpringappController.java
package web;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import bus.Product;
import bus.ProductManager;
public class SpringappController implements Controller {
/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());
private ProductManager prodMan;
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String now = (new java.util.Date()).toString();
logger.info("returning hello view with " + now);
Map myModel = new HashMap();
myModel.put("now", now);
myModel.put("products", getProductManager().getProducts());
return new ModelAndView("hello", "model", myModel);
}
public void setProductManager(ProductManager pm) {
prodMan = pm;
}
public ProductManager getProductManager() {
return prodMan;
}
}
17. 修改View以便显示商业数据,增加对message bundle的支持
springapp/war/WEB-INF/jsp/hello.jsp
<%@ include file="/WEB-INF/jsp/include.jsp" %>
<html>
<head><title><fmt:message key="title"/></title></head>
<body>
<h1><fmt:message key="heading"/></h1>
<p><fmt:message key="greeting"/> <c:out value="${model.now}"/>
</p>
<h3>Products</h3>
<c:forEach items="${model.products}" var="prod">
<c:out value="${prod.description}"/> <i>$<c:out value="${prod.price}"/></i><br><br>
</c:forEach>
</body>
</html>
18. 增加一些测试数据
我们现在还不打算增加代码,从数据库装载商业对象。我们只springapp-servlet.xml中增加bean和messageSource设置,提供一些测试数据和对messages resource bundle的支持。
springapp/war/WEB-INF/springapp-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--
- Application context definition for "springapp" DispatcherServlet.
-->
<beans>
<bean id="springappController" class="web.SpringappController">
<property name="productManager">
<ref bean="prodMan"/>
</property>
</bean>
<bean id="prodMan" class="bus.ProductManager">
<property name="products">
<list>
<ref bean="product1"/>
<ref bean="product2"/>
<ref bean="product3"/>
</list>
</property>
</bean>
<bean id="product1" class="bus.Product">
<property name="description"><value>Lamp</value></property>
<property name="price"><value>5.75</value></property>
</bean>
<bean id="product2" class="bus.Product">
<property name="description"><value>Table</value></property>
<property name="price"><value>75.25</value></property>
</bean>
<bean id="product3" class="bus.Product">
<property name="description"><value>Chair</value></property>
<property name="price"><value>22.79</value></property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename"><value>messages</value></property>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.htm">springappController</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
<property name="prefix"><value>/WEB-INF/jsp/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
</beans>
19. 增加message bundle
springapp/war/WEB-INF/classes/messages.properties
title=SpringApp
heading=Hello :: SpringApp
greeting=Greetings, it is now
20. 浏览改进后的结果http://localhost:8080/springapp
实现数据库持久层
21. 设置HSQL数据库,在JBuilder2005中增加HSQL库,选择菜单Tools-Configure-Libraries,在弹出的对话框中点击New按钮,输入HSQL库的名称:HSQL,点击Add按钮,将lib/hsqldb目录下的文件hsqldb.jar增加到HSQL库中。
选择菜单Enterprise-Enterprise Setup,在弹出的对话框中选择Database Drivers,按Add按钮增加HSQL库。
选择菜单Tools-Database Pilot,然后选择菜单Files-New输入Driver:org.hsqldb.jdbcDriver,URL:jdbc:hsqldb:db/test, 双击hsqldb:db/test,输入UserName:sa,不必输入PasssWord。输入如下的SQL语句并执行
CREATE TABLE products (
id INTEGER NOT NULL PRIMARY KEY,
description varchar(255),
price decimal(15,2)
);
CREATE INDEX products_description ON products(description);
INSERT INTO products (id, description, price) values(1, 'Lamp', 5.78);
INSERT INTO products (id, description, price) values(2, 'Table', 75.29);
INSERT INTO products (id, description, price) values(3, 'Chair', 22.81);
这样就会在JBuilder_Home/bin目录下创建一个目录db,存放了数据库test的数据
22. 创建JDBC DAO (Data access Object)实现
springapp/src/db/ProductManagerDao.java
package db;
import bus.Product;
import java.util.List;
public interface ProductManagerDao {
public List getProductList();
public void increasePrice(Product prod, int pct);
}
springapp/src/db/ProductManagerDaoJdbc.java
package db;
import bus.Product;
import java.util.List;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;
import org.springframework.jdbc.core.SqlParameter;
public class ProductManagerDaoJdbc implements ProductManagerDao {
/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());
private DataSource ds;
public List getProductList() {
logger.info("Getting products!");
ProductQuery pq = new ProductQuery(ds);
return pq.execute();
}
public void increasePrice(Product prod, int pct) {
logger.info("Increasing price by " + pct + "%");
SqlUpdate su =
new SqlUpdate(ds, "update products set price = price * (100 + ?) / 100 where id = ?");
su.declareParameter(new SqlParameter("increase", Types.INTEGER));
su.declareParameter(new SqlParameter("ID", Types.INTEGER));
su.compile();
Object[] oa = new Object[2];
oa[0] = new Integer(pct);
oa[1] = new Integer(prod.getId());
int count = su.update(oa);
logger.info("Rows affected: " + count);
}
public void setDataSource(DataSource ds) {
this.ds = ds;
}
class ProductQuery extends MappingSqlQuery {
ProductQuery(DataSource ds) {
super(ds, "SELECT id, description, price from products");
compile();
}
protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Product prod = new Product();
prod.setId(rs.getInt("id"));
prod.setDescription(rs.getString("description"));
prod.setPrice(new Double(rs.getDouble("price")));
return prod;
}
}
}
springapp/src/bus/Product.java
package bus;
import java.io.Serializable;
public class Product implements Serializable {
private int id;
private String description;
private Double price;
public void setId(int i) {
id = i;
}
public int getId() {
return id;
}
public void setDescription(String s) {
description = s;
}
public String getDescription() {
return description;
}
public void setPrice(Double d) {
price = d;
}
public Double getPrice() {
return price;
}
}
springapp/src/test/TestProductManagerDaoJdbc.java
package tests;
import java.util.List;
import java.util.ArrayList;
import junit.framework.TestCase;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import db.ProductManagerDaoJdbc;
import bus.Product;
public class TestProductManagerDaoJdbc extends TestCase {
private ProductManagerDaoJdbc pmdao;
public void setUp() {
pmdao = new ProductManagerDaoJdbc();
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("org.hsqldb.jdbcDriver");
ds.setUrl("jdbc:hsqldb:db/test");
ds.setUsername("sa");
ds.setPassword("");
pmdao.setDataSource(ds);
}
public void testGetProductList() {
List l = pmdao.getProductList();
Product p1 = (Product) l.get(0);
assertEquals("Lamp", p1.getDescription());
Product p2 = (Product) l.get(1);
assertEquals("Table", p2.getDescription());
}
public void testIncreasePrice() {
List l1 = pmdao.getProductList();
Product p1 = (Product) l1.get(0);
assertEquals(new Double("5.78"), p1.getPrice());
pmdao.increasePrice(p1, 10);
List l2 = pmdao.getProductList();
Product p2 = (Product) l2.get(0);
assertEquals(new Double("6.36"), p2.getPrice());
}
}
23. 修改Web应用使其使用数据库持久层
springapp/src/bus/ProductManager.java
package bus;
import java.io.Serializable;
import java.util.ListIterator;
import java.util.List;
import db.ProductManagerDao;
public class ProductManager implements Serializable {
private ProductManagerDao pmd;
private List products;
public void setProductManagerDao(ProductManagerDao pmd) {
this.pmd = pmd;
}
/*
public void setProducts(List p) {
products = p;
}
*/
public List getProducts() {
products = pmd.getProductList();
return products;
}
public void increasePrice(int pct) {
ListIterator li = products.listIterator();
while (li.hasNext()) {
Product p = (Product) li.next();
/*
double newPrice = p.getPrice().doubleValue() * (100 + pct)/100;
p.setPrice(new Double(newPrice));
*/
pmd.increasePrice(p, pct);
}
}
}
springapp/war/WEB-INF/springapp-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<!--
- Application context definition for "springapp" DispatcherServlet.
-->
<beans>
<!-- Controller for the initial "Hello" page -->
<bean id="springappController" class="web.SpringappController">
<property name="productManager">
<ref bean="prodMan"/>
</property>
</bean>
<!-- Validator and Form Controller for the "Price Increase" page -->
<bean id="priceIncreaseValidator" class="bus.PriceIncreaseValidator"/>
<bean id="priceIncreaseForm" class="web.PriceIncreaseFormController">
<property name="sessionForm"><value>true</value></property>
<property name="commandName"><value>priceIncrease</value></property>
<property name="commandClass"><value>bus.PriceIncrease</value></property>
<property name="validator"><ref bean="priceIncreaseValidator"/></property>
<property name="formView"><value>priceincrease</value></property>
<property name="successView"><value>hello.htm</value></property>
<property name="productManager">
<ref bean="prodMan"/>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property>
<property name="url">
<value>jdbc:hsqldb:/home/trisberg/workspace/springapp/db/test</value>
</property>
<property name="username"><value>sa</value></property>
<property name="password"><value></value></property>
</bean>
<bean id="prodManDao" class="db.ProductManagerDaoJdbc">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="prodMan" class="bus.ProductManager">
<property name="productManagerDao">
<ref bean="prodManDao"/>
</property>
<!--
<property name="products">
<list>
<ref bean="product1"/>
<ref bean="product2"/>
<ref bean="product3"/>
</list>
</property>
-->
</bean>
<!--
<bean id="product1" class="bus.Product">
<property name="description"><value>Lamp</value></property>
<property name="price"><value>5.75</value></property>
</bean>
<bean id="product2" class="bus.Product">
<property name="description"><value>Table</value></property>
<property name="price"><value>75.25</value></property>
</bean>
<bean id="product3" class="bus.Product">
<property name="description"><value>Chair</value></property>
<property name="price"><value>22.79</value></property>
</bean>
-->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename"><value>messages</value></property>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.htm">springappController</prop>
<prop key="/priceincrease.htm">priceIncreaseForm</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix"><value>/WEB-INF/jsp/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
</beans>
springapp/src/tests/MockProductManagerDaoImpl.java
package tests;
import bus.Product;
import java.util.List;
import db.ProductManagerDao;
import bus.Product;
public class MockProductManagerDaoImpl implements ProductManagerDao {
private List products;
public void setProducts(List p) {
products = p;
}
public List getProductList() {
return products;
}
public void increasePrice(Product prod, int pct) {
double newPrice = prod.getPrice().doubleValue() * (100 + pct)/100;
prod.setPrice(new Double(newPrice));
}
}
springapp/src/tests/WEB-INF/springapp-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--
- Application context definition for "springapp" DispatcherServlet.
-->
<beans>
<bean id="springappController" class="web.SpringappController">
<property name="productManager">
<ref bean="prodMan"/>
</property>
</bean>
<bean id="prodManDao" class="tests.MockProductManagerDaoImpl">
<property name="products">
<list>
<ref bean="product1"/>
<ref bean="product2"/>
<ref bean="product3"/>
</list>
</property>
</bean>
<bean id="prodMan" class="bus.ProductManager">
<property name="productManagerDao">
<ref bean="prodManDao"/>
</property>
<!--
<property name="products">
<list>
<ref bean="product1"/>
<ref bean="product2"/>
<ref bean="product3"/>
</list>
</property>
-->
</bean>
<bean id="product1" class="bus.Product">
<property name="description"><value>Lamp</value></property>
<property name="price"><value>5.75</value></property>
</bean>
<bean id="product2" class="bus.Product">
<property name="description"><value>Table</value></property>
<property name="price"><value>75.25</value></property>
</bean>
<bean id="product3" class="bus.Product">
<property name="description"><value>Chair</value></property>
<property name="price"><value>22.79</value></property>
</bean>
</beans>
springapp/src/tests/TestProductManager .java
package tests;
import java.util.List;
import java.util.ArrayList;
import junit.framework.TestCase;
import db.ProductManagerDao;
import bus.ProductManager;
import bus.Product;
public class TestProductManager extends TestCase {
private ProductManager pm;
public void setUp() {
pm = new ProductManager();
Product p = new Product();
p.setDescription("Chair");
p.setPrice(new Double("20.50"));
ArrayList al = new ArrayList();
al.add(p);
p = new Product();
p.setDescription("Table");
p.setPrice(new Double("150.10"));
al.add(p);
/*
pm.setProducts(al);
*/
MockProductManagerDaoImpl pmdao = new MockProductManagerDaoImpl();
pmdao.setProducts(al);
pm.setProductManagerDao(pmdao);
pm.getProducts();
}
public void testGetProducs() {
List l = pm.getProducts();
Product p1 = (Product) l.get(0);
assertEquals("Chair", p1.getDescription());
Product p2 = (Product) l.get(1);
assertEquals("Table", p2.getDescription());
}
public void testIncreasePrice() {
pm.increasePrice(10);
List l = pm.getProducts();
Product p = (Product) l.get(0);
assertEquals(new Double("22.55"), p.getPrice());
p = (Product) l.get(1);
assertEquals(new Double("165.11"), p.getPrice());
}
}
- ››开发Android 日历教程
- ››开发学院总结 Win 8实用技巧大全
- ››开发学院原创教程:把win8的IE10放桌面上方法(非...
- ››Spring源码学习-含有通配符路径解析(上)
- ››开发者眼中的Windows Phone和Android
- ››开发学院教你用SQL 语句最快速清空MySQL 数据表的...
- ››开发一个自己的HTML在线编辑器(一)
- ››开发一个自己的HTML在线编辑器(二)
- ››开发者在App Store上赚的钱比在Android Market上多...
- ››开发者应深入学习的10个Android开源应用项目
- ››Spring MVC与JAX-RS比较与分析
- ››开发移动 Web Ajax 应用
更多精彩
赞助商链接