📄 netstoreservicefactory.java
字号:
package netstore.service;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.struts.action.PlugIn;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.config.ModuleConfig;
import netstore.framework.util.IConstants;
/**
* 实现工厂
* 初始化参数默认从ServiceContext上下文获取,否则类中默认指定值
* 注意此处使用了struts插件
*/
/**
* A factory for creating Netstore Service Implementations. The specific service
* to instantiate is determined from the initialization parameter of the
* ServiceContext. Otherwise, a default implementation is used.
*
* @see netstore.service.NetstoreDebugServiceImpl
*/
public class NetstoreServiceFactory implements INetstoreServiceFactory, PlugIn {
// Hold onto the servlet for the destroy method
private ActionServlet servlet = null;
// The default is to use the debug implementation
String serviceClassname = "netstore.service.NetstoreServiceImpl";
String serviceOrderClassname = "netstore.service.OrderServiceImpl";
String serviceCustClassname = "netstore.service.CustServiceImpl";
String serviceProdClassname = "netstore.service.ProdServiceImpl";
/**
* 上层类通过调用该方法,实际上就能得到业务代理实现类的对象 netstore.service.NetstoreServiceImpl
*/
public INetstoreService createService() throws ClassNotFoundException,
IllegalAccessException, InstantiationException {
// 以servlet中定义为准,如定义,则改写默认值
String className = servlet
.getInitParameter(IConstants.SERVICE_CLASS_KEY);
if (className != null) {
serviceClassname = className;
}
try {
// java反射机制
INetstoreService instance = (INetstoreService) Class.forName(
serviceClassname).newInstance();
instance.setServletContext(servlet.getServletContext());
return instance;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取客户服务类
*/
public CustServiceImpl createCustService() throws ClassNotFoundException,
IllegalAccessException, InstantiationException {
try {
// java反射机制
CustServiceImpl instance = (CustServiceImpl) Class.forName(
serviceCustClassname).newInstance();
instance.setServletContext(servlet.getServletContext());
return instance;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取产品服务类
*/
public ProdServiceImpl createProdService() throws ClassNotFoundException,
IllegalAccessException, InstantiationException {
try {
// java反射机制
ProdServiceImpl instance = (ProdServiceImpl) Class.forName(
serviceProdClassname).newInstance();
instance.setServletContext(servlet.getServletContext());
return instance;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取订单服务类
*/
public OrderServiceImpl createOrderService() throws ClassNotFoundException,
IllegalAccessException, InstantiationException {
try {
// java反射机制
OrderServiceImpl instance = (OrderServiceImpl) Class.forName(
serviceOrderClassname).newInstance();
instance.setServletContext(servlet.getServletContext());
return instance;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException {
// Store the servlet for later
// 加载servlet进来
this.servlet = servlet;
/*
* Store the factory for the application. Any Netstore service factory
* must either store itself in the ServletContext at this key, or extend
* this class and don't override this method. The Netstore application
* assumes that a factory class that implements the
* IStorefrtonServiceFactory is stored at the proper key in the
* ServletContext. 存储工厂实现于Servlet
*/
servlet.getServletContext().setAttribute(
IConstants.SERVICE_FACTORY_KEY, this);
}
public void destroy() {
// Do nothing for now
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -