Spring获取bean对象

在非Spring管理环境中获取Bean对象


推荐一下两种方式


一、实现BeanFactoryPostProcessor接口

实现BeanFactoryPostProcessor接口,并重写postProcessBeanFactory方法,通过ConfigurableListableBeanFactory对象获取所需要的Bean

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

/**
 * @ApiNote: Spring工具类
 * @Author: 陌路
 * @Date: 2022/11/2 22:49
 * @Tool: Created by IntelliJ IDEA
 */
@Component
public final class SpringUtils implements BeanFactoryPostProcessor {

    private static final Logger LOGGER = LoggerFactory.getLogger(SpringUtils.class);
    /** Spring应用上下文环境 */
    private static ConfigurableListableBeanFactory beanFactory;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        SpringUtils.beanFactory = beanFactory;
    }

    public static ConfigurableListableBeanFactory getBeanFactory() {
        return beanFactory;
    }

    /**
     * @apiNote: 获取对象
     * @param: name 对象名
     * @return: Object 一个以所给名字注册的bean的实例
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        if (getBeanFactory() == null) {
            LOGGER.info("没有BeanFactory,忽略错误");
            return null;
        }
        return (T) getBeanFactory().getBean(name);
    }

    /**
     * @apiNote: 获取类型为requiredType的对象
     * @param: name 类名称
     * @return: T
     */
    public static <T> T getBean(Class<T> name) {
        if (getBeanFactory() == null) {
            LOGGER.info("本地调试Main模式,没有BeanFactory,忽略错误");
            return null;
        }
        return (T) getBeanFactory().getBean(name);
    }

    /**
     * @apiNote: 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
     * @param: name
     * @return: boolean
     */
    public static boolean containsBean(String name) {
        return getBeanFactory().containsBean(name);
    }

    /**
     * @apiNote: 判断bean是singleton还是prototype。
     * @Desc: 如果与name对应的bean没被找到,将会抛出(NoSuchBeanDefinitionException)
     * @param: name 实例对象名称
     * @return: 是singleton true
     */
    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        return getBeanFactory().isSingleton(name);
    }

    /**
     * @apiNote: 注册对象的类型
     * @param: name 对象名称
     * @return: clazz
     */
    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
        return getBeanFactory().getType(name);
    }

    /**
     * @apiNote: 如果name在bean定义中有别名,则返回这些别名
     * @param: name 对象别名
     * @return: String[]
     */
    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        return getBeanFactory().getAliases(name);
    }
}

二、实现ApplicationContextAware接口

实现ApplicationContextAware接口,并重写setApplicationContext方法,通过ApplicationContext对象获取所需要的Bean对象

import org.springframework.aop.framework.AopContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @ApiNote: spring工具类 方便在非spring管理环境中获取bean
 * @Author: 陌路
 * @Date: 2022/11/2 20:40
 * @Tool: Created by IntelliJ IDEA
 */
@Component
public final class ApplicationContextHolder implements ApplicationContextAware {
    /*
        实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext对象。
        Spring初始化时,扫描到该类,就会通过该方法将ApplicationContext对象注入。然后在代码中就可以获取spring容器bean了。
     */
     
    /** Spring应用上下文环境 */
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        ApplicationContextHolder.applicationContext = applicationContext;
    }

    /**
     * @apiNote: 根据对象名获取对象
     * @param: name
     * @return: T
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        return (T) applicationContext.getBean(name);
    }

    /**
     * @apiNote: 反射获取对象实例
     * @param: clazz
     * @return: T
     */
    public static <T> T getBean(Class<T> clz) {
        T result = (T) applicationContext.getBean(clz);
        return result;
    }

    /**
     * @apiNote: BeanFactory是否包含指定的bean
     * @param: name 实例对象名称
     * @return: 包含true
     */
    public static boolean containsBean(String name) {
        return applicationContext.containsBean(name);
    }

    /**
     * @apiNote: 判断bean是singleton还是prototype。
     * @Desc: 如果与name对应的bean没被找到,将会抛出(NoSuchBeanDefinitionException)
     * @param: name 实例对象名称
     * @return: 是singleton true
     */
    public static boolean isSingleton(String name) {
        return applicationContext.isSingleton(name);
    }

    /**
     * @apiNote: 注册对象的类型
     * @param: name 对象名称
     * @return: clazz
     */
    public static Class<?> getType(String name) {
        return applicationContext.getType(name);
    }

    /**
     * @apiNote: 如果name在bean定义中有别名,则返回这些别名
     * @param: name 对象别名
     * @return: String[]
     */
    public static String[] getAliases(String name) {
        return applicationContext.getAliases(name);
    }

    /**
     * @apiNote: 获取aop代理对象
     * @param: invoker
     * @return: T
     */
    @SuppressWarnings("unchecked")
    public static <T> T getAopProxy(T invoker) {
        return (T) AopContext.currentProxy();
    }

    /**
     * @apiNote: 获取当前的环境配置,无配置返回null
     * @return: 当前的环境配置
     */
    public static String[] getActiveProfiles() {
        return applicationContext.getEnvironment().getActiveProfiles();
    }

    /**
     * @apiNote: 获取当前的环境配置,当有多个环境配置时,只获取第一个
     * @return: 当前的环境配置
     */
    public static String getActiveProfile() {
        final String[] activeProfiles = getActiveProfiles();
        return ObjUtil.arrIsEmpty(activeProfiles) ? null : activeProfiles[0];
    }
}

