反射机制剖析(二): 利用抽象工厂实现BeanFactory

BeanFactory保存在ServletContext中,ServletContext充当Web和业务逻辑的桥梁

 

统一工厂创建和管理对象

利用抽象工厂+单例+反射实现

BeanFactory.java

/**
 * 抽象工厂
 * 采用单例
 * @author TCH
 *
 */
publicclass BeanFactory {
privatestatic BeanFactory instance = new BeanFactory();
 
//系统缺省配置文件名称
privatefinal String beansConfigFile = "beans-config.xml";
 
Documentdoc = null;
 
/*
 * key=配置文件中的id值
 * value=对应了改Id的对象
 */
MapserviceMap = new HashMap<String, Object>();
 
MapdaoMap = new HashMap<String, Object>();
 
privateBeanFactory(){
//加载beans-config.xml文件
try{
doc= newSAXReader().read(Thread.currentThread().getContextClassLoader().getResourceAsStream(beansConfigFile));
}catch (DocumentException e) {
e.printStackTrace();
thrownew RuntimeException();
}
}
 
publicstatic BeanFactory getInstance(){
returninstance;
}
 
/**
 * 根据产品编号取得具体的Service产品
 * @param beanId
 * @return
 */
publicsynchronized Object getServiceObject(Class c){
//如果存在对应对象实例,返回
if(serviceMap.containsKey(c.getName())) {
returnserviceMap.get(c.getName());
}
ElementbeanElt = (Element)doc.selectSingleNode("//service[@id=\"" +c.getName() +"\"]");
StringclassName = beanElt.attributeValue("class");
System.out.print(className);
 
ObjectserviceObj = null;
try{
serviceObj= Class.forName(className).newInstance();
//利用动态代理模式创建代理
TransactionHandlertransactionHandler = new TransactionHandler();
serviceObj= transactionHandler.newProxyInstance(serviceObj);
 
serviceMap.put(c.getName(),serviceObj);
}catch (Exception e) {
e.printStackTrace();
thrownew RuntimeException();
 
}
returnserviceObj;
}
 
/**
 * 根据产品编号取得具体的Dao产品
 * @param beanId
 * @return
 */
publicsynchronized Object getDaoObject(Class c){
//如果存在对应对象实例,返回
if(daoMap.containsKey(c.getName())) {
returndaoMap.get(c.getName());
}
ElementbeanElt = (Element)doc.selectSingleNode("//dao[@id=\"" +c.getName() +"\"]");
StringclassName = beanElt.attributeValue("class");
System.out.print(className);
 
ObjectdaoObj = null;
try{
daoObj= Class.forName(className).newInstance();
daoMap.put(c.getName(),daoObj);
}catch (Exception e) {
e.printStackTrace();
thrownew RuntimeException();
 
}
returndaoObj;
}
publicstatic void main(String[] args){
System.out.print(BeanFactory.class);
}
}


 

ServiceDao层的接口和实现类配置信息

beans-config.xml

<?xmlversion="1.0" encoding="UTF-8"?>
<beans>
<service-class>
<serviceid="com.java.drp.basedata.manager.ItemManager"class="com.java.drp.basedata.manager.ItemManagerImpl"/>
<serviceid="com.java.drp.flowcard.manager.FlowCardManager"class="com.java.drp.flowcard.manager.FlowCardManagerImp"/>
<serviceid="com.java.drp.statreport.manager.StatReportManager"class="com.java.drp.statreport.manager.StatReportManagerImp"/>
</service-class>
<dao-class>
<daoid="com.java.drp.basedata.dao.ItemDao"class="com.java.drp.basedata.dao.ItemDao4Oracle"/>
<daoid="com.java.drp.flowcard.dao.FlowCardDao"class="com.java.drp.flowcard.dao.FlowCardDao4Oracle"/>
</dao-class>
</beans>


 

抽象Servlet公共部分:command、User、BeanFactory

BaseServlet.java

publicclass BaseServlet extends HttpServlet {
 
privateString command;
 
privateUser user;
 
privateBeanFactory beanFactory;
 
@Override
protectedfinal void service(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
command= request.getParameter("command");
user= (User)request.getSession().getAttribute("user_info");
super.service(request,response);
}
 
protectedString getCommand() {
returncommand;
}
 
protectedUser getCurrentUser() {
returnuser;
}
 
protectedBeanFactory getBeanFactory() {
//从ServletContext中取得BeanFactory
beanFactory= (BeanFactory)this.getServletContext().getAttribute("beanFactory");
returnbeanFactory;
}
}


 

应用:物料维护Servlet,使用BeanFactory创建对象

FlowCardServlet.java

/**
 * 物料维护Servlet
 * @author Administrator
 *
 */
publicclass FlowCardServlet extends BaseServlet {
 
privateFlowCardManager flowCardManager;
 
@Override
publicvoid init() throws ServletException {
flowCardManager= (FlowCardManager)getBeanFactory().getServiceObject(FlowCardManager.class);
//                TransactionHandlertransactionHandler = new TransactionHandler();
//                //对目标生成代理对象
//                flowCardManager= (FlowCardManager)transactionHandler.newProxyInstance(flowCardManager);
}
 
@Override
protectedvoid doGet(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
if(Constants.DEL.equals(getCommand())) {
del(request,response);
}else {        
search(request,response);        
}
}
 
@Override
protectedvoid doPost(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
doGet(request,response);
}
 
/**
 * 删除
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
privatevoid del(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
String[]flowCardVouNos = request.getParameterValues("selectFlag");
flowCardManager.delFlowCard(flowCardVouNos);
response.sendRedirect(request.getContextPath()+ "/servlet/flowcard/FlowCardServlet");
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值