分享

普通类调用Spring web bean对象

 hehffyy 2012-06-20

说明:

1》. 本文针对Java语言.

2》. 代码在实际项目中使用过.

注:有问题可以QQ联系:412887952

需求的提出:

我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接使用spring提供的其他对象或者说有一些不需要交给spring管理,但是需要用到spring里的一些对象。如果这是spring框架的独立应用程序,我们通过

ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId"); 

这样的方式就可以很轻易的获取我们所需要的对象。

但是往往我们所做的都是Web Application,这时我们启动spring容器是通过在web.xml文件中配置,这样就不适合使用上面的方式在普通类去获取对象了,因为这样做就相当于加载了两次spring容器,而我们想是否可以通过在启动web服务器的时候,就把Application放在某一个类中,我们通过这个类在获取,这样就可以在普通类获取spring bean对象了,让我们接着往下看。

1. 通过Spring提供的工具类获取ApplicationContext对象
这种方式不需要修改任何的配置,只需要在你需要的地方,当然要求也比较高,需要能够获取HttpServletRequest对象,比如在jsp代码中使用,代码如下:

<%

  /**

    上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。

  */

  ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext()); 

  ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()); 

  MyService service1 = (MyService)ac1.getBean("myService");//这是beanId. 

  MyService service2 = (MyService)ac2.getBean("myService");

  out.println("第一种方式1----:"+service1.getSystemTime());//获取系统时

  out.println("<br />------------<br />");

  out.println("第一种方式2----:"+service2.getSystemTime());

%>

注:代码直接复制是不能正常运行的,需要需要bean的名称.

这种方式明显有很大的漏洞,感觉用起来不舒服,其一:需要request对象,其他很难封装一个Java工具类。

2. 继承自抽象类ApplicationObjectSupport 

我们编写一个当启动web服务容器的时候,就将ApplicationContext注入到一个spring工具类的一个静态属性中,这样在普通类就可以通过这个工具类获取ApplicationContext,从而通过getBean( )获取bean对象。

我们把这个工具类取名为:ToolSpring放在net.angel.tools包下:

代码如下:

package net.angel.tools;

import javax.faces.application.Application;

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

import org.springframework.context.support.ApplicationObjectSupport;

import org.springframework.web.context.support.WebApplicationObjectSupport;

/**

 * 获取spring信息的工具类

 * @author Administrator

 *

 */

public final class ToolSpring extends ApplicationObjectSupport {

private static ApplicationContext applicationContext = null;

@Override

protected void initApplicationContext(ApplicationContext context)

throws BeansException {

// TODO Auto-generated method stub

super.initApplicationContext(context);

if(ToolSpring.applicationContext == null){

ToolSpring.applicationContext = context;

System.out.println();

System.out.println();

System.out.println("---------------------------------------------------------------------");

System.out.println("========ApplicationContext配置成功,在普通类可以通过调用ToolSpring.getAppContext()获取applicationContext对象,applicationContext="+applicationContext+"========");

System.out.println("---------------------------------------------------------------------");

System.out.println();

System.out.println();

}

}

public static ApplicationContext getAppContext() {

return applicationContext;

}

public static Object getBean(String name){

return getAppContext().getBean(name);

}

}

编写完代码之后,我们需要在spring配置文件中加入:

<!-- 这个主要是用于普通类调用spring对象的配置,可以不配置id属性. -->

<bean id="toolSpring" class="net.angel.tools.ToolSpring"></bean>

这个配置,这步很重要,前往不能忘了,如果没有配置的话,获取的ApplicationContext将为null。

这样在启动的时候就会载入这个类,如果成功的话,会在控制台看到System.out.println的输出信息。

在普通类可以通过ToolSpring.getBean("beanId");获取spring的bean对象。

3. 继承自抽象类WebApplicationObjectSupport 

这种方式和第二种差不多,不过多介绍,直接给代码:

package net.angel.tools;

import javax.faces.application.Application;

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ApplicationObjectSupport;

import org.springframework.web.context.support.WebApplicationObjectSupport;

/**

 * 获取spring信息的工具类

 * @author Administrator

 *

 */

public final class ToolSpring extends WebApplicationObjectSupport{

private static ApplicationContext applicationContext = null;

@Override

protected void initApplicationContext(ApplicationContext context) {

// TODO Auto-generated method stub

super.initApplicationContext(context);

if(applicationContext == null){

applicationContext = context;

System.out.println();

System.out.println();

System.out.println("---------------------------------------------------------------------");

System.out.println("========ApplicationContext配置成功,在普通类可以通过调用ToolSpring.getAppContext()获取applicationContext对象,applicationContext="+applicationContext+"========");

System.out.println("---------------------------------------------------------------------");

System.out.println();

System.out.println();

}

}

public static ApplicationContext getAppContext() {

return applicationContext;

}

public static Object getBean(String name){

return applicationContext.getBean(name);

}

}

Spring配置文件加入:

<!-- 这个主要是用于普通类调用spring对象的配置,可以不配置id属性. -->

<bean id="toolSpring" class="net.angel.tools.ToolSpring"></bean>

4. 实现接口ApplicationContextAware

这种方式是实现接口的方式,本人比较喜欢这种方式,因为这种方式扩展性比较强,我们可以根据需要在加入其他的接口。方式跟以上几种方式差不多。

package net.angel.tools;

import javax.faces.application.Application;

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

import org.springframework.context.support.ApplicationObjectSupport;

import org.springframework.web.context.support.WebApplicationObjectSupport;

/**

 * 获取spring信息的工具类

 * @author Administrator

 *

 */

public final class ToolSpring implements ApplicationContextAware{

private static ApplicationContext applicationContext = null;

@Override

public  void setApplicationContext(ApplicationContext applicationContext)

throws BeansException {

// TODO Auto-generated method stub

if(ToolSpring.applicationContext == null){

ToolSpring.applicationContext  = applicationContext;

System.out.println();

System.out.println();

System.out.println("---------------------------------------------------------------------");

System.out.println("========ApplicationContext配置成功,在普通类可以通过调用ToolSpring.getAppContext()获取applicationContext对象,applicationContext="+applicationContext+"========");

System.out.println("---------------------------------------------------------------------");

System.out.println();

System.out.println();

}

}

public static ApplicationContext getApplicationContext() {

return applicationContext;

}

public static Object getBean(String name){

return getApplicationContext().getBean(name);

}

}

在Spring配置文件中加入:

<!-- 这个主要是用于普通类调用spring对象的配置,可以不配置id属性. -->

<bean id="toolSpring" class="net.angel.tools.ToolSpring"></bean>

在其他类通过使用

ToolSpring.getBean("beanId");就可以进行调用了。

<!--EndFragment-->

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多