普通类中通过bean获取service或者mapper,解决service或者mapper为null的异常

在使用普通类的时候,有时需要获取service,mapper等,直接使用自动注入@Autowired提示null异常
有两种解决方案

  • 方案一:创建工具类SpringUtil
    工具类
package com.websocket.util;

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

/**
 * 普通类获取bean
 *
 * @author 
 */
@Component
public class SpringUtil implements ApplicationContextAware {
    private static Logger logger = LoggerFactory.getLogger(SpringUtil.class);
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringUtil.applicationContext == null) {
            SpringUtil.applicationContext = applicationContext;
        }
        logger.info("ApplicationContext配置成功,applicationContext对象:" + SpringUtil.applicationContext);
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}

普通类

package com.websocket.entity;

import com.websocket.service.TwoChatService;
import com.websocket.util.SpringUtil;

/**
 * @author lenovo
 */
public class TestEntity {
    TwoChatService twoChatService = SpringUtil.getBean(TwoChatService.class);
    public Integer test(){
        int count = twoChatService.count();
        return count;
    }
}

controller调用

@RestController
@RequestMapping("/chatRecord")
public class ChatRecordController {

    @GetMapping("/test")
    public Integer testController(){
        TestEntity testEntity = new TestEntity();
        return testEntity.test();
    }
}

结果
在这里插入图片描述

  • 方案二:初始化容器

普通类中直接初始化

package com.websocket.entity;

import com.websocket.service.TwoChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class TestEntity2 {
    @Autowired
    private static TestEntity2 testEntity2;

    @Autowired
    TwoChatService twoChatService;
    @PostConstruct
    public void init(){
        testEntity2=this;
        testEntity2.twoChatService=this.twoChatService;
    }

    public Integer test2(){
        int count = testEntity2.twoChatService.count();
        return count;
    }

}

controller调用

 @GetMapping("/test2")
    public Integer testController2(){
        TestEntity2 testEntity = new TestEntity2();
        return testEntity.test2();
    }

结果
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XuDream

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值