Spring应用程序不会在包之外启动

han*_*ris 41 java spring

我正在按照教程使用Spring构建一个基本应用程序.只要我遵循这个子目录结构,它就能完美运行:

??? src
    ??? main
        ??? java
            ??? hello
Run Code Online (Sandbox Code Playgroud)

如果我将我的Application.javaScheduledTasks.java类移出hello包,我会收到以下错误:

** WARNING ** : Your ApplicationContext is unlikely to start due to a `@ComponentScan` of the default package.
Run Code Online (Sandbox Code Playgroud)

几秒钟后,确实......

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
Run Code Online (Sandbox Code Playgroud)

我的问题是,为什么我需要将我的课程放入包中?它有什么用?我怎样才能避免这个错误?如果它是一个非常简单的应用程序,我真的需要使用包吗?

Dim*_*San 83

将您的java文件重新hello包装到包中.

当类不包含包声明时,它被认为是在"默认包"中.通常不鼓励使用"默认包",应该避免使用.

这可能会导致使用Spring的启动应用程序的特殊问题@ComponentScan,@EntityScan@SpringBootApplication注解,因为从每一个罐子每一个类,将被读取.

在这里阅读更多.


小智 14

我将使用@SpringBootApplication注释的类从默认包移动到特定的包,它工作正常.