我正在关注this https://spring.io/guides/gs/scheduling-tasks/使用 Spring 构建基本应用程序的教程。只要我遵循这个子目录结构,它就可以完美地工作:
└── src
└── main
└── java
└── hello
如果我移动我的Application.java
and ScheduledTasks.java
hello 包中的类我收到以下错误:
** WARNING ** : Your ApplicationContext is unlikely to start due to a `@ComponentScan` of the default package.
几秒钟后,果然……
java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.context.annotation.AnnotationConfigApplicationContext@71fa8894: startup date [Wed Jan 18 22:19:12 CET 2017]; root of context hierarchy
我的问题是,为什么我需要将我的类放入包中?它有什么用呢?我怎样才能避免这个错误?如果它是一个非常简单的应用程序,我真的需要使用包吗?
将你的java文件再次放入hello
包裹。
当一个类不包含包声明时,它被认为位于“默认包”。通常不鼓励并且应该避免使用“默认包”。
它可能会导致使用 Spring Boot 应用程序的特殊问题@ComponentScan
, @EntityScan
or @SpringBootApplication
注释,因为每个罐子的每个班级,将被读取。
阅读更多here http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-structuring-your-code.html.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)