spring怎么获取全部的bean

spring怎么获取全部的bean,第1张

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

首先,获取的 bean 的时候要保证或取得的 bean 必须携带 spring 容器注入的属性,这里举一个简单的例子:

以 dao,service,action 这三种常见的数据模型,业务模型,控制器来举例子,(当然你使用 spring mvc 都是一样的,只不过省掉了 action,但都是控制器,你懂得):

如果jsp 视图需要接受一个 service 的 bean,必须要保证该 service 的 bean 中含有 注入的 dao 属性,这个懂吧?,如果直接在 jsp 中 new(实例化对象),这样将会丢失属性的,因为内存区域完全开辟了新的堆空间,跟 spring 容器所管理的 bean 完全不是一个,那么问题就来了,怎么样才能简单的得到这个拥有注入属性的 bean 呢?

可以尝试将该 bean 封装为控制器(controller)中的一个成员(属性),之后通过web 容器内置的request ,session ,application 等作用域来进行封装,这就是简单的解决办法了

在项目运行时吗?

这个方法我在用,spring初始化时将容器对象注入到工具类中,运行时用容器获取bean,你可以试试。

<!-- 调用ApplicationContextAware实现类的setApplicationContext方法,将spring容器注入工具类 -->

<bean class="comutilApplicationContextHelper"/>

package comutil;

import orgspringframeworkbeansBeansException;

import orgspringframeworkcontextApplicationContext;

import orgspringframeworkcontextApplicationContextAware;

/ spring容器工具类 /

public class ApplicationContextHelper implements ApplicationContextAware {

private static ApplicationContext applicationContext;

/

此方法可以把ApplicationContext对象注入到当前类中作为一个静态成员变量。

@param context ApplicationContext 对象

@throws BeansException

/

@Override

public void setApplicationContext(ApplicationContext context) throws BeansException {

ApplicationContextHelperapplicationContext = context;

}

/

按照名称获取bean

@param name bean的名字

@return 返回一个bean对象

/

public static Object getBean(String name) {

return applicationContextgetBean(name);

}

/

获取bean并转换类型

@param name bean的名字

@param requiredType bean的类型

@return 返回一个bean对象

/

@SuppressWarnings("unchecked")

public static <T> T getBean(String name, Class<T> requiredType) {

return (T) getBean(name);

}

}

通过xml配置文件

bean配置在xml里面,spring提供多种方式读取配置文件得到ApplicationContext

第一种方式:FileSystemXmlApplicationContext

通过程序在初始化的时候,导入Bean配置文件,然后得到Bean实例:

ApplicationContext ac = new FileSystemXmlApplicationContext(applicationContextxml)

acgetBean(beanName);

第二种方式:WebApplicationContextUtil

在B/S系统中,通常在webxml初始化bean的配置文件,然后由WebAppliCationContextUtil得到ApplicationContext例如:

ApplicationContext ctx = WebApplicationContextUtilsgetRequiredWebApplicationContext(ServletContext sc);

ApplicationContext ctx =�0�2�0�2 WebApplicationContextUtilsgetWebApplicationContext(ServletContext sc);

其中 servletContext sc 可以具体 换成 servletgetServletContext()或者 thisgetServletContext() 或者 requestgetSession()getServletContext();

另外,由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出WebApplicationContext 对象:

以上就是关于spring怎么获取全部的bean全部的内容,包括:spring怎么获取全部的bean、spring mvc里,jsp中怎么获取bean、如何取得Spring管理的bean等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9523867.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存