📄 esimpleplugin.java
字号:
package com.esimple.framework.web.init;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.io.UrlResource;
import com.esimple.framework.bean.*;
import com.esimple.framework.util.StringUtils;
/**
* Initializes Spring bean factory and makes it available through the servlet
* context
*
*@author steven
*@version 0.9
*/
public final class ESimplePlugIn implements PlugIn {
/** The key the factory is stored under in the servlet context */
public final static String BEAN_FACTORY = "BEAN_FACTORY";
/** The logging instance */
private Log log = LogFactory.getLog(this.getClass());
/** The location of the Spring bean configuration file */
private String beansConfig = "/WEB-INF/spring-beans.xml";
private String defaultDao = "defaultDAO";
/** The servlet context */
private ServletContext context = null;
/** The bean factory */
private DefaultListableBeanFactory factory = null;
/**
* Gets the path of the Spring bean configuration
*
*@return The beansConfig value
*/
public String getDefaultDao() {
return (this.defaultDao);
}
/**
* Sets the path of the Spring bean configuration
*
*@param beansConfig The path from the servlet context
*/
public void setDefaultDao(String defaultDao) {
this.defaultDao = defaultDao;
}
/**
* Gets the path of the Spring bean configuration
*
*@return The beansConfig value
*/
public String getBeansConfig() {
return (this.beansConfig);
}
/**
* Sets the path of the Spring bean configuration
*
*@param beansConfig The path from the servlet context
*/
public void setBeansConfig(String beansConfig) {
this.beansConfig = beansConfig;
}
/** Gracefully remove the Spring bean factory */
public void destroy() {
log.info("Destroying spring plugin");
context.removeAttribute(BEAN_FACTORY);
context = null;
factory = null;
}
/**
* Initialize and load the Spring bean factory
*
*@param servlet The ActionServlet for this web application
*@param config The ApplicationConfig for our owning module
*@exception ServletException if we cannot configure ourselves correctly
*/
public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException {
log.info("Initializing spring plug in from '" + beansConfig + "'");
context = servlet.getServletContext();
context.setAttribute("defaultDao", defaultDao);
factory = (DefaultListableBeanFactory) context.getAttribute(BEAN_FACTORY);
if (factory == null) {
List bxf = new ArrayList();
try{
String[] paths = StringUtils.split(beansConfig,",",true,true);
for ( int i = 0; i< paths.length; i++){
bxf.add(
new UrlResource(
servlet.getServletContext().getResource( paths[i] )
)
);
}
}catch(Exception e) {
throw new ServletException(e);
}
BeanContainerFactory bcf = BeanContainerFactory.getInstance();
bcf.initFromURL(bxf);
BeanContainer bc = BeanContainerFactory.getBeanContainer();
context.setAttribute(BEAN_FACTORY, bc.getFactory());
}//if end
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -