cess.execute(ctx);
}
}
Jakarta-Common-Chain使用笔记(3)
时间:2011-01-28 csdn博客 沈斌
也可以使用xml文件进行配置:新建chain-config.xml文件如下:
<catalog>
<chain name="sell-vehicle">
<command id="GetCustomerInfo" className="demo.chain.GetCustomerInfo" />
<command id="TestDriveVehicle" className="demo.chain.TestDriveVehicle" />
<command id="NegotiateSale" className="demo.chain.NegotiateSale" />
<command id="ArrangeFinancing" className="demo.chain.ArrangeFinancing" />
<command id="CloseSale" className="demo.chain.CloseSale" />
</chain>
</catalog>
新建SellVehicleContext.jar文件
package demo;
import org.apache.commons.chain.impl.ContextBase;
public class SellVehicleContext extends ContextBase ...{
private static final long serialVersionUID = 6954127190729021870L;
private String customerName;
public String getCustomerName() ...{
return customerName;
}
public void setCustomerName(String name) ...{
this.customerName = name;
}
}
测试类如下:
package demo;
import org.apache.commons.chain.Catalog;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.apache.commons.chain.config.ConfigParser;
import org.apache.commons.chain.impl.CatalogFactoryBase;
public class CatalogLoader ...{
private static final String CONFIG_FILE = "/demo/chain-config.xml";
private ConfigParser parser;
private Catalog catalog;
public CatalogLoader() ...{
parser = new ConfigParser();
}
public Catalog getCatalog() throws Exception ...{
if (catalog == null) ...{
parser.parse(this.getClass().getResource(CONFIG_FILE));
}
catalog = CatalogFactoryBase.getInstance().getCatalog();
return catalog;
}
public static void main(String[] args) throws Exception ...{
CatalogLoader loader = new CatalogLoader();
Catalog sampleCatalog = loader.getCatalog();
Command command = sampleCatalog.getCommand("sell-vehicle");
Context ctx = new SellVehicleContext();
command.execute(ctx);
}
}
Chain简明手册:http://blog.donews.com/foxgem/archive/2005/08/05/495938.aspx |