Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java IllegalStateException-@Component扫描springframework包_Java_Spring_Rest_Maven_Spring Mvc - Fatal编程技术网

Java IllegalStateException-@Component扫描springframework包

Java IllegalStateException-@Component扫描springframework包,java,spring,rest,maven,spring-mvc,Java,Spring,Rest,Maven,Spring Mvc,我一直在学习以下教程: 我最初能够使代码正常工作(运行完成的教程,发送HTTP消息并获得正确的响应),并成功地扩展了它 进一步展开后,我遇到以下异常: java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration#propertySourcesPlaceholderConfig

我一直在学习以下教程:

我最初能够使代码正常工作(运行完成的教程,发送HTTP消息并获得正确的响应),并成功地扩展了它

进一步展开后,我遇到以下异常:

java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration#propertySourcesPlaceholderConfigurer due to internal class not found. This can happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)
    at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:51)
    at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:92)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:174)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:136)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:116)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:330)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:611)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:961)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:950)
    at com.aharrison.hello.Application.main(Application.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
此后,我将代码精简为示例中显示的内容,但我仍然遇到异常

我认为它可能与以下问题有关,尽管它被标记为已关闭:

我对Spring不是很有经验,所以我不能完全理解正在讨论的内容

以下是我目前的课程:

Greeting.java:

package com.aharrison.hello;

public class Greeting {
    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}
迎宾员:

package com.aharrison.hello;

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.RestController;

import java.util.concurrent.atomic.AtomicLong;

@RestController
public class GreetingController {
    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }
}
Application.java:

package com.aharrison.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

/**
 * Created by Adam on 12/26/2014.
 */
@ComponentScan
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.aharrison</groupId>
<artifactId>SpringRestAPI</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>1.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>1.6.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>2.12.2</version>
    </dependency>
</dependencies>

4.0.0
com.aharison
斯普林里斯塔皮
1.0-快照
org.springframework.boot
SpringBootStarterWeb
1.2.0.1发布
org.springframework
弹簧芯
4.1.3.1发布
org.springframework.data
spring数据mongodb
1.6.1.1发布
org.mongodb
mongo java驱动程序
2.12.2

