创建类继承ApplicationContextAware

package net.ybclass.online_ybclass.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @ClassName:AppUtil
 * @Description:获取bean工具类
 * @Author:chenyb
 * @Date:2020/8/20 11:39 上午
 * @Versiion:1.0
 */
@Component
public class AppUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext args) throws BeansException {
        this.applicationContext=args;
    }
    public static Object getObject(String id){
        return applicationContext.getBean(id);
    }
}

使用

@RestController
public class TestController {
    @GetMapping("test")
    public JsonData test()
    {
        VideoService videoService= (VideoService)AppUtil.getObject("videoServiceImpl");
        return JsonData.buildSuccess(videoService.listVideo());
    }
}

 

相关文章:

  • 2022-12-23
  • 2021-12-04
  • 2021-11-22
  • 2022-02-25
  • 2022-12-23
  • 2021-09-20
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-12
  • 2022-12-23
  • 2021-10-14
  • 2022-12-23
  • 2022-01-28
  • 2021-09-01
相关资源
相似解决方案