s2sh中beanFactory的初始化
来源:互联网 发布:band of brothers 知乎 编辑:程序博客网 时间:2024/05/17 01:52
在s2sh中web.xml文件中的如下语句
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>
把Spring中的beanfactory初始化,并且放入了servletContext中,避免Struts2在每个action中初始beanfactory(耗资源)进行数据库的操作,而Struts2的action如何取到该初始化好的beanfactory呢,笔者总结了如下三个方法:
方法1:
public class LoginAction extends ActionSupport implements ServletContextAware {
....(程序其他代码)
private ServletContext servletContext;
public String execute() {
....(程序其他代码)
BeanFactory factory = WebApplicationContextUtils
.getRequiredWebApplicationContext(servletContext);
UserManager userManager = (UserManager) factory.getBean("userManager");
....(程序其他代码)
return SUCCESS;
}
@Override
public void setServletContext(ServletContext arg0) {
// TODO Auto-generated method stub
servletContext = arg0;
}
}
Action实现ServletContextAware 接口,复写setServletContext方法,在Struts2掉用此Action时会自动将ServletContext注入到(setServletContext方法)action类中,之后BeanFactory factory = WebApplicationContextUtils
.getRequiredWebApplicationContext(servletContext);语句可以得到BeanFactory;
方法2:
在Action类不实现ServletContextAware 接口,在execute方法中加入ServletContext servletC = ServletActionContext.getServletContext();语句就可以,即从ServletActionContext中直接把ServletContext取出来
public class LoginAction extends ActionSupport {
....(程序其他代码)
public String execute() {
....(程序其他代码)
ServletContext servletC = ServletActionContext.getServletContext();
BeanFactory factory = WebApplicationContextUtils
.getRequiredWebApplicationContext(servletC);
UserManager userManager = (UserManager) factory.getBean("userManager");
....(程序其他代码)
return SUCCESS;
}
}
方法三:
把Action交给Spring的ioc容器管理,那么在action类中就不会有beanFactory生成了
步骤如下:
在applicationContext.xml文件中加入action的bean
<bean name="loginAction" class="action.LoginAction" scope="prototype">
<property name="userManager" ref="userManager"/>
</bean>
Scope最好设成prototype,避免action的线程安全问题
在struts.xml文件中,action要按如下方式申明:
<package name="user" namespace="/" extends="struts-default">
<action name="login" class="loginAction">
<result name="success">/success.jsp</result>
<result name="input">/login.jsp</result>
<result name="error">/failed.jsp</result>
</action>
</package>
<action name="login" class="loginAction">中的class要与bean中的name相对应
最后,在action中的代码就是很简单的(当然,其中的setUserManager方法别忘了)
import manager.UserManager;
import bean.User;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String password;
private UserManager userManager;
private String username;
public String execute() {
User user = new User();
user.setPassword(password);
user.setUsername(username);
userManager.addUser(user);
return SUCCESS;
}
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
public void setUsername(String username) {
this.username = username;
}
}
- s2sh中beanFactory的初始化
- ApplicationContext和BeanFactory初始化的重大差别
- ApplicationContext的初始化和BeanFactory的初始化之间的区别
- BeanFactory中Bean的生命周期
- BeanFactory中Bean的生命周期
- BeanFactory中bean的生命周期
- 【Spring4揭秘 BeanFactory】BeanFactory中Bean的实例化过程
- BeanFactory-ApplicationContext初始化Bean
- S2SH中web.xml的配置
- s2sh中getSession()关闭的方法
- Spring 源码阅读 BeanFactory(三) 对象的初始化 singleton 草稿
- Spring中BeanFactory产生Bean的方法
- Spring中BeanFactory产生Bean的方法
- Spring中BeanFactory产生Bean的方法
- Spring中BeanFactory配置文件的元素解析
- Spring中BeanFactory与ApplicationContext的区别
- spring 中beanFactory和ApplicationContext的区别
- spring 中ApplicationContext与beanfactory的区别
- linux 中mdelay() 与msleep()的区别
- I/O多路复用模式与线程并发模式
- NSScanner类的基本用法
- 纪念Google Code 20150825 ReadOnly
- android 自定义图表学习集合
- s2sh中beanFactory的初始化
- 网络
- eclipseRCP深入浅出(学习总结)2015.08.25
- 【java关键字-Thread】Thread.Join()的用法,及其实现线程顺序执行的原理
- 一段datepickerdialog与自定义Timepickerdialog限定时间范围的逻辑
- insert all/first 使用与区别简介
- BeautifulSoup
- 矩阵 ACdream1213 Matrix Multiplication
- Linux下VIM文本内容替换