Webwork版本2.1.7
首先,需要配置web.xml,webwork的IOC容器可以区分三种作用域requet,session,application,我们可以使用Listener和filter进行配置
<!--
webwork IOC 使用的listener
-->
<
listener
>
<
listener-class
>
com.opensymphony.webwork.lifecycle.ApplicationLifecycleListener
</
listener-class
>
</
listener
>
<
listener
>
<
listener-class
>
com.opensymphony.webwork.lifecycle.SessionLifecycleListener
</
listener-class
>
</
listener
>
<!--
webwork IOC 使用的fitler
-->
<
filter
>
<
filter-name
>
container
</
filter-name
>
<
filter-class
>
com.opensymphony.webwork.lifecycle.RequestLifecycleFilter
</
filter-class
>
</
filter
>
<
filter-mapping
>
<
filter-name
>
container
</
filter-name
>
<
url-pattern
>
/*
</
url-pattern
>
</
filter-mapping
>
我们还要修改action的interceptor栈,以便让所有实现Aware接口的action将资源应用到action,其中必须要引用component interceptor,否则调用依赖资源的方法时候会碰到空指针异常
<
action
name
="testIOC"
class
="ch5.example2IOC.TestIOC"
>
<
default-interceptor-ref
name
="completeStack"
></
default-interceptor-ref
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
我们调用completeStack,已经包含了component interceptor了
创建组件接口:
package
ch5.example2IOC;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
org.hibernate.Session;
import
org.hibernate.SessionFactory;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
interface
PersistenceManager
...
{
void setHibernateSessionFactory (HibernateSessionFactory hibernateSessionFactory);
void setSessionFactory(SessionFactory sessionFactory);
Session getSession();
void testIOC();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
package
ch5.example2IOC;
import
com.opensymphony.webwork.lifecycle.SessionLifecycleListener;
import
org.hibernate.SessionFactory;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
interface
HibernateSessionFactory
...
{
SessionFactory getSessionFactory();
}
实现类:
package
ch5.example2IOC;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
org.hibernate.SessionFactory;
import
org.hibernate.cfg.Configuration;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
com.opensymphony.xwork.interceptor.component.Disposable;
import
com.opensymphony.xwork.interceptor.component.Initializable;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
HibernateSessionFactoryImpl
implements
HibernateSessionFactory,Initializable,Disposable
...
{
SessionFactory sessionFactory;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void dispose() ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try ...{
sessionFactory.close();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (Exception e) ...{
e.printStackTrace();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void init() ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try...{
Configuration configuration=new Configuration();
sessionFactory=configuration.configure().buildSessionFactory();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
}catch(Throwable e)...{//此处一定要是Throwable,否则会漏掉NoClassDefFoiundError和其他Error子类
e.printStackTrace();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public SessionFactory getSessionFactory() ...{
return sessionFactory;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
package
ch5.example2IOC;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
org.hibernate.Session;
import
org.hibernate.SessionFactory;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
com.opensymphony.xwork.interceptor.component.Disposable;
import
com.opensymphony.xwork.interceptor.component.Initializable;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
PersistenceManagerImpl
implements
PersistenceManager,HibernateSessionFactoryAware,Initializable,Disposable
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setHibernateSessionFactory(HibernateSessionFactory hibernateSessionFactory) ...{
this.hibernateSessionFactory=hibernateSessionFactory;
}
HibernateSessionFactory hibernateSessionFactory;
SessionFactory sessionFactory;
Session session;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Session getSession() ...{
return session;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void testIOC() ...{
System.out.println(this.sessionFactory);
System.out.println(this.session);
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void dispose() ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(session!=null&&session.isOpen())...{
session.close();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setSessionFactory(SessionFactory sessionFactory) ...{
this.sessionFactory=sessionFactory;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void init() ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try ...{
session=this.hibernateSessionFactory.getSessionFactory().openSession();
System.out.println(session);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (Exception e) ...{
e.printStackTrace();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
创建enabler接口(Aware接口)
package
ch5.example2IOC;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
interface
PersistenceManagerAware
...
{
void setPersistenceManager(PersistenceManager persistenceManager);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
package
ch5.example2IOC;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
org.hibernate.SessionFactory;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
interface
HibernateSessionFactoryAware
...
{
void setHibernateSessionFactory(HibernateSessionFactory hibernateSessionFactory);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
指明要注入HibernateSessionFactory
定义components.xml配置文件,放在classpath下
<?
xml version="1.0" encoding="UTF-8"
?>
<
components
>
<
component
>
<
scope
>
session
</
scope
>
<
class
>
ch5.example2IOC.PersistenceManagerImpl
</
class
>
<
enabler
>
ch5.example2IOC.PersistenceManagerAware
</
enabler
>
</
component
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
component
>
<
scope
>
application
</
scope
>
<
class
>
ch5.example2IOC.HibernateSessionFactoryImpl
</
class
>
<
enabler
>
ch5.example2IOC.HibernateSessionFactoryAware
</
enabler
>
</
component
>
</
components
>
Action:
package
ch5.example2IOC;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
java.util.Map;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
com.opensymphony.webwork.interceptor.SessionAware;
import
com.opensymphony.xwork.ActionSupport;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
TestIOC
extends
ActionSupport
implements
HibernateSessionFactoryAware,PersistenceManagerAware
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setPersistenceManager(PersistenceManager persistenceManager) ...{
this.s1=persistenceManager;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setHibernateSessionFactory(HibernateSessionFactory hibernateSessionFactory) ...{
this.s=hibernateSessionFactory;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
HibernateSessionFactory s=null;
PersistenceManager s1=null;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String execute() throws Exception ...{
s1.setHibernateSessionFactory(s);
System.out.println(s1);
System.out.println(s);
return NONE;
}
}
运行action可以看到,HibernateSessionFactory已经注入了
后记:Webwork只提供了接口注入,而Sprin框架还提供了set注入和构造注入,希望webwork以后会改进
Webwork2.4似乎在ioc方面有很很大的变动,我在2.4上没有调试通过,我发现lifecycle包有了变动,至于2.4
如何,我也没有继续研究,没有必要,现在ioc基本都是spring的天下,如果有朋友在2.4上有了进展,请通知
我啊 ,谢谢了