当前位置:首页 » 网页前端 » web获取springbean
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

web获取springbean

发布时间: 2022-07-24 13:49:20

‘壹’ spring 怎么获取bean

Spring在代码中获取bean的几种方式
方法一:在初始化时保存ApplicationContext对象
方法二:通过Spring提供的utils类获取ApplicationContext对象
方法三:继承自抽象类ApplicationObjectSupport
方法四:继承自抽象类WebApplicationObjectSupport
方法五:实现接口ApplicationContextAware
方法六:通过Spring提供的ContextLoader
获取spring中bean的方式总结:

方法一:在初始化时保存ApplicationContext对象
ApplicationContext ac = new ("applicationContext.xml"); ac.getBean("userService");//比如:<bean id="userService" class="com.cloud.service.impl.UserServiceImpl"></bean>

说明:这样的方式适用于采用Spring框架的独立应用程序,须要程序通过配置文件手工初始化Spring的情况。
方法二:通过Spring提供的工具类获取ApplicationContext对象

ApplicationContext ac1 = WebApplicationContextUtils.(ServletContext sc); ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc); ac1.getBean("beanId"); ac2.getBean("beanId");

说明:这样的方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象。然后在通过它获取须要的类实例。上面两个工具方式的差别是,前者在获取失败时抛出异常。后者返回null。
方法三:继承自抽象类ApplicationObjectSupport

说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法。能够方便的获取ApplicationContext。
Spring初始化时。会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。
方法四:继承自抽象类WebApplicationObjectSupport

说明:类似上面方法。调用getWebApplicationContext()获取WebApplicationContext
方法五:实现接口ApplicationContextAware

说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。Spring初始化时,会通过该方法将ApplicationContext对象注入。

下面是实现ApplicationContextAware接口方式的代码,前面两种方法类似:
public class SpringContextUtil implements ApplicationContextAware { // Spring应用上下文环境 private static ApplicationContext applicationContext; /** * 实现ApplicationContextAware接口的回调方法。设置上下文环境 * * @param applicationContext */ public void setApplicationContext(ApplicationContext applicationContext) { SpringContextUtil.applicationContext = applicationContext; } /** * @return ApplicationContext */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * 获取对象 * * @param name * @return Object * @throws BeansException */ public static Object getBean(String name) throws BeansException { return applicationContext.getBean(name); } }

尽管,spring提供的后三种方法能够实如今普通的类中继承或实现对应的类或接口来获取spring 的ApplicationContext对象,可是在使用是一定要注意实现了这些类或接口的普通java类一定要在Spring 的配置文件applicationContext.xml文件里进行配置。否则获取的ApplicationContext对象将为null。
方法六:通过Spring提供的ContextLoader
WebApplicationContext wac = ContextLoader.();wac.getBean(beanID);

最后提供一种不依赖于servlet,不须要注入的方式。可是须要注意一点,在server启动时。Spring容器初始化时,不能通过下面方法获取Spring 容器,细节能够查看spring源代码org.springframework.web.context.ContextLoader。

‘贰’ 如何从当前spring容器中获得bean

1.第一种方式:

在用spring做一个web项目,有一个需求是在没有servletContext的情况下,根据beanId获得当前容器中的具体bean对象。请问如何实现?注意,这个beanId可能是外部系统传过来的,所以无法用@autowire提前注入。
发现

Java代码

WebApplicationContext wac = ContextLoader.getCurrentWebApplicationCon;

但这个方法只能获得web容器,我想做的是获得当前的spring root container即无论当前是通过web还是非web加载的bean都能让我获得到。

2.第二种方式(费web工程)


context = new ();

3.第三种方式

写一个类,实现BeanFactoryAware接口,把该接口配置到spring中,然后把getbean方法写成静态的,就可以动态获取了。下面是示例:

Java代码

public class Springfactory implements BeanFactoryAware {

private static BeanFactory beanFactory;

// private static ApplicationContext context;

public void setBeanFactory(BeanFactory factory) throws BeansException {
this.beanFactory = factory;
}

/**
* 根据beanName名字取得bean
*
* @param beanName
* @return
*/
public static <T> T getBean(String beanName) {
if (null != beanFactory) {
return (T) beanFactory.getBean(beanName);
}
return null;
}

}

<bean id="springFactoryUtil"
class="com.hundsun.jresplus.mvc.demo.util.SpringFactoryUtil">
</bean>
使用的时候,通过Springfactory.getBean("beanName"),就可以获取到bean了。注意:这个是静态方法,直接通过类名去调用。

‘叁’ 如何通过代码直接获得Spring容器中的Bean

[引文]通常在struts2+spring的例子上,要想使用spring的Bean,可以在applicationContext.xml加上如下配置
<bean id="springBean" scope="prototype" class=""
<property name="name" value="chen"/</bean<bean id="myAction" scope="prototype" class=""
<property name="springBean" ref="springBean"/</bean如果是j2ee应用,启动web应用时将会自动加载ApplicationContext实例(Spring容器负责创建Bean实例)
一旦struts2的myAction实例化,其中的SpringBean也会被自动注入进来,从而达到使用SpringBean的目的。
[问题]但是仍有需要通过代码来调用SpringBean的情况:
1)对于不是由spring创建管理的类,如在java 代码中直接使用new去创建一个对象,并且想在这个对象中使用
SpringBean;因为这个对象并不是由Spring容器创建管理的类,所以即使它有setter方法,容器的springBean也不会被注入。
2)动态更改springBean中的属性值,如在代码运行时,name值需要发生变动;
3)对于一个独立的应用程序[解决]定义一个非Spring容器创建管理的类
public class NonSpringClass implements ServletContextAware {
private SpringBean springBean;
//如果 testGetBean不是被Spring容器创建管理,即使它有setter方法,容器的springBean也不会被注入。
public void setSpringBean(SpringBean springBean){this.springBean=springBean;}//利用ApplicationContext 从spring容器中获得springBean;
//Spring有两个核心接口BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口,
//它们代表了Spring容器,Spring容器是产生Bean的工厂,用于管理容器中的Bean。
public NonSpringClass (){//ApplicationContext acx = new ("applicationContext.xml");
ApplicationContext acx = new ("src/WEB-INF/applicationContext.xml");
springBean=(SpringBean)acx.getBean("springBean");}}调用这个类的代码:
import com.NonSpringClass;
public class TestCode {
private NonSpringClass nonSpringClass;
//这样nonSpringClass里将包含Spring容器创建的springBean}}

‘肆’ spring 中怎么取得bean

总共有6种方法可以实现:

方法一:在初始化时保存ApplicationContext对象
方法二:通过Spring提供的utils类获取ApplicationContext对象
方法三:继承自抽象类ApplicationObjectSupport
方法四:继承自抽象类WebApplicationObjectSupport
方法五:通过Spring提供的ContextLoader

获取spring中bean的方式总结:

方法一:在初始化时保存ApplicationContext对象

ApplicationContext ac = new ("applicationContext.xml"); ac.getBean("userService");//比如:<bean id="userService" class="com.cloud.service.impl.UserServiceImpl"></bean>

说明:这样的方式适用于采用Spring框架的独立应用程序,须要程序通过配置文件手工初始化Spring的情况。

方法二:通过Spring提供的工具类获取ApplicationContext对象

ApplicationContext ac1 = WebApplicationContextUtils.(ServletContext sc); ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc); ac1.getBean("beanId"); ac2.getBean("beanId");

