How to initialize a field with @Autowired annotation in SpringBoot
<p>I try to use @Autowired annotation to initialize a service bean in springboot, but some exceptions accur when the applitaion starting.
Here is the code.</p>

<pre><code>package com.kindlepocket.web.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.kindlepocket.web.service.TextBookInfoSearchService;
import com.kindlepocket.web.util.CheckUtil;
import com.kindlepocket.web.util.MessageUtil;

@RestController
@EnableAutoConfiguration
@RequestMapping("/Weixin")
public class KindlePocketController {

private static final long serialVersionUID = 1L;

@Autowired
private TextBookInfoSearchService searchService;// = new TextBookInfoSearchService();

private static Logger logger = Logger.getLogger(KindlePocketController.class);

@RequestMapping(value = "/wx.do", method = RequestMethod.GET)
@ResponseBody
public void validate(HttpServletRequest request, HttpServletResponse response,
        @RequestParam("signature") String signature, @RequestParam("timestamp") String timestamp,
        @RequestParam("nonce") String nonce, @RequestParam("echostr") String echostr) {

    if (logger.isInfoEnabled()) {
        logger.info("\n***The message got: signature:" + signature + " timestamp:" + timestamp
                + " nonce:" + nonce + " echostr:" + echostr);
    }

    PrintWriter out = null;
    try {
        out = response.getWriter();
    } catch (IOException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("response error");
        }
    }

    if (CheckUtil.checkSignature(signature, timestamp, nonce)) {
        out.print(echostr);
        if (logger.isInfoEnabled()) {
            logger.info("validated!");
        }
    }
}

@RequestMapping(value = "/wx.do", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity processMessage(HttpServletRequest request, HttpServletResponse response)
        throws IOException {

    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");

    /*
     * PrintWriter out = null; try { out = response.getWriter(); } catch (IOException e) { e.printStackTrace(); }
     */

    try {

        Map&lt;String, String&gt; map = MessageUtil.xmlToMap(request);
        String fromUserName = map.get("FromUserName");
        String toUserName = map.get("ToUserName");
        String msgType = map.get("MsgType");
        String content = map.get("Content");

        if (logger.isInfoEnabled()) {
            logger.info("\n***The message got: fromUserName:" + fromUserName + " toUserName:"
                    + toUserName + " msgType:" + msgType + " content:" + content);
        }

        String responseMessage = null;
        if (MessageUtil.MESSAGE_TEXT.equals(msgType)) {

            switch (content) {
            case "1":
                responseMessage = MessageUtil.initText(toUserName, fromUserName, MessageUtil.firstMenu());
                break;
            case "2":
                responseMessage = MessageUtil
                        .initText(toUserName, fromUserName, MessageUtil.secondMenu());
                break;
            case "3":
                responseMessage = MessageUtil.initPicTextMessage(toUserName, fromUserName);
                break;
            default:
                // responseMessage = MessageUtil.initText(toUserName, fromUserName, MessageUtil.menuText());
                List&lt;String&gt; titleList = this.searchService.search(content);
                responseMessage = MessageUtil.initPicTextMessage(toUserName, fromUserName, titleList);
                break;
            }

            /*
             * TextMessage textMessage = new TextMessage(); textMessage.setFromUserName(toUserName); textMessage.setToUserName(fromUserName); textMessage.setMsgType("text"); textMessage.setCreateTime(new Date().getTime()); textMessage.setContent("the message you sent was : " + content); responseMessage = MessageUtil.textMessageToXml(textMessage); if (logger.isInfoEnabled()) { logger.info("the message responsed is :\n" + responseMessage); }
             */
        } else if (MessageUtil.MESSAGE_EVENT.equals(msgType)) {
            String eventType = map.get("Event");
            if (MessageUtil.MESSAGE_SUBSCRIBE.equals(eventType)) {
                responseMessage = MessageUtil.initText(toUserName, fromUserName,
                        MessageUtil.welcomeText());
            }
        }
        if (logger.isInfoEnabled()) {
            logger.info("\n***The message responsed: \n" + responseMessage);
        }
        // out.print(responseMessage);
        return ResponseEntity.status(HttpStatus.OK).body(responseMessage);

    } catch (DocumentException e) {
        e.printStackTrace();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    } /*
       * finally { out.close(); }
       */

}

public static void main(String[] args) throws Exception {
    SpringApplication.run(KindlePocketController.class, args);
}
</code></pre>

<p>}</p>

