spring的bean不能注入原因分析

1、异常信息

2.有可能引起的原因:

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

 

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

 

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

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

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

public class BeanFactory {

       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");

       }

}

猜你喜欢

转载自www.cnblogs.com/wanghaichao/p/9190238.html
今日推荐