问题:

  • 在这种情况下,是什么导致了异常?我上面的代码是否有问题,或者问题与环境有关
  • 当异常显示“.如果您错误地将@ComponentScan放在默认包中”,它所指的默认包是哪个?这是否适用于目前的情况

  • 提前谢谢

    当我运行您提供的代码时,一切正常。我唯一需要做的更改是在
    pom.xml
    中添加了以下内容:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.0.RELEASE</version>
    </parent>
    
    
    org.springframework.boot
    spring启动程序父级
    1.2.0.1发布
    
    这将启用整个Spring引导机制,并且是启动应用程序所必需的

    有关我的测试运行的成功输出,请参见以下内容:

      .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v1.2.0.RELEASE)
    
    2014-12-27 17:41:12.472  INFO 4065 --- [           main] com.aharrison.hello.Application          : Starting Application on My-MacBook-Pro.local with PID 4065 (/Users/wassgren/test/target/test-classes started by wassgren in /Users/wassgren/test/test-di)
    2014-12-27 17:41:12.506  INFO 4065 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4a668b6e: startup date [Sat Dec 27 17:41:12 CET 2014]; root of context hierarchy
    2014-12-27 17:41:13.407  INFO 4065 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
    2014-12-27 17:41:14.186  INFO 4065 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration' of type [class org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2014-12-27 17:41:14.703  INFO 4065 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080/http
    2014-12-27 17:41:15.047  INFO 4065 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
    2014-12-27 17:41:15.048  INFO 4065 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.15
    2014-12-27 17:41:15.154  INFO 4065 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2014-12-27 17:41:15.154  INFO 4065 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2651 ms
    2014-12-27 17:41:16.399  INFO 4065 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
    2014-12-27 17:41:16.404  INFO 4065 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
    2014-12-27 17:41:16.404  INFO 4065 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
    2014-12-27 17:41:16.907  INFO 4065 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4a668b6e: startup date [Sat Dec 27 17:41:12 CET 2014]; root of context hierarchy
    2014-12-27 17:41:16.979  INFO 4065 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/greeting],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public com.aharrison.hello.Greeting com.aharrison.hello.GreetingController.greeting(java.lang.String)
    2014-12-27 17:41:16.981  INFO 4065 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
    2014-12-27 17:41:16.981  INFO 4065 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
    2014-12-27 17:41:17.013  INFO 4065 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2014-12-27 17:41:17.014  INFO 4065 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2014-12-27 17:41:17.059  INFO 4065 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2014-12-27 17:41:17.206  INFO 4065 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
    2014-12-27 17:41:17.292  INFO 4065 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http
    2014-12-27 17:41:17.294  INFO 4065 --- [           main] com.aharrison.hello.Application          : Started Application in 5.245 seconds (JVM running for 6.088)
    
    。\uuuuuuuuuuuu_
    /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
    \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
    '  |____| .__|_| |_|_| |_\__, | / / / /
    =========|_|==============|___/=/_/_/_/
    ::弹簧启动::(v1.2.0.版本)
    2014-12-27 17:41:12.472 INFO 4065---[main]com.aharison.hello.Application:在My-MacBook-Pro.local上以PID 4065启动应用程序(/Users/wassgren/test/target/test类由wassgren在/Users/wassgren/test/test di中启动)
    2014-12-27 17:41:12.506信息4065---[main]国家配置嵌入式Web应用程序上下文:刷新org.springframework.boot.context.embedded。AnnotationConfigEmbeddedWebApplicationContext@4a668b6e:启动日期[Sat Dec 27 17:41:12 CET 2014];上下文层次结构的根
    2014-12-27 17:41:13.407信息4065---[main]o.s.b.f.s.DefaultListableBeanFactory:重写bean“beanNameViewResolver”的bean定义:替换[根bean:类[null];scope=;abstract=false;lazyInit=false;autowireMode=3;dependencyCheck=0;autowireCandidate=true;primary=false;factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhiteLabeleErrorViewConfiguration;factoryMethodName=BeanNameResolver;initMethodName=null;destroyMethodName=(推断);在类路径资源[org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]]中用[Root bean:class[null]定义;scope=;abstract=false;lazyInit=false;autowireMode=3;dependencyCheck=0;autowireCandidate=true;primary=false;factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfiguration适配器;factoryMethodName=beanNameViewResolver;initMethodName=null;destroyMethodName=(推断);在类路径资源[org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]中定义
    2014-12-27 17:41:14.186信息4065---[main]trationlegate$BeanPostProcessorChecker:Bean'org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration'类型为[class org.springframework.boot.autoconfigurare.dao.PersistenceExceptionTranslationAutoConfiguration]不符合由所有BeanPostProcessor处理的条件(例如:不符合自动代理的条件)
    2014-12-27 17:41:14.703信息4065---[main]s.b.c.e.t.TomcatEmbeddedServletContainer:Tomcat已用端口初始化:8080/http
    2014-12-27 17:41:15.047 INFO 4065---[main]o.apache.catalina.core.StandardService:启动服务Tomcat
    2014-12-27 17:41:15.048 INFO 4065---[main]org.apache.catalina.core.StandardEngine:启动Servlet引擎:apache Tomcat/8.0.15
    2014-12-27 17:41:15.154信息4065---[ost-startStop-1]o.a.c.c.c.[Tomcat].[localhost].[/]:初始化Spring嵌入式WebApplicationContext
    2014-12-27 17:41:15.154信息4065---[ost-startStop-1]o.s.web.context.ContextLoader:根WebApplicationContext:初始化在2651毫秒内完成
    2014-12-27 17:41:16.399信息4065---[ost-startStop-1]o.s.b.c.e.ServletRegistrationBean:将servlet:“dispatcherServlet”映射到[/]
    2014-12-27 17:41:16.404信息4065---[ost-startStop-1]o.s.b.c.embedded.FilterRegistrationBean:将筛选器:“hiddenHttpMethodFilter”映射到:[/*]
    2014-12-27 17:41:16.404信息4065---[ost-startStop-1]o.s.b.c.embedded.FilterRegistrationBean:将筛选器:“characterEncodingFilter”映射到:[/*]
    2014-12-27 17:41:16.907信息4065---[main]s.w.s.m.a.RequestMappingHandlerAdapter:正在寻找@ControllerAdvice:org.springframework.boot.context.embedded。AnnotationConfigEmbeddedWebApplicationContext@4a668b6e:启动日期[Sat Dec 27 17:41:12 CET 2014];上下文层次结构的根
    2014-12-27 17:41:16.979信息4065--[