您的位置:首页 > 编程语言 > Java开发

关于spring boot无法自动注入bean问题解决方案

2017-09-11 16:25 1211 查看
Description:

Field demoService in com.spring.web.DemoApplication required a bean of type 'com.spring.service.DemoService' that could not be found.

Action:

Consider defining a bean of type 'com.spring.service.DemoService' in your configuration.

谷歌翻译为:

com.spring.web.DemoApplication中的field demoService需要无法找到类型为“com.spring.service.DemoService”的bean。

我的代码:
controller:DemoApplication

package com.spring.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.spring.service.DemoService;
@RestController
//@EnableAutoConfiguration
@SpringBootApplication()
public class DemoApplication {

@Autowired
private DemoService demoService;

@RequestMapping("/")
public String helloWorld(){
String msg =  demoService.demo();
return msg;
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

Service:DemoService

package com.spring.service;

public interface DemoService {
public String demo();
}


ServiceImpl:DemoServiceImpl

package com.spring.service.impl;

import org.springframework.stereotype.Service;

import com.spring.service.DemoService;
@Component
public class DemoServiceImpl implements DemoService {

@Override
public String demo() {
System.out.println("Hello World!");
return "Hello World!";
}

}

报错:
.   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/  ___)| |_)| | | | | || (_| |  ) ) ) )
'  |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::        (v1.5.6.RELEASE)

2017-09-11 16:16:10.367  INFO 14512 --- [           main] com.spring.web.DemoApplication           : Starting DemoApplication on PC2014101864 with PID 14512 (E:\workspacedubbo\boot\boot-web\target\classes started by Administrator in E:\workspacedubbo\boot\boot-web)
2017-09-11 16:16:10.369  INFO 14512 --- [           main] com.spring.web.DemoApplication           : No active profile set, falling back to default profiles: default
2017-09-11 16:16:10.421  INFO 14512 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@31206beb: startup date [Mon Sep 11 16:16:10 CST 2017]; root of context hierarchy
2017-09-11 16:16:11.710  INFO 14512 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-09-11 16:16:11.721  INFO 14512 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2017-09-11 16:16:11.722  INFO 14512 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.16
2017-09-11 16:16:11.856  INFO 14512 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-09-11 16:16:11.856  INFO 14512 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1438 ms
2017-09-11 16:16:11.992  INFO 14512 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-09-11 16:16:11.995  INFO 14512 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-09-11 16:16:11.995  INFO 14512 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-09-11 16:16:11.995  INFO 14512 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-09-11 16:16:11.995  INFO 14512 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-09-11 16:16:12.025  WARN 14512 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demoApplication': Unsatisfied dependency expressed through field 'demoService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.spring.service.DemoService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2017-09-11 16:16:12.026  INFO 14512 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2017-09-11 16:16:12.038  INFO 14512 --- [           main] utoConfigurationReportLoggingInitializer :

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-09-11 16:16:12.107 ERROR 14512 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   :

***************************
APPLICATION FAILED TO START
***************************

Description:

Field demoService in com.spring.web.DemoApplication required a bean of type 'com.spring.service.DemoService' that could not be found.

Action:

Consider defining a bean of type 'com.spring.service.DemoService' in your configuration.

解决办法:
根据英文的提示是在配置中找不到一个指定自动注入类型的bean,

网上搜索得知,

SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描!

“Application类”是指SpringBoot项目入口类。这个类的位置很关键:

如果Application类所在的包为:com.boot.app,则只会扫描com.boot.app包及其所有子包,如果service或dao所在包不在com.boot.app及其子包下,则不会被扫描!

即, 把Application类放到dao、service所在包的上级,com.boot.Application

知道这一点非常关键,不知道Spring文档里有没有给出说明,如果不知道还真是无从解决。

经过多方排查得出结论:

  正常情况下加上@Component注解的类会自动被Spring扫描到生成Bean注册到spring容器中,既然他说没找到,也就是该注解被没有被spring识别,问题的核心关键就在application类的注解SpringBootApplication上,
这个注解其实相当于下面这一堆注解的效果,其中一个注解就是@Component,在默认情况下只能扫描与控制器在同一个包下以及其子包下的@Component注解,以及能将指定注解的类自动注册为Bean的@Service@Controller和@ Repository,至此明白问题所在,之前我将接口与对应实现类放在了与控制器所在包的同一级目录下,这样的注解自然是无法被识别的。



      



之前我的controller和service是在同级目录下面现把controller放在service的上级目录,再启动就不报错了。



至此,得出两种解决办法:

  1 .将接口与对应的实现类放在与application启动类的同一个目录或者他的子目录下,这样注解可以被扫描到,这是最省事的办法

  2 .在指定的application类上加上这么一行注解,手动指定application类要扫描哪些包下的注解,见下图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring
相关文章推荐