AppContextAware.getBean(XX.class);
package com.jmu.infrastructure.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * 类说明:用于注入Spring上下文ApplicationContext,以在应用程序中提供更为方便的控制. * 注意,只能该对象被配置到Spring中时才有效,需要在Spring的配置文件中进行配置, * * <pre> * 范例:<bean class="com.jmu.infrastructure.utils.AppContextAware" /> * </pre> * */ public class AppContextAware implements ApplicationContextAware { /** * logger */ private static Logger logger = LoggerFactory.getLogger(AppContextAware.class); /** * 系统中的context对象 */ private static ApplicationContext context = null; /** * * 获取Spring上下文ApplicationContext对象 * * @return ApplicationContext对象 * */ public static ApplicationContext getContext() { if (context == null) { logger.error("当前context为空,可能是Spring配置文件中没有配置加载本类[{}]!", AppContextAware.class.getName()); throw new IllegalStateException("当前没有Spring的applicationContext注入,请确定是否有配置Spring,并在Spring中配置了本类的注入!" + AppContextAware.class); } return context; } /** * 取指定类型的Bean,如果不存在或存在多于1个,则抛出异常IllegalStateException. * * @param <E> * E * @param type * type * @return 指定类型的Bean */ @SuppressWarnings("unchecked") public static <E> E getBeanByType(Class<? extends E> type) { try { String[] beanNames = getContext().getBeanNamesForType(type); if (beanNames != null && beanNames.length == 1) { return (E) getContext().getBean(beanNames[0]); } if (beanNames == null || beanNames.length == 0) { throw new IllegalStateException("未找到指定类型的Bean定义."); } throw new IllegalStateException("找到多个同类型的Bean定义."); } catch (Exception e) { logger.error("根据类型在Spring上下文查找对象出错:" + type, e); throw new IllegalStateException("根据类型在Spring上下文查找对象出错:" + type, e); } } /** * * 从Spring Context中获取指定的Bean * * @param <E> * E * @param beanName * bean的名称 * @return bean对象 * */ @SuppressWarnings("unchecked") // 从Spring中取对象并转换是免不了有这错误的,所以忽略 public static <E> E getBean(String beanName) { try { return (E) getContext().getBean(beanName); } catch (Exception e) { // logger.error("在Spring上下文查找对象出错:" + beanName, th); throw new IllegalStateException("在Spring上下文查找对象出错:" + beanName); } } /** * 从Spring Context中获取指定的Bean * * @param <E> * E * @param clazz * clazz * @return 指定的Bean * */ public static <E> E getBean(Class<E> clazz) { return getBeanByType(clazz); // return getBean(clazz.getName()); } /** * * 是否有指定的Bean存在. * * @param beanName * beanName * @return 是否有指定的Bean存在. * */ public static boolean containBean(String beanName) { return getContext().containsBean(beanName); } /** * * 用于在被Spring加载时,由Spring注入ApplicationContext对象 * * @param context * 被注入的context对象 * @throws BeansException */ public void setApplicationContext(ApplicationContext context) throws BeansException { logger.debug("准备注入SpringContext[{}]", context.toString()); if (AppContextAware.context != null) { logger.warn("注意,已经注入过Spring上下文[{}],请检查配置是否有问题导致重复注入!", AppContextAware.context.toString()); // throw new // IllegalStateException("已经注册过Spring上下文,请检查配置是否有问题导致重复注入!"); } AppContextAware.context = context; } }
相关推荐
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中获取Spring配置的bean呢...下面通过本文给大家介绍Java中Spring获取bean方法小结,对spring获取bean方法相关知识感兴趣的朋友一起学习吧
主要为大家详细介绍了Java类获取Spring中bean的5种方式,感兴趣的小伙伴们可以参考一下
java中获得spring中的BEAN,
NULL 博文链接:https://xieke90.iteye.com/blog/2234803
Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...
本篇文章是对在Java中如何获取Spring中配置的bean进行了详细的分析介绍,需要的朋友参考下
众所周知,Spring框架是控制反转(IOC)或依赖性注入(DI) 模式的推动因素,而这种推动是通过基于容器的配置实现的。过去,Spring允许开发人员使用基于XML 的配置,通过利用应用程序上下文XML文件来管理bean依赖性。此...
主要介绍了Spring启动后获取所有拥有特定注解的Bean实例代码,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
介绍了Java获取Bean容器的几种常见调用方式,其中详细写出了如何实现ApplicationContextAware 接口来获取Bean容器的工具类
以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. 在配置文件中注入<bean class="xxx.xxx.SpringContextHolder" lazy-init="false" /> 即可使用
主要给大家介绍了关于Spring装配Bean之用Java代码安装配置bean的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧。
主要为大家详细介绍了Spring boot工具类静态属性注入,及多环境配置详解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
SpringUtil工具类(获取applicationContext,通过name获取 Bean,通过class获取Bean,通过name,以及Clazz返回)
使用@Autowired的方式注入 必须在类上加@Component。我是用的这种 @Component public class KlineDataUtil { private static RedisService redisService; @Autowired public void set(RedisService redisService ...
通过精细的分析,本文揭示了AnnotationConfigApplicationContext的实例化过程,详细解读了DefaultListableBeanFactory的作用及其在Bean生产和获取中的关键性作用。同时,本文对Spring Bean的生命周期进行了深入剖析...
大家都知道spring有多重配置方式,基于XML,基于注解,基于java类的配置,其中基于XML是最强大的一种,下面这篇文章主要给大家介绍了关于Spring装配Bean之XML安装配置bean的相关资料,需要的朋友可以参考借鉴,下面...
XML配置可能很冗长使用注释配置Spring Bean 注释使XML配置最小化扫描组件类Spring将扫描您的Java类以获取特殊注释在Spring容器中自动注册Bean默认Bean ID 您可以显式创建Bean ID 前任。 @Component(“ beanID”) ...
本示例是在 Axis2 通过 外部 Spring容器 获取指定的实现Bean,以提供服务。全部自己写WSDL和服务实现类,不使用 Axis2提供的 Java2WSDL 和 WSDL2Java 工具
spring框架技术+第2天+xmind思维导图:生命周期,介绍simple project,打印出构造方法...bean作用域request session globalSession:web项目获取核心配置文件要配置两个地方:spring监听器、spring作用域范围的监听。
5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 5.4.2.1. 注册用户自定义的PropertyEditor 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 ...