Java Spring 应用程序不在包外启动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41729712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring application does not start outside of a package
提问by handris
I am following thistutorial to build a basic application with Spring. It is working flawlessly as long as I follow this sub-directory structure:
我正在按照本教程使用 Spring 构建一个基本应用程序。只要我遵循以下子目录结构,它就可以完美运行:
└── src
└── main
└── java
└── hello
If I move my Application.java
and ScheduledTasks.java
classes out of the hello package I get the following error:
如果我将 myApplication.java
和ScheduledTasks.java
classes 移出 hello 包,我会收到以下错误:
** WARNING ** : Your ApplicationContext is unlikely to start due to a `@ComponentScan` of the default package.
And a few seconds later, indeed...
几秒钟后,确实……
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
My question is, why do I need to put my classes into a package? What use does it have? How can I avoid this error? Do I really need to use packages if it is a really simple application?
我的问题是,为什么我需要将我的课程放入一个包中?它有什么用?我怎样才能避免这个错误?如果它是一个非常简单的应用程序,我真的需要使用包吗?
采纳答案by DimaSan
Put your java files again to hello
package.
再次将您的 java 文件hello
打包。
When a class doesn't include a package declaration it is considered to be in the “default package”. The use of the “default package” is generally discouraged, and should be avoided.
当一个类不包含包声明时,它被认为是在“默认包”中。通常不鼓励使用“默认包”,应该避免使用。
It can cause particular problems for Spring Boot applications that use @ComponentScan
, @EntityScan
or @SpringBootApplication
annotations, since every class from every jar, will be read.
这可能会导致春季启动应用程序特定的问题,使用@ComponentScan
,@EntityScan
或@SpringBootApplication
注解,因为从每一个罐子每一个类,将被读取。
Read more here.
在这里阅读更多。
回答by Meg
I moved the class annotated with @SpringBootApplication from the default package to a specific package and it worked.
我将用 @SpringBootApplication 注释的类从默认包移动到特定包并且它起作用了。
回答by Amit
I had created a blank Maven folder where Java folder was the last one. I added class MainApplication.
我创建了一个空白的 Maven 文件夹,其中 Java 文件夹是最后一个。我添加了类 MainApplication。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
Further, I had to create a package within Java folder such as com.test and then I moved my MainApplication class into it. Note "package com.test;" Now this default package Spring boot is looking for.
此外,我必须在 Java 文件夹中创建一个包,例如 com.test,然后我将 MainApplication 类移动到其中。注意“包 com.test;” 现在这个默认包 Spring boot 正在寻找。
package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
Then it worked fine.
然后它工作得很好。