尝试设置简单的Web应用程序调度程序。所以这是我的配置:
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>scheduler</artifactId>
<version>0.1.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.1.7.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<start-class>hello.Application</start-class>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>hello.Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>http://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>http://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
我的课程如下所示
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application implements WebApplicationInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(ScheduledTasks.class);
}
public void onStartup(ServletContext arg0) throws ServletException {
SpringApplication.run(ScheduledTasks.class);
}
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.setPort(8081);
factory.setSessionTimeout(50, TimeUnit.MINUTES);
return factory;
}
}
@EnableScheduling
public class ScheduledTasks {
@Scheduled(fixedRate = 500)
public void reportCurrentTime() {
System.out.println("Testing successful ");
}
}
但是当我尝试启动容器时,我看到异常
ERROR 2592 --- [ost-startStop-1] o.s.boot.SpringApplication : Application startup failed
...
...
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:174)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:147)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:121)
... 20 more
请建议,谢谢
答案 0 :(得分:6)
您只是在运行ScheduledTasks
:
SpringApplication.run(ScheduledTasks.class);
何时应该运行:
SpringApplication.run(Application.class);
运行ScheduledTasks意味着Spring Boot不知道Application
中的配置。至关重要的是,它意味着它没有看到@EnableAutoConfiguration
哪个开启了自动配置,并且因为你在类路径上有Tomcat,所以创建了嵌入式Tomcat实例。
我会通过将@EnableScheduling
注释从ScheduledTasks
移至Application
,运行Application.class
而不是ScheduledTasks.class
并注释ScheduledTasks
来解决此问题。与@Component
:
<强> Application.class:强>
@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableScheduling
public class Application implements WebApplicationInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class);
}
// …
}
<强> ScheduledTasks.class 强>:
@Component
public class ScheduledTasks {
@Scheduled(fixedRate = 500)
public void reportCurrentTime() {
System.out.println("Testing successful ");
}
}
移动@EnableScheduling
意味着它在配置类上,它所属的位置。运行Application.class
意味着Spring Boot会查看其所有配置,包括启用组件扫描和启用自动配置。使用ScheduledTasks
注释@Component
表示通过组件扫描找到它并检测到其@Scheduled
方法。
这应该可以解决问题。我也会纠正你的pom,这样你就不会使用Spring Boot的混合版本(你现在有1.1.6和1.1.7)并摆脱你的EmbeddedServletContainerFactory
bean而转而支持使用application.properties
中的src/main/resources
来配置端口和会话超时:
<强> application.properties:强>
server.port = 8081
server.session-timeout = 3000
答案 1 :(得分:0)
除了@Andy Wilkinson指出在OP的情况下哪个是真的但在我的情况下不是
我看到我的spring-boot-starter-tomcat与
<scope>provided</scope>
(因为我添加了一个servlet初始化程序来在wildfly上运行它)我注释掉了这个位并从spring-boot-starter-tomcat依赖项中删除了它并开始工作。在这里添加,以防其他人有相同的问题
答案 2 :(得分:0)
使用这种依赖性解决了我的问题:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</dependency>