Plugin
生命周期方法:init destroy
用户提供set方法,告诉ActionServlet设置属性
应用:
在struts启动时加载hibernate(读取hibernate配置文件,打开hibernate的sessionfactory)
1.环境:导入struts包及hibernate包
2.设计一个类:HibernatePlugin实现plugin接口
属性:String hibernateConfigFile(文件名)
读取hibernate配置文件
打开sessionFactory
3.在struts-config.xml配置文件中添加一对plugin标签,在plugin中加入子标签
导hibernate包
工程名-MyEclipse-Add Hibernate Capabilities...
package demo;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}
新建HibernatePlugin类
在HibernatePlugin类中,添加hibernateConfigFile属性,需要ActionServlet为该属性设置值,所以要提供该属性的set方法,在init方法中读取hibernate配置文件,打开SessionFactory,在destroy方法中关闭SessionFactory
package demo;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
public class HibernatePlugin implements PlugIn {
private String hibernateConfigFile=null;
@Override
public void destroy() {
System.out.println("in the destroy method");
//释放资源
HibernateSessionFactory.closeSession();
}
public String getHibernateConfigFile() {
return hibernateConfigFile;
}
public void setHibernateConfigFile(String hibernateConfigFile) {
this.hibernateConfigFile = hibernateConfigFile;
}
@Override
public void init(ActionServlet arg0, ModuleConfig arg1)
throws ServletException {
System.out.println("in the init method...");
//1.读取hibernate的配置文件
HibernateSessionFactory.setConfigFile(hibernateConfigFile);
//2.打开SessionFactory
HibernateSessionFactory.getSessionFactory();
System.out.println("init is over");
}
}
在struts-config.xml配置文件中添加一对plugin标签,在plugin中加入子标签
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foudation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="addStudentForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="sname" type="java.lang.String"></form-property>
<form-property name="major" type="java.lang.String"></form-property>
<form-property name="birth" type="java.sql.Date"></form-property>
<form-property name="score" type="java.lang.Float"></form-property>
</form-bean>
</form-beans>
<action-mappings>
<action path="/addStudentAction" type="demo.AddStudentAction" name="addStudentForm">
<exception type="demo.MyException" path="/Error.jsp" key="ex"></exception>
<forward name="addStudentSuccess" path="/WEB-INF/addStudent.jsp"></forward>
</action>
<action path="/studentAction" type="demo.StudentAction" name="addStudengtForm" parameter="method"/>
</action-mappings>
<message-resources parameter="ecception"></message-resources>
<plugin-in className="demo.HibernatePlugin">
<set-property property="hibernateConfigFile" value="hibernate.cfg.xml" />
</plugin-in>
</struts-config>