获得spring里注册Bean的四种方法,特别是第三种方法,简单:
一:方法一(多在struts框架中)继承BaseDispatchAction
- importcom.mas.wawacommunity.wap.service.UserManager;
-
publicclassBaseDispatchActionextendsDispatchAction{
-
-
protectedWebApplicationContextctx;
-
protectedUserManageruserMgr;
-
-
protectedfinalObjectgetBean(StringbeanName){
-
returnctx.getBean(beanName);
- }
-
protectedActionForwardunspecified(ActionMappingmapping,ActionFormform,
- javax.servlet.http.HttpServletRequestrequest,
- javax.servlet.http.HttpServletResponseresponse){
-
returnmapping.findForward("index");
- }
-
publicvoidsetServlet(ActionServletservlet){
-
this.servlet=servlet;
-
this.ctx=WebApplicationContextUtils.getWebApplicationContext(servlet.getServletContext());
-
this.userMgr=(UserManager)getBean("userManager");
- }
- }
二:方法二实现BeanFactoryAware
一定要在spring.xml中加上:
<bean id="serviceLocator" class="项目中ServiceLocator的类路径比如:com.am.oa.commons.service.SpringContextUtil " singleton="true" />
当对serviceLocator实例时就自动设置BeanFactory,以便后来可直接用beanFactory
- publicclassServiceLocatorimplementsBeanFactoryAware{
-
privatestaticBeanFactorybeanFactory=null;
-
privatestaticServiceLocatorservlocator=null;
-
publicvoidsetBeanFactory(BeanFactoryfactory)throwsBeansException{
-
this.beanFactory=factory;
- }
-
publicBeanFactorygetBeanFactory(){
-
returnbeanFactory;
- }
-
publicstaticServiceLocatorgetInstance(){
-
if(servlocator==null)
-
servlocator=(ServiceLocator)beanFactory.getBean("serviceLocator");
-
returnservlocator;
- }
-
-
publicstaticObjectgetService(StringservName){
-
returnbeanFactory.getBean(servName);
- }
-
-
publicstaticObjectgetService(StringservName,Classclazz){
-
returnbeanFactory.getBean(servName,clazz);
- }
- }
action调用:
- publicclassUserActionextendsBaseActionimplementsAction,ModelDriven{
-
privateUsersuser=newUsers();
-
protectedServiceLocatorservice=ServiceLocator.getInstance();
-
UserServiceuserService=(UserService)service.getService("userService");
-
publicStringexecute()throwsException{
-
returnSUCCESS;
- }
-
publicObjectgetModel(){
-
returnuser;
- }
-
publicStringgetAllUser(){
- HttpServletRequestrequest=ServletActionContext.getRequest();
-
Listls=userService.LoadAllObject(Users.class);
-
request.setAttribute("user",ls);
-
this.setUrl("/yonghu.jsp");
-
returnSUCCESS;
- }
- }
三:方法三实现ApplicationContextAware
一定要在spring.xml中加上:
<bean id="SpringContextUtil " class="项目中applicationContext的类路径比如:com.am.oa.commons.service.SpringContextUtil " singleton="true" />
当对SpringContextUtil 实例时就自动设置applicationContext,以便后来可直接用applicationContext
- publicclassSpringContextUtilimplementsApplicationContextAware{
-
privatestaticApplicationContextapplicationContext;
-
-
publicvoidsetApplicationContext(ApplicationContextapplicationContext)throwsBeansException{
- SpringContextUtil.applicationContext=applicationContext;
- }
-
-
publicstaticApplicationContextgetApplicationContext(){
-
returnapplicationContext;
- }
-
-
publicstaticObjectgetBean(Stringname)throwsBeansException{
-
returnapplicationContext.getBean(name);
- }
-
-
publicstaticObjectgetBean(Stringname,ClassrequiredType)throwsBeansException{
-
returnapplicationContext.getBean(name,requiredType);
- }
-
-
publicstaticbooleancontainsBean(Stringname){
-
returnapplicationContext.containsBean(name);
- }
-
-
publicstaticbooleanisSingleton(Stringname)throwsNoSuchBeanDefinitionException{
-
returnapplicationContext.isSingleton(name);
- }
-
-
publicstaticClassgetType(Stringname)throwsNoSuchBeanDefinitionException{
-
returnapplicationContext.getType(name);
- }
-
-
publicstaticString[]getAliases(Stringname)throwsNoSuchBeanDefinitionException{
-
returnapplicationContext.getAliases(name);
- }
- }
action调用:
- packagecom.anymusic.oa.webwork;
-
importjava.util.List;
-
importjava.util.Map;
-
importjavax.servlet.http.HttpServletRequest;
-
importcom.anymusic.oa.commons.service.ServiceLocator;
-
importcom.anymusic.oa.hibernate.pojo.Role;
-
importcom.anymusic.oa.hibernate.pojo.Users;
-
importcom.anymusic.oa.spring.IUserService;
-
importcom.opensymphony.webwork.ServletActionContext;
-
importcom.opensymphony.xwork.Action;
-
importcom.opensymphony.xwork.ActionContext;
-
importcom.opensymphony.xwork.ModelDriven;
-
publicclassUserActionextendsBaseActionimplementsAction,ModelDriven{
-
privateUsersuser=newUsers();
-
-
UserServiceuserService=(UserService)SpringContextUtil.getBean("userService");
-
publicStringexecute()throwsException{
-
returnSUCCESS;
- }
-
publicObjectgetModel(){
-
returnuser;
- }
-
publicStringgetAllUser(){
- HttpServletRequestrequest=ServletActionContext.getRequest();
-
Listls=userService.LoadAllObject(Users.class);
-
request.setAttribute("user",ls);
-
this.setUrl("/yonghu.jsp");
-
returnSUCCESS;
- }
- }
四.通过servlet 或listener设置spring的ApplicationContext,以便后来引用.
注意分别extends ContextLoaderListener和ContextLoaderServlet .然后就可用SpringContext来getBean.
覆盖原来在web.xml中配置的listener或servlet.
- publicclassSpringContext{
-
privatestaticApplicationContextapplicationContext;
- */
-
publicvoidsetApplicationContext(ApplicationContextapplicationContext)throwsBeansException{
- SpringContextUtil.applicationContext=applicationContext;
- }
-
-
publicstaticApplicationContextgetApplicationContext(){
-
returnapplicationContext;
- }
- */
-
publicstaticObjectgetBean(Stringname)throwsBeansException{
-
returnapplicationContext.getBean(name);
- }
- }
-
publicclassSpringContextLoaderListenerextendsContextLoaderListener{
-
publicvoidcontextInitialized(ServletContextEventevent){
-
super.contextInitialized(event);
- SpringContext.setApplicationContext(
- WebApplicationContextUtils.getWebApplicationContext(event.getServletContext())
- );
- }
- }
-
publicclassSpringContextLoaderServletextendsContextLoaderServlet{
-
privateContextLoadercontextLoader;
-
publicvoidinit()throwsServletException{
-
this.contextLoader=createContextLoader();
-
SpringContext.setApplicationContext(this.contextLoader.initWebApplicationContext(getServletContext()));
- }
- }
自己使用:
1.如果applicationContext文件是在src的根目录下:
ApplicationContext apt= new FileSystemXmlApplicationContext("src/applicationContext.xml");
apt.getBean("ID");
可以取得
2.如果文件是在WebRoot/WEB-INF下可以用
ApplicationContext applicationContext= new FileSystemXmlApplicationContext("../applicationContext.xml");
取得
3.如果是在servlet中取可用
ServletContext application = this.getServletContext();
//获取spring上下文信息 为servlet使用
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(application);
IDicService dicService = (IDicService)wac.getBean("dicService");
分享到:
相关推荐
《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+Spring+Ajax》通过对SSH中的各种技术循序渐进地讲解,使读者尽快掌握开发基于SSH的Web程序的方法。《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+...
《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+Spring+Ajax》通过对SSH中的各种技术循序渐进地讲解,使读者尽快掌握开发基于SSH的Web程序的方法。《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+...
《跟我学Java Web》通过对Java Web开发中所运用到的各种技术循序渐进地进行讲解,使读者能尽快掌握开发Web应用程序的方法。《跟我学Java Web》内容包括搭建Web开发环境、HTML相关技术基础知识、JavaScript相关技术...
《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+Spring+Ajax》通过对SSH中的各种技术循序渐进地讲解,使读者尽快掌握开发基于SSH的Web程序的方法。《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+...
《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+Spring+Ajax》通过对SSH中的各种技术循序渐进地讲解,使读者尽快掌握开发基于SSH的Web程序的方法。《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+...
《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+Spring+Ajax》通过对SSH中的各种技术循序渐进地讲解,使读者尽快掌握开发基于SSH的Web程序的方法。《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+...
《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+Spring+Ajax》通过对SSH中的各种技术循序渐进地讲解,使读者尽快掌握开发基于SSH的Web程序的方法。《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+...
Spring 的开发包:..............................................................................................................6 1.3.2 创建 web 工程引入相应 jar 包:........................................
SSH笔记-静态工厂方法配置bean和实例工厂方法配置bean
《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+Spring+Ajax》通过对SSH中的各种技术循序渐进地讲解,使读者尽快掌握开发基于SSH的Web程序的方法。《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+...
文件数据存储在Blob类型的FILE_CONTENT表字段上,在Spring中采用OracleLobHandler来处理Lob字段(包括Clob和Blob),由于在程序中不需要引用到oracle数据驱动程序的具体类且屏蔽了不同数据库处理Lob字段方法上的...
《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+Spring+Ajax》通过对SSH中的各种技术循序渐进地讲解,使读者尽快掌握开发基于SSH的Web程序的方法。《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+...
本书通过对SSH中的各种技术循序渐进地讲解,使读者尽快掌握开发基于SSH的Web程序的方法。本书内容包括Web客户端技术、JSP/Servlet技术、Struts 2(拦截器、类型转换、输入校验、上传和下载文件、Struts 2的各种标签...
Java局域网通信——飞鸽传书源代码,大家都知道VB版、VC版还有Delphi版的飞鸽传书软件,但是Java版的确实不多,因此这个Java文件传输实例不可错过,Java网络编程技能的提升很有帮助。 Java聊天程序,包括服务端和...
Java局域网通信——飞鸽传书源代码,大家都知道VB版、VC版还有Delphi版的飞鸽传书软件,但是Java版的确实不多,因此这个Java文件传输实例不可错过,Java网络编程技能的提升很有帮助。 Java聊天程序,包括服务端和...
ssh 框架整合的实例。 applicationContext.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop=...
主要包括Java Web开发环境、JSP语法、JSP内置对象、Java Bean技术、Servlet技术、EL与JSTL标签库、数据库应用开发、初识Struts2基础、揭密Struts2高级技术、Hib锄劬e技术入门、Hibernate高级应用、Spring核心之IoC、...
采用在web.xml中加载spring配置文件的方法降低struts和spring的耦合度,使用自定义的MyDelegatingRequestProcessor类来代替spring的DelegatingRequestProcessor类来解决spring中action的bean的重复配置问题。...
《跟我学Java Web》通过对Java Web开发中所运用到的各种技术循序渐进地进行讲解,使读者能尽快掌握开发Web应用程序的方法。《跟我学Java Web》内容包括搭建Web开发环境、HTML相关技术基础知识、JavaScript相关技术...