当前位置: 移动技术网 > IT编程>开发语言>Java > Java中Spring获取bean方法小结

Java中Spring获取bean方法小结

2019年07月22日  | 移动技术网IT编程  | 我要评论

spring是一个轻量级的控制反转(ioc)和面向切面(aop)的容器框架,如何在程序中获取spring配置的bean呢?

bean工厂(com.springframework.beans.factory.beanfactory)是spring框架最核心的接口,它提供了高级ioc的配置机制。beanfactory使管理不同类型的java对象成为可能,应用上下文(com.springframework.context.applicationcontext)建立在beanfactory基础之上,提供了更多面向应用的功能,它提供了国际化支持和框架事件体系,更易于创建实际应用。我们一般称beanfactory为ioc容器,而称applicationcontext为应用上下文。但有时为了行文方便,我们也将applicationcontext称为spring容器。

对于两者的用途,我们可以进行简单划分:beanfactory是spring框架的基础设施,面向spring本身;applicationcontext面向使用spring框架的开发者,几乎所有的应用场合我们都直接使用applicationcontext而非底层的beanfactory。

applicationcontext的初始化和beanfactory有一个重大的区别:beanfactory在初始化容器时,并未实例化bean,直到第一次访问某个bean时才实例目标bean;而applicationcontext则在初始化应用上下文时就实例化所有单实例的bean。因此applicationcontext的初始化时间会比beanfactory稍长一些

本文不涉及通过 @resource 、 @autowired 自动注入,仅仅通过 applicationcontext 获取 sping 配置文件中的 bean 。

要获取xml中配置的bean,最关键的是获取org.springframework.context.applicationcontext

第一种获取 applicationcontext 的方法:

import org.springframework.context.applicationcontext;
import org.springframework.context.support.filesystemxmlapplicationcontext;
applicationcontext applicationcontext = new filesystemxmlapplicationcontext("applicationcontext.xml");

或者

import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
private applicationcontext applicationcontext = new classpathxmlapplicationcontext("applicationcontext.xml");

这种方式实例化applicationcontext是非常耗时的,这种方式适用于采用spring框架的独立应用程序,仅仅推荐使用在程序需要通过配置文件手工初始化spring的情况。applicationcontext的主要实现类是classpathxmlapplicationcontext和filesystemxmlapplicationcontext,前者默认从类路径加载配置文件,后者默认从文件系统中装载配置文件

例子:

public class beanmanager {
private static applicationcontext context = new classpathxmlapplicationcontext("appcontext.xml") ;
public static object getbean(string beanid) {
return context.getbean(beanid);
}
}

在 web.xml 中写一个 servlet ,自动启动, init 方法中调用一下 beanmanager

init() throws servletexception {
beanmanager bm = new beanmanager();//可选的,为的是在 web 应用启动的时候就让 spring 加载 bean 配置。
// 否则会在第一次调用 beanmanager 的时候加载,影响一次速度。
}

在 java 代码中使用 beanmanager.getbean(string beanid); 来获得 bean 实例。

第二种获取 applicationcontext 的方法: 通过spring提供的工具类获取applicationcontext对象,专为web工程定制的方法,推荐web项目中使用。例如:

servletcontext servletcontext = request.getsession().getservletcontext();
applicationcontext ac1 = webapplicationcontextutils .getrequiredwebapplicationcontext(servletcontext sc)
applicationcontext ac2 = webapplicationcontextutils .getwebapplicationcontext(servletcontext sc)
ac1.getbean("beanid");
ac2.getbean("beanid");

通过javax.servlet.servletcontext 获取到applicationcontext实例对象,这意味着,必须使用到request、session等等。

这样,就不能把applicationcontext对象设置为成员变量。需要在每个具体的方法中通过request、session等获取到servletcontext再获取applicationcontext实例。

因此,此方法仅仅推荐使用在可以获取到servletcontext对象的web项目中,并且不需要将applicationcontext对象定义为成员变量的情况下。

注意:当使用webapplicationcontextutils获取applicationcontext实例时,需要在web.xml配置文件中添加org.springframework.web.context.contextloaderlistener监听器,否则获取不到applicationcontext对象,返回null。

配置文件:web.xml

<!--contextloaderlistener自动注入 applicationcontext,通过
webapplicationcontextutils.getwebapplicationcontext(request.getsession().getservletcontext())获取 -->
<!--spring配置文件加载位置 -->
<context-param>
<param-name>contextconfiglocation</param-name>
<param-value>/web-inf/spring/appcontext.xml,/web-inf/spring/appinterceptor.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
</listener>

3. 继承自抽象类applicationobjectsupport

抽象类applicationobjectsupport提供getapplicationcontext()方法,可以方便的获取到applicationcontext。spring初始化时,会通过该抽象类的setapplicationcontext(applicationcontext context)方法将applicationcontext 对象注入。

4. 继承自抽象类webapplicationobjectsupport

通过继承org.springframework.web.context.support.webapplicationobjectsupport使用getwebapplicationcontext() 获取到org.springframework.web.context.webapplicationcontext由于web应用比一般的应用拥有更多的特性,因此webapplicationcontext扩展了applicationcontext。webapplicationcontext定义了一个常量root_web_application_ context_attribute,在上下文启动时,webapplicationcontext实例即以此为键放置在servletcontext的属性列表中,因此我们可以直接通过以下语句从web容器中获取webapplicationcontext:

webapplicationcontext wac = (webapplicationcontext)servletcontext.getattribute(
webapplicationcontext.root_web_application_context_attribute);

5. 实现接口applicationcontextaware

实现该接口的setapplicationcontext(applicationcontext context)方法,并保存applicationcontext 对象。spring初始化时,会通过该方法将applicationcontext 对象注入。

第三、四、五种方法都需要将类配置在 spring 配置文件中:

<!--假定applicationcontexttool为继承或者实现了第三、四、五种方法的具体实现类-->
<bean class="com.twovv.utils.applicationcontexttool"></bean>

否则将获取不到 applicationcontext ,返回 null 。

以上内容给大家介绍了java中spring获取bean方法小结,希望大家喜欢。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网