spring 提供了Awear 接口去 让bean 能感受到外界的环境。Awear 接口有很多实现,常用的有

ApplicationContextAware (可以通过实现这个接口去获取ApplicationContext),
BeanNameAware(可以获取Bean自身的一些属性),
BeanFactoryAware(可以获取BeanFactory)
@Component
public class ApplicationContextManager implements ApplicationContextAware{ private static ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("---------------------添加Application--------------------------");
if(ApplicationContextManager.applicationContext == null){
ApplicationContextManager.applicationContext = applicationContext;
}
} public static ApplicationContext getApplicationContext(){
return applicationContext;
} /**
* 通过name 获取Bean
*/
public static Object getBean(String beanName){
return applicationContext.getBean(beanName);
} public static <T> T getBean(String beanName,Class<T> clazz){
return applicationContext.getBean(beanName,clazz);
} public static <T> T getBean(Class<T> clazz){
return applicationContext.getBean(clazz);
}
}

在Bean中获取spring 容器 并通过容器获取容器中其他bean的更多相关文章

  1. Java(多)线程中注入Spring的Bean

    问题说明 今天在web应用中用到了Java多线程的技术来并发处理一些业务,但在执行时一直会报NullPointerException的错误,问题定位了一下发现是线程中的Spring bean没有被注入 ...

  2. 监听器如何获取Spring配置文件(加载生成Spring容器)

    Spring容器是生成Bean的工厂,我们在做项目的时候,会用到监听器去获取spring的配置文件,然后从中拿出我们需要的bean出来,比如做网站首页,假设商品的后台业务逻辑都做好了,我们需要创建一个 ...

  3. 【转】Java(多)线程中注入Spring的Bean

    问题说明 今天在web应用中用到了Java多线程的技术来并发处理一些业务,但在执行时一直会报NullPointerException的错误,问题定位了一下发现是线程中的Spring bean没有被注入 ...

  4. web环境中的spring MVC

    1. web.xml文件的简单详解 在web环境中, spring MVC是建立在IOC容器的基础上,要了解spring mvc,首先要了解Spring IOC容器是如何在web环境中被载入并起作用的 ...

  5. 监听器如何获取Spring配置文件

    我们在做项目的时候,会用到监听器去获取spring的配置文件,然后从中拿出我们需要的bean出来,比如做网站首页,假设商品的后台业务逻辑都做好了,我们需要创建一个监听器,在项目启动时将首页的数据查询出 ...

  6. 获取Spring容器中的Bean

    摘要 SpringMVC框架开发中可能会在Filter或Servlet中用到spring容器中注册的java bean 对象,获得容器中的java bean对象有如下方法 Spring中的Applic ...

  7. spring源码 — 二、从容器中获取Bean

    getBean 上一节中说明了容器的初始化,也就是把Bean的定义GenericBeanDefinition放到了容器中,但是并没有初始化这些Bean.那么Bean什么时候会初始化呢? 在程序第一个主 ...

  8. [十]SpringBoot 之 普通类获取Spring容器中的bean

    我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接使用 ...

  9. 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean

    7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...

  10. SpringBoot 之 普通类获取Spring容器中的bean

    [十]SpringBoot 之 普通类获取Spring容器中的bean   我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器 ...

随机推荐

  1. pyhton函数

    函数编写文档 放在函数开头的字符串称为文档字符串(docstring),将作为函数的一部分存储起来 def square(x): 'Calculates the square of the numbe ...

  2. 面试题Redis最常被问到知识点总结

    1.什么是redis? redis是一个高性能的key-value数据库,它是完全开源免费的,而且redis是一个NOSQL类型数据库,是为了解决高并发.高扩展,大数据存储等一系列的问题而产生的数据库 ...

  3. css动画速度与三次贝赛尔曲线

      今天要说的是css的动画曲线.   首先要说的是语法: value: 1.linear:线性动画,也就是匀速,以相同的速度开始以相同的速度结束. 2.ease:默认的动画效果,特点是先快后慢,时间 ...

  4. CSP2019-S2参赛总结 暨 近期学习反思

    前言 岁月不居,时节如流.眨眼间,2019的联赛就已经落下帷幕了,回忆这一年的学习,有许许多多的事情想写下来.趁联赛结果还未出来,赶紧写下这篇文章,以记录我这段时间的学习和生活. "你怎么又 ...

  5. python web Tornado框架

    1.Tornado Tornado:python编写的web服务器兼web应用框架 1.1.Tornado的优势 轻量级web框架异步非阻塞IO处理方式出色的抗负载能力优异的处理性能,不依赖多进程/多 ...

  6. UE4添加模块

    添加模块在这篇文章里已经有详细的描述了: https://orfeasel.com/creating-custom-modules/ 但是这篇文章中少写了一个步骤: 最后要在 <工程名>E ...

  7. A. Transmigration

    A. Transmigration time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. sublime text3 最新 license注册码分享 2018

    —– BEGIN LICENSE —– Die Socialisten GmbH 10 User License EA7E-800613 51311422 E45F49ED 3F0ADE0C E5B8 ...

  9. 项目配置 xml文件时 报错提示(The reference to entity "useSSL" must end with the ';' delimiter.)

    这次在配置xml文件时,出现错误提示( The reference to entity “useSSL” must end with the ‘;’ delimiter.) 报错行为 <prop ...

  10. 利用Zookeeper实现分布式锁

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...