说明:这样的方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象。然后在通过它获取须要的类实例。上面两个工具方式的差别是,前者在获取失败时抛出异常。后者返回null。

方法三:继承自抽象类ApplicationObjectSupport

说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法。能够方便的获取ApplicationContext。

Spring初始化时。会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。

方法四:继承自抽象类WebApplicationObjectSupport

说明:类似上面方法。调用getWebApplicationContext()获取WebApplicationContext

方法五:通过Spring提供的ContextLoader

WebApplicationContext wac = ContextLoader.();wac.getBean(beanID);

‘伍’ 项目中获取Spring的Bean的几种方式

但是,对于系统中非Spring框架管理的类,如果需要获取Spring管理的类,或者,程序中需要动态的根据Bean的id来获取Bean实例,不可能事先为该类提供所有需要的Bean属性的setter方法,在类似这样的情况下,获取Spring框架管理的类实例的方法有多种,现在简单总结如下:方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new ("applicationContext.xml"); ac.getBean("beanId");说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。 方法二:通过Spring提供的工具类获取ApplicationContext对象代码:import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; ServletContext sc=loc_session.getServletContext(); ApplicationContext ac1 = WebApplicationContextUtils.(sc) ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(sc) ac1.getBean("beanId"); ac2.getBean("beanId");说明:这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。 上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。 方法三:继承自抽象类ApplicationObjectSupport说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。方法四:继承自抽象类WebApplicationObjectSupport说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext 方法五:实现接口ApplicationContextAware说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。Spring初始化时,会通过该方法将ApplicationContext 对象注入。 以上方法适合不同的情况,请根据具体情况选用相应的方法。

‘陆’ 如何获取spring的bean

方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法三:继承自抽象类ApplicationObjectSupport 方法四:继承自抽象类WebApplicationObjectSupport 方法五:实现接口ApplicationContextAware 方法六:通过Spring提供的ContextLoader
获取spring中bean的方式总结:
方法一:在初始化时保存ApplicationContext对象

[html] view plain
ApplicationContext ac = new ("applicationContext.xml");
ac.getBean("beanId");

