1、异常信息

spring的bean不能注入原因分析

2.有可能引起的原因:

1、在applicationContext.xml的配置文件里的包扫描不对。

 spring的bean不能注入原因分析

2、在web.xml里没有加载spring容器。

 spring的bean不能注入原因分析

3、分布式工程,使用dubbo或者hsf通信,在服务层,或者消费层,单词写错了。

spring的bean不能注入原因分析

spring的bean不能注入原因分析

4、还有一种可能,有可能是pom 里的jar包冲突。

5、从ApplicationContext 获取bean的时候getBean传的参数与配置的bean的id不一致导致无法取到,建议bean的id与接口名保持一致,不要添加或去除字母:

public static RedisUtilService getRedisUtilService() {
        // 此处应为redisUtil
        return (RedisUtilService)getApplicationContext().getBean("redisUtilService");
    }
<hsf:consumer >
                  version="${version}" group="${moon}">
    </hsf:consumer>

6、web.xml的加载顺序与它们在 web.xml 文件中的先后顺序无关。不会因为 filter 写在 listener 的前面而会先加载 filter。加载顺序依次为:listener -> filter -> servlet,所以在filter中是无法使用@autowire注解注入bean的,需要我们手动加载,在过滤器中使用service是需要手动注入的。代码如下:

private static ApplicationContext getApplicationContext() {
        ApplicationContext ac = null;
        try {
            ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ac;
}

public static StaffInfoService getStaffInfoService() {
        return (StaffInfoService)getApplicationContext().getBean("staffInfoService");
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2018-08-22
  • 2021-06-10
  • 2021-09-22
  • 2021-05-24
  • 2021-12-15
猜你喜欢
  • 2021-04-10
  • 2022-12-23
  • 2021-12-08
  • 2021-07-01
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案