`

【转】在Servlet中访问Spring管理下的bean

阅读更多

提供一个思路,仅做参考
web.xml:

 

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:springbeans.xml</param-value>
</context-param>
<listener>
       <listener-class>lnx.base.InitPlatformLoader</listener-class></listener>

 

 


InitPlatformLoader.java :

 


package lnx.base;

import javax.servlet.ServletContextEvent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 平台初始化类
 */
public class InitPlatformLoader extends ContextLoaderListener {
    protected final Log logger = LogFactory.getLog(getClass());

    public void contextInitialized(ServletContextEvent event) {
        logger.info("初始化平台...");
        super.contextInitialized(event);
        String path = event.getServletContext().getRealPath("/");
        logger.debug("系统部署根路径:" + path);
        Platform.getInstance().setWebPath(path);
        // 初始化Support中Spring的CTX
        ApplicationContext ctx = WebApplicationContextUtils
            .getRequiredWebApplicationContext(event.getServletContext());
        Platform.getInstance().setApplicationContext(ctx);
        logger.info("初始化完成...");
    }
}

 

Platform.java:

 

package lnx.base;

import org.springframework.context.ApplicationContext;
import org.springframework.util.Assert;

/**
* 平台,为各级提供spring的getbean的方法
*
* @author Feng
*/
public class Platform {
    private static Platform ourInstance = new Platform();

    public static Platform getInstance() {
        return ourInstance;
    }

    private Platform() {
    }

    private ApplicationContext applicationContext = null;

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    /**
     * 根据BeanName获得Bean
     *
     * @param beanName sping配置的id
     * @return 符合条件的bean
     */
    public Object getBean(String beanName) {
        Assert.hasText(beanName);
        return this.applicationContext.getBean(beanName);
    }

    // 平台部署的根物理路径
    private String WebPath;

    /**
     * 获得平台部署的根物理路径
     *
     * @return 根物理路径
     */
    public String getWebPath() {
        return WebPath;
    }

    /**
     * 设置根物理路径
     *
     * @param webPath 根物理路径
     */
    public void setWebPath(String webPath) {
        WebPath = webPath;
    }
}

 


用法:

Platform.getInstance().getBean("bean的name");

 

 

来自:http://bbs.langsin.com/thread-4286-1-89.html 非常感谢!

分享到:
评论

相关推荐

    在Servlet直接获取Spring框架中的Bean.docx

    介绍了在Servlet直接获取Spring框架中的Bean.docx

    org.springframework.web.servlet-3.0.1.RELEASE-A.jar

    Error creating bean with name 'org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0' defined in ServletContext resource [/WEB-INF/springMVC-servlet.xml]: Initialization of bean failed;...

    基于jsp+servlet图书管理系统之后台用户信息查询操作

    之前上传的是基于jsp+servlet实现的图书管理系统后台的插入操作,这次上传的是查询操作,附有数据库,代码有详细的解释,当执行查询和插入操作之后会显示在页面上面

    spring-servlet

    &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" &gt; &lt;property name="prefix" value="/WEB-INF/jsp/" &gt;&lt;/property&gt; &lt;property name="suffix" value=".jsp" &gt;&lt;/property&gt;...

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Spring中ApplicationContext加载机制

    ApplicationContext 是 Spring 框架中的核心组件之一,负责加载和管理应用程序中的 Bean 对象。在 Web 应用程序中,ApplicationContext 的加载机制是非常重要的, Spring 提供了多种方式来加载 ApplicationContext。...

    Spring MVC 入门实例

    11 &lt;bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt; 12 13 14 &lt;prop key="/hello.do"&gt;helloController 15 16 17 &lt;/bean&gt; 18 19 &lt;bean id=...

    Spring攻略(第二版 中文高清版).part1

    6.2 在你的Servlet和过滤器中使用Spring 214 6.2.1 问题 214 6.2.2 解决方案 215 6.2.3 工作原理 215 6.3 将Spring与Struts 1.x集成 220 6.3.1 问题 220 6.3.2 解决方案 220 6.3.3 工作原理 220 6.4...

    Spring攻略(第二版 中文高清版).part2

    6.2 在你的Servlet和过滤器中使用Spring 214 6.2.1 问题 214 6.2.2 解决方案 215 6.2.3 工作原理 215 6.3 将Spring与Struts 1.x集成 220 6.3.1 问题 220 6.3.2 解决方案 220 6.3.3 工作原理 220 6.4...

    spring4.3.9相关jar包

    spring-beans.jar(必须):这 个jar 文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean 以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI ...

    spring3.2与Ibatis整合

    spring3.2与Ibatis整合,在servlet中获取bean

    springweb-Jackson

    对于在sringmvc中将Map自动转到json返回到页面中。 1:加入三个jar包: maven中是&lt;!-- Jackson Json处理工具包 --&gt; &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt; &lt;artifactId&gt;jackson-databind ${...

    Spring_Framework_ API_5.0.5 (CHM格式)

    请注意,在 Spring5 中,传统的 SpringMVC 支持 Servlet3.1 上运行,或者支持 JavaEE7 的服务器。 5. 函数式web框架 除了响应式功能之外,Spring5 还提供了一个函数式Web框架。它提供了使用函数式编程风格来定义...

    基于 MySQL+Tomcat+Servlet+JSP 的简单作业管理系统源码+数据库+项目说明.zip

    基于 MySQL+Tomcat+Servlet+JSP 的简单作业管理系统源码+数据库+项目说明.zip # HomeworkManagement ## 简介 一个基于 MySQL+Tomcat+Servlet+JSP 的简单作业管理系统 --- ## 更新 ### Ver 5.0 - 所有模块...

    从零开始学Spring Boot

    1.20 Spring Boot普通类调用bean 1.21 使用模板(thymeleaf-freemarker) 1.22 Spring Boot 添加JSP支持 1.23 Spring Boot Servlet 1.24 Spring Boot过滤器、监听器 1.25 Spring Boot 拦截器HandlerInterceptor 1.26...

    j2ee简介(包括servletjavabean jsp jdbc hibernate struts spring)

    j2ee简介(包括servletjavabean jsp jdbc hibernate struts spring)

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Spring集成AXIS2的Web_service配置方法

    首先,需要在 Spring 项目中添加 AXIS2 的依赖项,包括 axis2-spring-1.4.1.jar 等。然后,需要在 Spring 配置文件中添加 AXIS2 的配置,例如: ```xml &lt;bean id="exhibitorAwareService" class=...

    spring4.1核心包

    配置文件 创建和管理bean。 4.spring-context-4.1.1.RELEASE.jar 在基础IOC功能上提供扩展服务,此外还提供许多企业级服务的支持,有邮件服务、任务调度、JNDI定位,EJB集成、远程访问、缓存以及多种视图层框架的...

    Spring.3.x企业应用开发实战(完整版).part2

    Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。  Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架...

Global site tag (gtag.js) - Google Analytics