---
title: "spring的xml配置与annotation注解混合使用无法Autowired的问题"
author: "fly0wings"
date: "2014-06-22T12:34:32Z"
tags: []
categories: ["编程语言"]
slug: "spring-xml-annotation-autowired"
draft: false
lastmod: "2014-06-22T12:34:32Z"
id: 2083398
url: "https://fly0wing.iteye.com/blog/2083398"
---

## 问题:

在LVMMCrawlerSuit.java是xml配置的bean, 需要注入用@Component声明的bean. 但是运行的时候却报NullPointerException. 说明没有注入进来.

## []()代码:

1\. java

Java代码  [![收藏代码](http://fly0wing201406100924.iteye.com/images/icon_star.png)]()

01. 1. LVMMCrawlerSuit.java
02. public class LVMMCrawlerSuit extends AbstractCrawlerSuit{
03. @Resource
04. private LVMMURLBuilder lvmmurlBuilder;
05. public LVMMCrawlerSuit() {
06. }
07. }
08. 2. LVMMURLBuilder.java
09. @Component
10. public class LVMMURLBuilder extends AbstractResourceURLBuilder {
11. public LVMMURLBuilder() {    }
12. }

 

2\. 配置.

Xml代码  [![收藏代码](http://fly0wing201406100924.iteye.com/images/icon_star.png)]()

1. &lt;bean id="LVMMCrawlerSuit" class = 'com.qunar.b2c.crawlersuit.impl.LVMMCrawlerSuit'/&gt;

## []()问题查找:

1\. 查找网上资源,未果.

2\. 果断debug,跟踪源码.

       将断点定位到 org.springframework.context.support.AbstractRefreshableApplicationContext#loadBeanDefinitions ,该方法是加载bean的必经之路.跟踪发现,该方法共执行两次,生成了两个不同的 org.springframework.beans.factory.support.DefaultListableBeanFactory, 并且后者的parentBeanFactory为前者,根据原设计是后者可以调用前者的bean 并完成注入.

      现在报NullPointerException,很明显是"父调用子",所以肯定拿不到.在打印的log中进行了佐证.

      在debug中发现,两次执行分别来自不同的beans资源文件: app-web.xml 和 applicationContext-\*.xml, 按key查找,很容易找到了配置信息如下.

Xml代码  [![收藏代码](http://fly0wing201406100924.iteye.com/images/icon_star.png)]()

01. &lt;servlet&gt;
02. &lt;servlet-name&gt;dispatcherServlet&lt;/servlet-name&gt;
03. &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
04. &lt;init-param&gt;
05. &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
06. &lt;param-value&gt;
07. classpath:classpath:app-web.xml
08. &lt;/param-value&gt;
09. &lt;/init-param&gt;
10. &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
11. &lt;/servlet&gt;
12. &lt;context-param&gt;
13. &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
14. &lt;param-value&gt;classpath:applicationContext-\*.xml&lt;/param-value&gt;
15. &lt;/context-param&gt;

 

    既然,两次加载,并且加载了不同的beans,虽然有父子的层级关系,但是限制多多. 那么就尝试合二为一.

    在test中,发现因为修改了spring默认加载的文件名,所以删除任何一个配置都不能正确运行.那么就全部设置成一样的吧. test success......

## []()解决方案:

    方案一. 将配置文件路径合并, 分别指定给不同配置.

Xml代码  [![收藏代码](http://fly0wing201406100924.iteye.com/images/icon_star.png)]()

01. &lt;servlet&gt;
02. &lt;servlet-name&gt;dispatcherServlet&lt;/servlet-name&gt;
03. &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
04. &lt;init-param&gt;
05. &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
06. &lt;param-value&gt;
07. classpath:applicationContext-\*.xml,classpath:app-web.xml
08. &lt;/param-value&gt;
09. &lt;/init-param&gt;
10. &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
11. &lt;/servlet&gt;
12. &lt;context-param&gt;
13. &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
14. &lt;param-value&gt;classpath:applicationContext-\*.xml,classpath:app-web.xml&lt;/param-value&gt;
15. &lt;/context-param&gt;

 

 

    方案二. 原有配置不变,合理规划Bean的定义及合理使用.

         在方案一中, 使用的简单,粗暴的解决办法. 没有考虑到spring的设计思想. 既然有ioc容器的父子级划分,那么在使用的时候,一定会有用的.

         在使用annotation定义bean 的时候,是需要增加如下代码,对使用何种注解的类才管理到ioc容器中.

 

Xml代码  [![收藏代码](http://fly0wing201406100924.iteye.com/images/icon_star.png)]()

1. &lt;context:component-scan base-package="com.qunar.b2c"&gt;
2. &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /&gt;
3. &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /&gt;
4. &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /&gt;
5. &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.Component" /&gt;
6. &lt;/context:component-scan&gt;

 

        上述提到, 在 spring web的使用中, 会加载两个ioc容器,

          1. 一个是contextConfigLocation定义,用来启动spring核心框架的. 所以在该步骤中,应加载应用中的基础服务信息的bean,如 dao,Service 等等.

          2. 另外一个ioc容器是web加载的容器, 那么只需加载Controller相关的bean.

        因为在spring ioc的 DefaultListableBeanFactory类是支持父子关系,

            1. 子容器是可以访问到父容器中的bean,

            2. 然而父容器访问不了子容器的bean,

        这就保证了, Controller可以访问 Service等, 但是Service 访问不了web层的bean, 这样就将职责分开了.所以修改的配置如下:

 

Xml代码  [![收藏代码](http://fly0wing201406100924.iteye.com/images/icon_star.png)]()

01. applicationContext-beans.xml:
02. &lt;context:component-scan base-package="com.qunar.b2c"&gt;
03. &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /&gt;
04. &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /&gt;
05. &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.Component" /&gt;
06. &lt;/context:component-scan&gt;
07. app-web.xml
08. &lt;context:component-scan base-package="com.qunar.b2c"&gt;
09. &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /&gt;
10. &lt;/context:component-scan&gt;

 

     在开发定义bean的时候, 也需要注意,把bean定义到哪一层级.