SSH 回顾二 特殊情况下 获取Spring 注入对象
来源:互联网 发布:淘宝红包是现金吗 编辑:程序博客网 时间:2024/06/05 12:06
特殊情况一:ActionForm
思路有两种:
第一种:将你需要注入的参数设置为static的,然后将设值方法setXXX的返回值设置为非void型,比如你的
protected IDaoService daoService;
改为
protected static IDaoService daoService;
设值方法setXXX,为:
public static boolean setDaoService(IDaoService daoService) {
LookuserForm.daoService = daoService;
return true;
}
然后在spring配置文件中设值:
<bean id="BaseBoolean"
class="org.shan.student.form.LookuserForm"
factory-method="setDaoService"
depends-on="daoService">
<constructor-arg ref="daoService"></constructor-arg>
</bean>
这样就实现了daoService的注射。class就是你的ActionForm的类,factory-method就是你的非void型注入对象的方法,depends-on就是你要注入对象使用的bean,constructor-arg ref="XXX"也是一样了。
第二种:重写ActionForm的public void setServlet(ActionServlet servlet)方法。
public void setServlet(ActionServlet servlet) {
ServletContext context = servlet.getServletContext();
ApplicationContext ctx =WebApplicationContextUtils.getWebApplicationContext(context);
LookuserForm temp = (LookuserForm) ctx.getBean("LookuserForm");
this.daoService = temp.getDaoService ();
super.setServlet(servlet);
}
特殊情况二:servlet中获取在Servlet(或者Filter,或者Listener)中使用spring的IOC容器默认情况下Servlet容器创建spring容器对象,注入到servletContext中,servletContext对象又是注入到session对象中,session对象又是注入到request对象中,request对象又是注入到servlet对象中,(其实不是很标准的注入,是传参数,或者对属性直接付值)。层层依赖可以得到spring容器对象。
ServletContext servletContext = request.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
UsersManager um = (UsersManager)ctx.getBean( "UserManager");
- SSH 回顾二 特殊情况下 获取Spring 注入对象
- 特殊情况下ActionForm,Servlet..下Spring如何注入对象
- JSP 获取Spring 注入对象
- 通过Spring注入,获取Service对象实例
- java监听器:ServletContextListener获取spring注入对象
- 获取spring注入的service对象
- JSP 获取Spring注入的对象
- 获取span里面的值(特殊情况下 )
- Spring初始化时,没注入时获取注入,对象webapplicationcontext
- spring回顾系列:依赖注入
- Ajax获取SSH框架下JSON对象
- SSH下使用Spring注解自动注入bean
- SSH下使用Spring注解自动注入bean
- spring简单回顾二
- 在ServletContextListener实现类中获取spring注入对象
- 在ServletContextListener实现类中获取spring注入对象
- spring不通过注入的方式获取对象(BeanFactoryAware \ApplicationContextAware )
- 在ServletContextListener实现类中获取spring注入对象
- PYTHON GUI模块之WXPYTHON
- SNMP介紹及命令
- Lapack参考资料
- linux下的压缩与解压
- javascript中的dom对象
- SSH 回顾二 特殊情况下 获取Spring 注入对象
- SQL Server 应用程序中的高级SQL注入(1)
- 在Linux控制台下使用libjpeg显示JPEG图像在framebuffer上
- 工程实践QT部分之二(准备工作续以及程序编写)
- 什么是软件框架
- 一次传说中的ORA-07445问题的解决
- Zabbix agent自动安装脚本
- SQL Server应用程序中的高级SQL注入(2)
- 获取系统时间(精确到微秒),驱动中KeQueryPerformanceCounter,Win32程序QueryPerformanceCounter