咨询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 + "========");

        }

    }

 

    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打包的问题,打进去的包,找不到依赖包,然后就抛出错误

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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值