<p>And following is service.</p>

<pre><code>package com.kindlepocket.web.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

@Component
public class TextBookInfoSearchService {

private ApiService apiService = new ApiService();

public List&lt;String&gt; search(String content) {

    Map&lt;String, Object&gt; contentMap = new HashMap&lt;String, Object&gt;();
    contentMap.put("title", content);
    try {
        String result = this.apiService.doGet("http://127.0.0.1:8081/search/title", contentMap);
        System.out.println("result:" + result);
    } catch (Exception e) {
        e.printStackTrace();
    }

    List&lt;String&gt; titles = new ArrayList&lt;String&gt;();
    // test
    titles.add("张学良口述历史");
    titles.add("布谷鸟的呼唤");
    return titles;

}
</code></pre>

<p>}</p>

<p>And here is the log:</p>

<pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'kindlePocketController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.kindlepocket.web.service.TextBookInfoSearchService com.kindlepocket.web.controller.KindlePocketController.searchService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.kindlepocket.web.service.TextBookInfoSearchService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at com.kindlepocket.web.controller.KindlePocketController.main(KindlePocketController.java:140) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.kindlepocket.web.service.TextBookInfoSearchService com.kindlepocket.web.controller.KindlePocketController.searchService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.kindlepocket.web.service.TextBookInfoSearchService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
... 17 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.kindlepocket.web.service.TextBookInfoSearchService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
... 19 common frames omitted

