我关注 this使用 Spring 构建基本应用程序的教程。只要我遵循这个子目录结构,它就可以完美地工作:
└── src
└── main
└── java
└── hello
如果我将 Application.java
和 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
包。
当一个类不包含包声明时,它被认为在“默认包”中。通常不鼓励使用“默认包”,应避免使用。
对于使用 @ComponentScan
、@EntityScan
或 @SpringBootApplication
注释的 Spring Boot 应用程序,它可能会导致特定问题,因为 < em>每个 jar 中的每个类都会被读取。
阅读更多 here .
关于java - Spring 应用程序不会在包之外启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41729712/