其他方法

一、通过ApplicationContext对象获取

适用于Spring框架的独立应用程序,需要程序通过配置文件初始化Spring。

需要applicationContext.xml配置文件配置

<bean id="user" class="cn.molu.pojo.User"></bean>
@Test
public void test() {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    //ApplicationContext applicationContext = new FileSystemXmlApplicationContext("applicationContext.xml"); 
    User user = (User) applicationContext.getBean("user");
    System.out.println(test);
}
二、通过ApplicationContext对象获取

适合于Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象。然后在通过它获取须要的类实例。以下两个工具方式的差别是,ServletContext在获取失败时抛出异常。ApplicationContext返回null。

ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc); 
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc); 
ac1.getBean("beanId"); 
ac2.getBean("beanId");  
三、通过ApplicationContextAware对象(推荐)

实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext对象。Spring初始化时,扫描到该类,就会通过该方法将ApplicationContext对象注入。然后在代码中就可以获取spring容器bean了。

见上文实现ApplicationContextAware接口

四、通过继承ApplicationObjectSupport对象

调用父类的getApplicationContext()方法,获取Spring容器对象。

public class SpringContextHelper extends ApplicationObjectSupport {
    public Object getBean(String beanName) {
        return getApplicationContext().getBean(beanName);
    }
}
五、通过继承WebApplicationObjectSupport对象

调用getWebApplicationContext()获取WebApplicationContext

public class SpringContextHelper extends WebApplicationObjectSupport {
    public Object getBean(String beanName) {
        return getApplicationContext().getBean(beanName);
    }
}
六、通过BeanFactory对象(不推荐)

使用BeanFactory从工厂中直接获取Bean实例,但是XmlBeanFactory类已经废弃,因此不建议使用。

@Test
public void test() {
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
    Test test= (Test) beanFactory.getBean("test");
    System.out.println(test);
}
七、通过ContextLoader提供的getCurrentWebApplicationContext方法
@Test
public void test() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/applicationContext.xml");
    ServletContextListener listener = new ContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    listener.contextInitialized(event);
    
    WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
    Test test= (Test) wac.getBean("test");
    System.out.println(test);
}
八、通过实现BeanFactoryPostProcessor接口

spring工具类 方便在非spring管理环境中获取bean

见上文实现BeanFactoryPostProcessor接口

BeanFactoryApplicationContextSpring的两大核心接口,都可以当做Spring的容器。其中ApplicationContextBeanFactory的子接口。


BeanFactory
(1)、是Spring里面最底层的接口(最原始的接口),包含了各种Bean的定义,读取bean配置文档,管理bean的加载、实例化,控制bean的生命周期,维护bean之间的依赖关系。

(2)、采用的是延迟加载形式来注入Bean的,即只有在使用到某个Bean时(调用getBean()),才对该Bean进行加载实例化。这样,我们就不能发现一些存在的Spring的配置问题。如果Bean的某一个属性没有注入,BeanFacotry加载后,直至第一次使用调用getBean方法才会抛出异常。

(3)BeanFactory通常以编程的方式被创建。

(4)BeanFactory和ApplicationContext都支持BeanPostProcessor、BeanFactoryPostProcessor的使用,但两者之间的区别是:BeanFactory需要手动注册,而ApplicationContext则是自动注册。

(5) 占用内存小。

ApplicationContext
1、ApplicationContext接口作为BeanFactory的派生,除了提供BeanFactory所具有的功能外,还提供了更完整的框架功能:

继承MessageSource,因此支持国际化。
统一的资源文件访问方式。
提供在监听器中注册bean的事件。
同时加载多个配置文件。
载入多个(有继承关系)上下文 ,使得每一个上下文都专注于一个特定的层次,比如应用的web层。
2、ApplicationContext,它是在容器启动时,一次性创建了所有的Bean。这样,在容器启动时,我们就可以发现Spring中存在的配置错误,这样有利于检查所依赖属性是否注入。ApplicationContext启动后预载入所有的单实例Bean,通过预载入单实例bean ,确保当你需要的时候,你就不用等待,因为它们已经创建好了。

3、ApplicationContext占用内存空间大,当程序的配置bean特别多时,程序启动慢。

4、ApplicationContext能以编程式方式创建,还能能以声明的方式创建,如使用ContextLoader。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值