咨询spring的bean注入问题?

目前为了将方法抽离出来,选择了静态方法的操作,但是静态方法里面需要调用service,而autowired无法在静态方法里面使用,所有网上找了下别人的写法,99LRC歌词工具类如下,

@Component

public final class SpringUtils implements ApplicationContextAware {

    public static ApplicationContext applicationContext = null;

    @Override

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        if (SpringUtils.applicationContext == null){

            SpringUtils.applicationContext = applicationContext;

            System.out.println(

                    "========ApplicationContext配置成功,在普通类可以通过调用ToolSpring.getAppContext()获取applicationContext对象,applicationContext="

                            + applicationContext + "========");

扫描二维码关注公众号,回复: 10654748 查看本文章

        }

    }

    public static ApplicationContext getApplicationContext(){

        return applicationContext;

    }

    //通过

    public static Object getBean(String name) {

        return getApplicationContext().getBean(name);

    }

    //通过class获取Bean.

    public static <T> T getBean(Class<T> clazz){

        return getApplicationContext().getBean(clazz);

    }

}

静态方法调用如下:

 public static void doGetAuthenticationInfo(ChannelHandlerContext ctx,MqttConnectMessage mqttConnectMessage){

        //获取客户端标识

        String clientIdentifier = mqttConnectMessage.payload().clientIdentifier();

        //获取设备账号密码

        String userName = mqttConnectMessage.payload().userName();

        byte[] bytePassword = mqttConnectMessage.payload().passwordInBytes();

        String password = new String(bytePassword);

        //数据库查询是否有该信息

        ClientsInfo clientsInfo = SpringUtils.getApplicationContext().getBean(ClientsInfoService.class).findUserInfo(clientIdentifier, userName, password);

         System.out.println("客户信息"+clientsInfo);

        //对客户机发送信息

        sendMqttConnAckMessage(ctx,clientsInfo);

    }

问题:使用第一项打包方式,提示 Error creating bean with name 'clientsInfoServiceImpl': 用第二个打包方式则可以正常 跑,@service,@mapper都加了的,无法注入bean,求解;或者有没有别的方法呢?对象.方法?

2020-03-20 14:53:18org.springframework.boot.SpringApplication.reportFailure(SpringApplication.java:826)SpringApplication.javaERRORApplication run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientsInfoServiceImpl': Unsatisfied dependency expressed through field 'clientsInfoMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 

setApplicationContext 方法上加@Autowired,指导他需要注入值。

第二种方法,SpringUtils 使用单例模式,然后使用@Configuration+@Bean 以 SpringUtils.getInstance()的使用让spring帮你注入值。

注入静态成员变量这样搞

@Component

public class RedisPool {

    private static JedisPool jedisPool;

    @Autowired

    private JedisPool jp;

    @PostConstruct

    public void init() {

        jedisPool = jp;

    }

......

我这样试过了,不行,直接在我调用方法的类里面这样写了,拿不到,静态变量是空值

把你的final去掉,然后让你家@autowired的加了吗?

我感觉好像不是我代码问题,而是我idea打包的问题,打进去的包,找不到依赖包,然后就抛出错误

可以进行手动注入啊 先测试在打包

已经找出问题了,是打包问题,不是代码出问题了!

发布了122 篇原创文章 · 获赞 2 · 访问量 5217

猜你喜欢

转载自blog.csdn.net/liuji0517/article/details/105065348
今日推荐