在非Spring 容器管理的类中,获取spring容器中的 bean

第一种方式

写一个工具类,实现ApplicationContextAware接口,从spring容器中获取

① 写一个工具类

package com.june.utils;

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

@Component
public class ApplicationContextUtils implements ApplicationContextAware {

    public static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    //根据bean名字获取工厂中指定bean 对象
    public static Object getBean(String beanName){
        return context.getBean(beanName);
    }
}

② 使用.(以经常遇到的 redisTemplate 为例)
因为这个类不是spring管理的,所以无法使用 @Autowire 注解直接注入。

package com.june.shiro.config;

import java.util.Collection;
import java.util.Set;

public class RedisCache<k,v> implements Cache<k,v> {


    public RedisTemplate getRedisTemplate() {
       // 使用工具类,根据名字获取容器中的RedisTemplate
        RedisTemplate redisTemplate = (RedisTemplate) ApplicationContextUtils.getBean("redisTemplate");
        
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }

第二种方式
问题 : 比如在非spring容器管理的 SubjectExcelListener 类中要调用sevice

一层一层传递的方式 !!!
在controller 调用service的时候,把service当作参数传入到方法中。 然后在service中调用 SubjectExcelListener 类的时候,把 service 当作参数传递过去

Controller类

@RestController
@RequestMapping("/eduservice/subject")
@CrossOrigin
public class EduSubjectController {

    @Autowired
    private EduSubjectService subjectService;

    @PostMapping("addSubject")
    public R addSubject(MultipartFile file) {
		// subjectService 当作参数传入
        subjectService.saveSubject(file,subjectService);
        
        return R.ok();
    }

}

Service 类

@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {

        try{
            // 这儿调用SubjectExcelListener时候,以有参方法的形式,把subjectService 传入
            InputStream in = file.getInputStream();
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

SubjectExcelListener 类。 需要写出public EduSubjectService subjectService;构造方法

public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {

    //因为SubjectExcelListener不交给spring进行管理,需要自己new,不能注入其他对象
    //不能实现数据库操作
    public EduSubjectService subjectService;
    public SubjectExcelListener() {}
    public SubjectExcelListener(EduSubjectService subjectService) {
        this.subjectService = subjectService;
    }
    
             ...
             ...

	public void invoke() {
		//这儿可以调用 subjectService 的方法了
		subjectService.save(existOneSubject);

	}
	


仅供记录,以防忘记

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值