2016-04-24 22:08:34.805  INFO 16680 --- [           main] .b.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [file:/E:/git_Repo/kindlePocketRepoMine/kindlepocket-web/target/classes/, file:/E:/itcast-workspace/repository/org/springframework/boot/spring-boot-starter-web/1.3.3.RELEASE/spring-boot-starter-web-1.3.3.RELEASE.jar, file:/E:/itcast-workspace/repository/org/springframework/boot/spring-boot-starter/1.3.3.RELEASE/spring-boot-starter-1.3.3.RELEASE.jar, file:/E:/itcast-workspace/repository/org/springframework/boot/spring-boot/1.3.3.RELEASE/spring-boot-1.3.3.RELEASE.jar, file:/E:/itcast-workspace/repository/org/springframework/boot/spring-boot-autoconfigure/1.3.3.RELEASE/spring-boot-autoconfigure-1.3.3.RELEASE.jar, file:/E:/itcast-workspace/repository/org/springframework/boot/spring-boot-starter-logging/1.3.3.RELEASE/spring-boot-starter-logging-1.3.3.RELEASE.jar, file:/E:/itcast-workspace/repository/ch/qos/logback/logback-classic/1.1.5/logback-classic-1.1.5.jar, file:/E:/itcast-workspace/repository/ch/qos/logback/logback-core/1.1.5/logback-core-1.1.5.jar, file:/E:/itcast-workspace/repository/org/slf4j/jcl-over-slf4j/1.7.16/jcl-over-slf4j-1.7.16.jar, file:/E:/itcast-workspace/repository/org/slf4j/jul-to-slf4j/1.7.16/jul-to-slf4j-1.7.16.jar, file:/E:/itcast-workspace/repository/org/slf4j/log4j-over-slf4j/1.7.16/log4j-over-slf4j-1.7.16.jar, file:/E:/itcast-workspace/repository/org/springframework/spring-core/4.2.5.RELEASE/spring-core-4.2.5.RELEASE.jar, file:/E:/itcast-workspace/repository/org/yaml/snakeyaml/1.16/snakeyaml-1.16.jar, file:/E:/itcast-workspace/repository/org/springframework/boot/spring-boot-starter-tomcat/1.3.3.RELEASE/spring-boot-starter-tomcat-1.3.3.RELEASE.jar, file:/E:/itcast-workspace/repository/org/apache/tomcat/embed/tomcat-embed-core/8.0.32/tomcat-embed-core-8.0.32.jar, file:/E:/itcast-workspace/repository/org/apache/tomcat/embed/tomcat-embed-el/8.0.32/tomcat-embed-el-8.0.32.jar, file:/E:/itcast-workspace/repository/org/apache/tomcat/embed/tomcat-embed-logging-juli/8.0.32/tomcat-embed-logging-juli-8.0.32.jar, file:/E:/itcast-workspace/repository/org/apache/tomcat/embed/tomcat-embed-websocket/8.0.32/tomcat-embed-websocket-8.0.32.jar, file:/E:/itcast-workspace/repository/org/springframework/boot/spring-boot-starter-validation/1.3.3.RELEASE/spring-boot-starter-validation-1.3.3.RELEASE.jar, file:/E:/itcast-workspace/repository/org/hibernate/hibernate-validator/5.2.4.Final/hibernate-validator-5.2.4.Final.jar, file:/E:/itcast-workspace/repository/javax/validation/validation-api/1.1.0.Final/validation-api-1.1.0.Final.jar, file:/E:/itcast-workspace/repository/org/jboss/logging/jboss-logging/3.3.0.Final/jboss-logging-3.3.0.Final.jar, file:/E:/itcast-workspace/repository/com/fasterxml/classmate/1.1.0/classmate-1.1.0.jar, file:/E:/itcast-workspace/repository/org/springframework/spring-web/4.2.5.RELEASE/spring-web-4.2.5.RELEASE.jar, file:/E:/itcast-workspace/repository/org/springframework/spring-aop/4.2.5.RELEASE/spring-aop-4.2.5.RELEASE.jar, file:/E:/itcast-workspace/repository/aopalliance/aopalliance/1.0/aopalliance-1.0.jar, file:/E:/itcast-workspace/repository/org/springframework/spring-beans/4.2.5.RELEASE/spring-beans-4.2.5.RELEASE.jar, file:/E:/itcast-workspace/repository/org/springframework/spring-context/4.2.5.RELEASE/spring-context-4.2.5.RELEASE.jar, file:/E:/itcast-workspace/repository/org/springframework/spring-webmvc/4.2.5.RELEASE/spring-webmvc-4.2.5.RELEASE.jar, file:/E:/itcast-workspace/repository/org/springframework/spring-expression/4.2.5.RELEASE/spring-expression-4.2.5.RELEASE.jar, file:/E:/itcast-workspace/repository/dom4j/dom4j/1.6.1/dom4j-1.6.1.jar, file:/E:/itcast-workspace/repository/xml-apis/xml-apis/1.0.b2/xml-apis-1.0.b2.jar, file:/E:/itcast-workspace/repository/log4j/log4j/1.2.16/log4j-1.2.16.jar, file:/E:/itcast-workspace/repository/xpp3/xpp3/1.1.4c/xpp3-1.1.4c.jar, file:/E:/itcast-workspace/repository/com/thoughtworks/xstream/xstream/1.3.1/xstream-1.3.1.jar, file:/E:/itcast-workspace/repository/xpp3/xpp3_min/1.1.4c/xpp3_min-1.1.4c.jar, file:/E:/itcast-workspace/repository/org/apache/solr/solr-solrj/4.10.2/solr-solrj-4.10.2.jar, file:/E:/itcast-workspace/repository/org/apache/httpcomponents/httpcore/4.4.4/httpcore-4.4.4.jar, file:/E:/itcast-workspace/repository/org/apache/httpcomponents/httpmime/4.5.1/httpmime-4.5.1.jar, file:/E:/itcast-workspace/repository/org/apache/zookeeper/zookeeper/3.4.6/zookeeper-3.4.6.jar, file:/E:/itcast-workspace/repository/org/codehaus/woodstox/wstx-asl/3.2.7/wstx-asl-3.2.7.jar, file:/E:/itcast-workspace/repository/org/noggit/noggit/0.5/noggit-0.5.jar, file:/E:/itcast-workspace/repository/org/slf4j/slf4j-api/1.7.16/slf4j-api-1.7.16.jar, file:/E:/itcast-workspace/repository/org/apache/httpcomponents/httpclient/4.3.5/httpclient-4.3.5.jar, file:/E:/itcast-workspace/repository/commons-codec/commons-codec/1.6/commons-codec-1.6.jar, file:/E:/itcast-workspace/repository/org/apache/commons/commons-lang3/3.3.2/commons-lang3-3.3.2.jar, file:/E:/itcast-workspace/repository/commons-io/commons-io/1.3.2/commons-io-1.3.2.jar, file:/E:/itcast-workspace/repository/com/fasterxml/jackson/core/jackson-databind/2.6.5/jackson-databind-2.6.5.jar, file:/E:/itcast-workspace/repository/com/fasterxml/jackson/core/jackson-annotations/2.6.5/jackson-annotations-2.6.5.jar, file:/E:/itcast-workspace/repository/com/fasterxml/jackson/core/jackson-core/2.6.5/jackson-core-2.6.5.jar]
</code></pre>

