Java 由于缺少 bean,Web 服务器无法启动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21796811/
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
Webserver fails to start due to missing bean
提问by Bula
I'm trying to start a simple spring application
我正在尝试启动一个简单的 spring 应用程序
I have the main.java file right here:
我这里有 main.java 文件:
package main;
import javafx.application.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
@EnableAutoConfiguration
public class Main {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Here are the libs that I have:
这是我拥有的库:
Here is pom.xml:
这是 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>blog</groupId>
<artifactId>blog</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
And finally the error is this one:
最后的错误是这个:
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.0.0.BUILD-SNAPSHOT)
[2014-02-15 11:14:25.385] - 6116 INFO [main] --- main.Main: Starting Main on Vlad-PC with PID 6116 (C:\Users\Vlad\IdeaProjects\blog\target\classes started by Vlad)
[2014-02-15 11:14:25.473] - 6116 INFO [main] --- org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext: Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2ca76650: startup date [Sat Feb 15 11:14:25 GMT 2014]; root of context hierarchy
[2014-02-15 11:14:25.579] - 6116 INFO [main] --- org.springframework.boot.logging.ClasspathLoggingApplicationListener: Application failed to start with classpath: [file:/C:/Program%20Files/Java/jdk1.7.0_51/jre/lib/charsets.jar, file:/C:/Program%20Files/Java/jdk1.7.0_51/jre/lib/deploy.jar, file:/C:/Program%20Files/Java/jdk1.7.0_51/jre/lib/javaws.jar, file:/C:/Program%20Files/Java/jdk1.7.0_51/jre/lib/jce.jar, file:/C:/Program%20Files/Java/jdk1.7.0_51/jre/lib/jfr.jar, file:/C:/Program%20Files/Java/jdk1.7.0_51/jre/lib/jfxrt.jar, file:/C:/Program%20Files/Java/jdk1.7.0_51/jre/lib/jsse.jar, file:/C:/Program%20Files/Java/jdk1.7.0_51/jre/lib/management-agent.jar, file:/C:/Program%20Files/Java/jdk1.7.0_51/jre/lib/plugin.jar, file:/C:/Program%20Files/Java/jdk1.7.0_51/jre/lib/resources.jar, file:/C:/Program%20Files/Java/jdk1.7.0_51/jre/lib/rt.jar, file:/C:/Program%20Files/Java/jdk1.7.0_51/jre/lib/ext/access-bridge-64.jar, file:/C:/Program%20Files/Java/jdk1.7.0_51/jre/lib/ext/dnsns.jar, file:/C:/Program%20Files/Java/jdk1.7.0_51/jre/lib/ext/jaccess.jar, file:/C:/Program%20Files/Java/jdk1.7.0_51/jre/lib/ext/localedata.jar, file:/C:/Program%20Files/Java/jdk1.7.0_51/jre/lib/ext/sunec.jar, file:/C:/Program%20Files/Java/jdk1.7.0_51/jre/lib/ext/sunjce_provider.jar, file:/C:/Program%20Files/Java/jdk1.7.0_51/jre/lib/ext/sunmscapi.jar, file:/C:/Program%20Files/Java/jdk1.7.0_51/jre/lib/ext/zipfs.jar, file:/C:/Users/Vlad/IdeaProjects/blog/target/classes/, file:/C:/Users/Vlad/IdeaProjects/blog/lib/com.springsource.org.apache.tomcat.api-7.0.12.jar, file:/C:/Users/Vlad/IdeaProjects/blog/lib/javax.servlet-3.0.0.v201103241009.jar, file:/C:/Users/Vlad/.m2/repository/org/springframework/spring-web/4.0.0.RELEASE/spring-web-4.0.0.RELEASE.jar, file:/C:/Users/Vlad/.m2/repository/aopalliance/aopalliance/1.0/aopalliance-1.0.jar, file:/C:/Users/Vlad/.m2/repository/org/springframework/spring-aop/4.0.0.RELEASE/spring-aop-4.0.0.RELEASE.jar, file:/C:/Users/Vlad/.m2/repository/org/springframework/spring-beans/4.0.0.RELEASE/spring-beans-4.0.0.RELEASE.jar, file:/C:/Users/Vlad/.m2/repository/org/springframework/spring-core/4.0.0.RELEASE/spring-core-4.0.0.RELEASE.jar, file:/C:/Users/Vlad/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar, file:/C:/Users/Vlad/.m2/repository/org/springframework/spring-context/4.0.0.RELEASE/spring-context-4.0.0.RELEASE.jar, file:/C:/Users/Vlad/.m2/repository/org/springframework/spring-expression/4.0.0.RELEASE/spring-expression-4.0.0.RELEASE.jar, file:/C:/Users/Vlad/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/1.0.0.BUILD-SNAPSHOT/spring-boot-autoconfigure-1.0.0.BUILD-SNAPSHOT.jar, file:/C:/Users/Vlad/.m2/repository/org/springframework/boot/spring-boot/1.0.0.BUILD-SNAPSHOT/spring-boot-1.0.0.BUILD-SNAPSHOT.jar, file:/C:/Program%20Files%20(x86)/JetBrains/IntelliJ%20IDEA%20Community%20Edition%2013.0.2/lib/idea_rt.jar]
Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:136)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:616)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:306)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:877)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:866)
at main.Main.main(Main.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:186)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:159)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)
... 12 more
From what I can understand I'm missing a bean? However it looks as if I do have the jar spring beans.
据我所知,我错过了一个豆子?然而,看起来我确实有罐装春豆。
采纳答案by Ralph
Spring boot try to start a web server, but did not findy any in the class path. Try to add this dependency
Spring boot 尝试启动 Web 服务器,但在类路径中没有找到任何内容。尝试添加此依赖项
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>I Dont know</version
</dependency>
</dependencies>
Btw you pom does not look like a typical spring boot pom! Compare it with http://projects.spring.io/spring-boot/docs/README.html
顺便说一句,你的 pom 看起来不像典型的弹簧靴 pom!与http://projects.spring.io/spring-boot/docs/README.html进行比较
回答by devHymanie
There are differences depending on the version.
根据版本不同会有差异。
org.springframework -> Please correct to a higher version. :)
org.springframework -> 请更正到更高版本。:)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
</dependencies>
For example, a org.springframework.boot 1.2.3.RELEASE, error was always when it is org.springframework 4.1.4.RELEASE below version.
例如,一个 org.springframework.boot 1.2.3.RELEASE,当它是 org.springframework 4.1.4.RELEASE 版本以下时总是出错。
In the case of more than org.springframework 4.1.5.RELEASE, I worked without error.
在超过 org.springframework 4.1.5.RELEASE 的情况下,我工作没有错误。
回答by Simon
I had the same error now and I solved it by just including the same versions of the Spring packages in my build.gradle file:
我现在遇到了同样的错误,我通过在 build.gradle 文件中包含相同版本的 Spring 包来解决它:
buildscript {
ext {
springBootVersion = '1.1.6.RELEASE'
}
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-tomcat'
compile 'org.springframework.boot:spring-boot-starter-security'
compile 'org.springframework.boot:spring-boot-starter-actuator'
compile 'org.springframework.boot:spring-boot-starter-aop'
compile 'org.springframework.boot:spring-boot-starter-data-mongodb'
For example, if you changed one of the packages and you add on a version number to override the static value of 1.1.6.RELEASE, then the error will be displayed:
例如,如果您更改了其中一个包并添加了一个版本号以覆盖 1.1.6.RELEASE 的静态值,则将显示错误:
buildscript {
ext {
springBootVersion = '1.1.6.RELEASE'
}
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-tomcat'
compile 'org.springframework.boot:spring-boot-starter-security'
compile 'org.springframework.boot:spring-boot-starter-actuator'
compile 'org.springframework.boot:spring-boot-starter-aop'
compile 'org.springframework.boot:spring-boot-starter-data-mongodb:1.2.5.RELEASE'
回答by ttati
you need to add the annotation:
您需要添加注释:
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
回答by ttati
I might be late to the party but I might just save someone hours of agony looking for the solution.I had that same problem while setting up a spring-boot project.
我可能会迟到,但我可能会为寻找解决方案的人节省几个小时的痛苦。我在设置 spring-boot 项目时遇到了同样的问题。
I had this in my build.gradle
我的 build.gradle 中有这个
compile group: 'org.springframework.boot', name: 'spring-boot-starter-
web', version: '1.5.9.RELEASE'
//compile group: 'org.springframework', name: 'spring-context',
version: '5.0.2.RELEASE'// <--- Source of my problems
compile group: 'org.springframework.security.oauth', name: 'spring-
security-oauth2', version: '2.2.1.RELEASE'
compile group: 'com.fasterxml.Hymanson.datatype', name: 'Hymanson-
datatype-jsr310', version: '2.9.3'
testCompile group: 'com.h2database', name: 'h2', version: '1.4.196'
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'
compile group: 'org.hibernate', name: 'hibernate-core', version:
'5.2.12.Final'
compile group: 'commons-io', name: 'commons-io', version: '2.6'
compile group: 'com.google.guava', name: 'guava', version: '23.6-jre'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-
data-jpa', version: '1.5.9.RELEASE'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.springframework.boot', name: 'spring-boot-
starter-test', version: '1.5.9.RELEASE'
This is a spring-boot project and so, it will do a lot of configuration for you
这是一个 spring-boot 项目,所以它会为你做很多配置
That said, you do not need some the many dependencies you would have to put if it was a spring project. The boot-starterdependencies are enough.
也就是说,如果它是一个 spring 项目,你不需要一些你必须放置的许多依赖项。在引导启动依赖是不够的。