• 【Spring】普通类获取Spring容器的bean的方法


    我们通常在项目中获取Spring容器里bean的方式,一般是使用注解的方式(@Autowired、@Resource)直接注入就可以直接使用了,那么如果在一个普通的类里(其他地方使用它的实例是以new的方式使用的),此时再用注解的方式注入的将会是null,那这种情况下,我们该如何使用Spring容器里的bean呢?本篇博客讲解五种方法,接下来就依次详细讲一下使用方式

    一、在初始化时保存ApplicationContext对象

    FileSystemXmlApplicationContext fileSystemXmlApplicationContext = new FileSystemXmlApplicationContext("applicationContext.xml");
    
    • 1
    // 1、使用类名获取
    XXX xxx = SpringContextService.getBean(XXX.class);
    // 2、使用bean名获取
    Object yyy = SpringContextService.getBean("beanName");
    
    • 1
    • 2
    • 3
    • 4

    二、通过Spring提供的工具类获取ApplicationContext对象

    WebApplicationContext requiredWebApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    
    WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    
    • 1
    • 2
    • 3
    // 1、使用类名获取
    XXX xxx = SpringContextService.getBean(XXX.class);
    // 2、使用bean名获取
    Object yyy = SpringContextService.getBean("beanName");
    
    • 1
    • 2
    • 3
    • 4

    三、继承自抽象类ApplicationObjectSupport

    public class SpringTest extends ApplicationObjectSupport {
        private void test(){
       		// 1、使用类名获取
            XXX xxx = getApplicationContext().getBean(XXX.class);
            // 2、使用bean名获取
            Object yyy = getApplicationContext().getBean("beanName");
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    四、继承自抽象类WebApplicationObjectSupport

    ```java
    public class SpringTest extends WebApplicationObjectSupport{
        private void test(){
       		// 1、使用类名获取
            XXX xxx = getApplicationContext().getBean(XXX.class);
            // 2、使用bean名获取
            Object yyy = getApplicationContext().getBean("beanName");
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    五、实现接口ApplicationContextAware

    实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。
    Spring初始化时,会通过该方法将ApplicationContext对象注入
    1、添加一个类,实现ApplicationContextAware

    @Component
    public class SpringContextService implements ApplicationContextAware {
    	private static ApplicationContext applicationContext;
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            if (SpringContextService.applicationContext == null) {
                SpringContextService.applicationContext = applicationContext;
            }
        }
    
        public static <T> T getBean(Class<T> clazz) {
            return applicationContext.getBean(clazz);
        }
        public static Object getBean(String beanName) {
            return applicationContext.getBean(beanName);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    2、普通类的实际使用

    // 1、使用类名获取
    XXX xxx = SpringContextService.getBean(XXX.class);
    // 2、使用bean名获取
    Object yyy = SpringContextService.getBean("beanName");
    
    • 1
    • 2
    • 3
    • 4

    以上就是普通类获取Spring容器的bean的几种方法,感谢您的阅读!

  • 相关阅读:
    【JVM】10 道不得不会的 JVM 面试题
    ABAP中 delete 语句的使用
    HTB-Explore
    RL 暂态电路与磁能
    Nacos使用(三)
    从零开始学习opencv——在虚拟环境下安装opencv环境
    FFmpeg开发笔记(三十)解析H.264码流中的SPS帧和PPS帧
    EasyCVR级联向上级注册时,上级平台通道显示为0是什么原因?
    算法 | 详解斐波那契数列问题
    SpringMVC面试题
  • 原文地址:https://blog.csdn.net/cxh6863/article/details/128174470