<p>And pom.xml follows:
    `    http://maven.apache.org/xsd/maven-4.0.0.xsd">
       4.0.0
       com.kindlepocket.web
       kindlepocket-web
       0.0.1-SNAPSHOT</p>

<pre><code>  &lt;parent&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
    &lt;version&gt;1.3.3.RELEASE&lt;/version&gt;
&lt;/parent&gt;
&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;dom4j&lt;/groupId&gt;
        &lt;artifactId&gt;dom4j&lt;/artifactId&gt;
        &lt;version&gt;1.6.1&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;log4j&lt;/groupId&gt;
        &lt;artifactId&gt;log4j&lt;/artifactId&gt;
        &lt;version&gt;1.2.16&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;xpp3&lt;/groupId&gt;
        &lt;artifactId&gt;xpp3&lt;/artifactId&gt;
        &lt;version&gt;1.1.4c&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;com.thoughtworks.xstream&lt;/groupId&gt;
        &lt;artifactId&gt;xstream&lt;/artifactId&gt;
        &lt;version&gt;1.3.1&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;org.apache.solr&lt;/groupId&gt;
        &lt;artifactId&gt;solr-solrj&lt;/artifactId&gt;
        &lt;version&gt;4.10.2&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt;
        &lt;artifactId&gt;httpclient&lt;/artifactId&gt;
        &lt;version&gt;4.3.5&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
        &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
        &lt;version&gt;3.3.2&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
        &lt;artifactId&gt;commons-io&lt;/artifactId&gt;
        &lt;version&gt;1.3.2&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
        &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
    &lt;/dependency&gt;

&lt;/dependencies&gt;

&lt;plugins&gt;
    &lt;plugin&gt;
        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
        &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
        &lt;version&gt;3.2&lt;/version&gt;
        &lt;configuration&gt;
            &lt;source&gt;1.8&lt;/source&gt;
            &lt;target&gt;1.8&lt;/target&gt;
            &lt;encoding&gt;UTF-8&lt;/encoding&gt;
        &lt;/configuration&gt;
    &lt;/plugin&gt;
&lt;/plugins&gt;
</code></pre>

<p>`
I don't know why the injection failed.
Did I missed some configurations ? Or the annotation is not correct?
Thanks everyone .</p>