【Spring Boot 25】探索ApplicationContextAware接口,高级java工程师年薪

本文介绍了在Spring框架中,通过实现ApplicationContextAware接口来避免使用newClassPathXmlApplicationContext()导致的配置文件重复加载和线程配置类初始化问题。这种方法使得我们可以从已有的Spring上下文中获取bean,从而减少资源浪费,提高应用效率。
摘要由CSDN通过智能技术生成

我们可以进行xml加载并进行初始化

ApplicationContext appContext = new ClassPathXmlApplicationContext(“applicationContext-common.xml”);

UserService userService = (UserService)appContext.getBean(“userService”);

但是这样就会存在一个问题:因为它会重新装载applicationContext-common.xml并实例化上下文bean,如果有些线程配置类也是在这个配置文件中,那么会造成做相同工作的的线程会被启两次。一次是web容器初始化时启动,另一次是上述代码显示的实例化了一次。当于重新初始化一遍!!!!这样就产生了冗余。

三、解决办法ApplicationContextAware


不用类似new ClassPathXmlApplicationContext()的方式,从已有的spring上下文取得已实例化的bean。通过ApplicationContextAware接口进行实现。

当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取Spring配置文件中,所有有引用到的bean对象。

研究了一天,终于百度到了,都是固定写法!!!

package com.util;

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

impo

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

rt org.springframework.stereotype.Component;

@Component

public class SpringContextUtil implements ApplicationContextAware{

private static ApplicationContext applicationContext = null;

@Override

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

if (SpringContextUtil.applicationContext == null) {

SpringContextUtil.applicationContext = applicationContext;

}

}

public static ApplicationContext getApplicationContext() {

return applicationContext;

}

public static Object getBean(String name) {

return getApplicationContext().getBean(name);

}

public static Object getBean(Class clazz) {

return getApplicationContext().getBean(clazz);

}

public static Object getBean(String name, Class clazz) {

return getApplicationContext().getBean(name, clazz);

}

}

@Autowired

private static UserService userService;

public static void main(String[] args) {

SpringApplication.run(DBApplication.class,args);

//ApplicationContext applicationContext = SpringContextUtil.getApplicationContext();

//userService = applicationContext.getBean(UserService.class);

userService = (UserService)SpringContextUtil.getBean(UserService.class);

userService.selectUser(“zs”);

}

1、在spring的配置文件中,注册方法类SpringContextUtil。之所以方法类SpringContextUtil能够灵活自如地获取ApplicationContext,就是因为spring能够为我们自动地执行了setApplicationContext。但是,spring不会无缘无故地为某个类执行它的方法的,所以,就很有必要通过注册方法类AppUtil的方式告知spring有这样子一个类的存在。这里我们使用@Component来进行注册,或者我们也可以像下面这样在配置文件声明bean:

2、加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的。

public void setApplicationContext(ApplicationContext context) throws BeansException

方法,获得ApplicationContext对象,ApplicationContext对象是由spring注入的。前提必须在Spring配置文件中指定该类。

3、使用静态的成员ApplicationContext类型的对象。

四、可以通过框架中的beforeStart()预加载service接口

//继承Thread的类中注入失败的问题

public class MyTask extend BaseTask{

@Autowired

private UserService userService;

public static UserService static_userService;

@Override

public void beforeStart() {

static_userService= userService;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值