java – IllegalStateException – @ComponentScanning springframework包

前端之家收集整理的这篇文章主要介绍了java – IllegalStateException – @ComponentScanning springframework包前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我一直在研究以下教程:

http://spring.io/guides/gs/rest-service/

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

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

  1. 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)
  2. at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:51)
  3. at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:92)
  4. at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:174)
  5. at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:136)
  6. at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:116)
  7. at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:330)
  8. at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243)
  9. at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
  10. at org.springframework.context.support.PostProcessorRegistrationDelegate.invokebeanfactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
  11. at org.springframework.context.support.AbstractApplicationContext.invokebeanfactoryPostProcessors(AbstractApplicationContext.java:611)
  12. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
  13. at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
  14. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
  15. at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
  16. at org.springframework.boot.SpringApplication.run(SpringApplication.java:961)
  17. at org.springframework.boot.SpringApplication.run(SpringApplication.java:950)
  18. at com.aharrison.hello.Application.main(Application.java:15)
  19. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  20. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  21. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  22. at java.lang.reflect.Method.invoke(Method.java:606)
  23. at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

我已经将代码重新删回到示例中显示内容,但我仍然遇到异常.

我认为它可能与以下问题有关,虽然它标记为已关闭

https://github.com/spring-projects/spring-boot/issues/2050

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

这是我目前的课程:

Greeting.java:

  1. package com.aharrison.hello;
  2. public class Greeting {
  3. private final long id;
  4. private final String content;
  5. public Greeting(long id,String content) {
  6. this.id = id;
  7. this.content = content;
  8. }
  9. public long getId() {
  10. return id;
  11. }
  12. public String getContent() {
  13. return content;
  14. }
  15. }

GreetingController:

  1. package com.aharrison.hello;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RequestMethod;
  4. import org.springframework.web.bind.annotation.RequestParam;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import java.util.concurrent.atomic.AtomicLong;
  7. @RestController
  8. public class GreetingController {
  9. private static final String template = "Hello,%s!";
  10. private final AtomicLong counter = new AtomicLong();
  11. @RequestMapping("/greeting")
  12. public Greeting greeting(@RequestParam(value="name",defaultValue="World") String name) {
  13. return new Greeting(counter.incrementAndGet(),String.format(template,name));
  14. }
  15. }

Application.java:

  1. package com.aharrison.hello;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  4. import org.springframework.context.annotation.ComponentScan;
  5. /**
  6. * Created by Adam on 12/26/2014.
  7. */
  8. @ComponentScan
  9. @EnableAutoConfiguration
  10. public class Application {
  11. public static void main(String[] args) {
  12. SpringApplication.run(Application.class,args);
  13. }
  14. }

pom.xml中:

问题:

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

提前致谢.

最佳答案
当我运行你提供的代码时,一切正常.我必须做的唯一改变是在pom.xml中我添加了以下内容

这使得整个Spring Boot机制成为可能,以便您能够启动应用程序.

请参阅下面的测试运行成功输出

  1. . ____ _ __ _ _
  2. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  3. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  4. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  5. ' |____| .__|_| |_|_| |_\__,| / / / /
  6. =========|_|==============|___/=/_/_/_/
  7. :: Spring Boot :: (v1.2.0.RELEASE)
  8. 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)
  9. 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
  10. 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]]
  11. 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)
  12. 2014-12-27 17:41:14.703 INFO 4065 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080/http
  13. 2014-12-27 17:41:15.047 INFO 4065 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
  14. 2014-12-27 17:41:15.048 INFO 4065 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.15
  15. 2014-12-27 17:41:15.154 INFO 4065 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  16. 2014-12-27 17:41:15.154 INFO 4065 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2651 ms
  17. 2014-12-27 17:41:16.399 INFO 4065 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
  18. 2014-12-27 17:41:16.404 INFO 4065 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
  19. 2014-12-27 17:41:16.404 INFO 4065 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
  20. 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
  21. 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)
  22. 2014-12-27 17:41:16.981 INFO 4065 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],custom=[]}" onto public org.springframework.http.ResponseEntity

猜你在找的Spring相关文章