说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。
方法二:通过Spring提供的工具类获取ApplicationContext对象
[java] view plain
ApplicationContext ac1 = WebApplicationContextUtils.(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ac2.getBean("beanId");

说明:这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。
方法三:继承自抽象类ApplicationObjectSupport
说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取ApplicationContext。
Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。
方法四:继承自抽象类WebApplicationObjectSupport
说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext
方法五:实现接口ApplicationContextAware
说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。Spring初始化时,会通过该方法将ApplicationContext对象注入。
以下是实现ApplicationContextAware接口方式的代码,前面两种方法类似:
[java] view plain
package com.ifaboo.MiomOmmg.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;

/**
* 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
*/
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextUtil.applicationContext = applicationContext; // NOSONAR
}

/**
* 取得存储在静态变量中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
checkApplicationContext();
return applicationContext;
}

/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
checkApplicationContext();
return (T) applicationContext.getBean(name);
}

/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(Class<T> clazz) {
checkApplicationContext();
return (T) applicationContext.getBeansOfType(clazz);
}

/**
* 清除applicationContext静态变量.
*/
public static void cleanApplicationContext() {
applicationContext = null;
}

private static void checkApplicationContext() {
if (applicationContext == null) {
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
}
}

}

虽然,spring提供的后三种方法可以实现在普通的类中继承或实现相应的类或接口来获取spring 的ApplicationContext对象,但是在使用是一定要注意实现了这些类或接口的普通java类一定要在Spring 的配置文件applicationContext.xml文件中进行配置。否则获取的ApplicationContext对象将为null。
方法六:通过Spring提供的ContextLoader
[java] view plain
WebApplicationContext wac = ContextLoader.();
wac.getBean(beanID);

最后提供一种不依赖于servlet,不需要注入的方式。但是需要注意一点,在服务器启动时,Spring容器初始化时,不能通过以下方法获取Spring 容器,细节可以查看spring源码org.springframework.web.context.ContextLoader。

‘柒’ spring怎么获取全部的bean

通过xml配置文件 bean配置在xml里面,spring提供多种方式读取配置文件得到ApplicationContext. 第一种方式: 通过程序在初始化的时候,导入Bean配置文件,然后得到Bean实例: ApplicationContext ac = new (applicationContext.xml) ac.getBean(beanName); 第二种方式:WebApplicationContextUtil 在B/S系统中,通常在web.xml初始化bean的配置文件,然后由WebAppliCationContextUtil得到ApplicationContext.例如: ApplicationContext ctx = WebApplicationContextUtils.(ServletContext sc); ApplicationContext ctx =

‘捌’ Spring如何获取Bean

通过xml配置文件
bean配置在xml里面,spring提供多种方式读取配置文件得到ApplicationContext.
第一种方式:
通过程序在初始化的时候,导入Bean配置文件,然后得到Bean实例:
ApplicationContext ac = new (applicationContext.xml)
ac.getBean(beanName);
第二种方式:WebApplicationContextUtil
在B/S系统中,通常在web.xml初始化bean的配置文件,然后由WebAppliCationContextUtil得到ApplicationContext.例如:
ApplicationContext ctx = WebApplicationContextUtils.(ServletContext sc);
ApplicationContext ctx =�0�2�0�2 WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
其中 servletContext sc 可以具体 换成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext();
另外,由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出WebApplicationContext 对象:

‘玖’ Spring获取bean的几种方式

但是,对于系统中非Spring框架管理的类,如果需要获取Spring管理的类,或者,程序中需要动态的根据Bean的id来获取Bean实例,不可能事先为该类提供所有需要的Bean属性的setter方法,在类似这样的情况下,获取Spring框架管理的类实例的方法有多种,现在简单总结如下:方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new ("applicationContext.xml"); ac.getBean("beanId");说明: 这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。 方法二:通过Spring提供的工具类获取ApplicationContext对象代码:import org.springframework.web.context.support.WebApplicationContextUtils; ApplicationContext ac1 = WebApplicationContextUtils.(ServletContext sc) ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc) ac1.getBean("beanId"); ac2.getBean("beanId");说明: 这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后 在通过它获取需要的类实例。 上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。 方法三:继承自抽象类ApplicationObjectSupport说明: 抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationCont ext。Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。 方法四:继承自抽象类WebApplicationObjectSupport说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext 方法五:实现接口ApplicationContextAware说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对 象。Spring初始化时,会通过该方法将ApplicationContext 对象注入。 以上方法适合不同的情况,请根据具体情况选用相应的方法。 这里值得提一点的是,系统中用到上述方法的类实际上就于Spring框架紧密耦合在一起了,因为这些类是知 道它们是运行在Spring框架上的,因此,系统中,应该尽量的减少这类应用,使系统尽可能的独立于当前运 行环境,尽量通过DI的方式获取需要的服务提供者。 本人认为,方法五比较可行,可以设计一个工具类,专门来获取Spring中的类。减少对业务代码的侵入性。