java - 设置 Spring Boot Servelet Initializer 时遇到问题
问题描述
我正在尝试使用 Spring Boot 设置一个简单的 Web CRUD 应用程序。我知道我应该是 spring boot servelet 初始化程序,但是在日志中,我怀疑它没有正常运行,因为它没有记录我写的内容。
最终目标是让我能够访问http://localhost:8080/LNU-Project/和 home.jsp 显示。
这是 github 上的链接。https://github.com/rjpruitt16/LNU-Project/tree/master/src/main
WebAppInitializer.java
package com.project.LNUProject;
import com.project.LNUProject.config.WebConfig; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration;
@Slf4j
@SpringBootApplication
public class WebAppInitializer extends SpringBootServletInitializer {
private static final String DISPATCHER_SERVLET_NAME = "dispatcher";
public static void main(String[] args) {
SpringApplication.run(WebAppInitializer.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebAppInitializer.class);
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
log.info("onStartUp");
// create the spring application context
AnnotationConfigWebApplicationContext context =
new AnnotationConfigWebApplicationContext();
context.register(WebConfig.class);
// create the dispatcher servlet
DispatcherServlet dispatcherServlet =
new DispatcherServlet(context);
// register and configure the dispatcher servlet
ServletRegistration.Dynamic registration =
servletContext.addServlet(DISPATCHER_SERVLET_NAME, dispatcherServlet);
registration.setLoadOnStartup(1);
registration.addMapping("/");
} }
WebConfig.java
package com.project.LNUProject.config;
import com.project.LNUProject.utils.ViewNames;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
@EnableWebMvc
@Configuration
@ComponentScan
@Slf4j
public class WebConfig implements WebMvcConfigurer {
// == constants ==
public static final String RESOLVER_PREFIX = "/WEB-INF/view/";
public static final String RESOLVER_SUFFIX =".jsp";
// == bean methods
@Bean
public ViewResolver viewResolver() {
UrlBasedViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix(RESOLVER_PREFIX);
viewResolver.setSuffix(RESOLVER_SUFFIX);
return viewResolver;
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
log.info("registry add properly");
registry.addViewController("/").setViewName(ViewNames.HOME);
}
}
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>com.project</groupId>
<artifactId>LNU-Project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>Database</module>
<module>WEB</module>
</modules>
<packaging>pom</packaging>
<name>LNU-Project</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
**<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.6.7</version>
<configuration>
<container>
<containerId>tomcat9x</containerId>
<type>embedded</type>
</container>
</configuration>
</plugin>
</plugins>**
</build>
</project>
解决方案
请按照以下步骤在外部 tomcat 上部署 Spring Boot 应用程序:
需要打包 WAR 应用程序而不是 JAR。为此,我们将 pom.xml 更改为以下内容:
<packaging>war</packaging>
添加Tomcat依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
最后,我们通过实现
SpringBootServletInitializer
接口来初始化Tomcat所需的Servlet上下文:@SpringBootApplication public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } }
推荐阅读
- html - 单击按钮时将数据包发送到另一个网站
- ansible - 结合 json 寄存器导致 ansible
- python - 删除具有 minidom 的节点
- excel - 使用 TFS 2018 和 Excel 将任务名称重命名为 Activity
- java - 接口中的方法没有被 mockito junit 覆盖
- windows - 如何通过 GetSubscriptionsForUser API 确定 Microsoft Store 订阅购买中的自动续订偏好更改?
- azure - Azure DevOps 中的看板
- python - 使用 xgboost 分类器进行多类分类?
- datagridview - 如何在datagridview的最后一行下获得灰色区域的高度?
- java - 测试 Oracle spring.datasource.validationQuery=SELECT 1 from dual 的 Spring Boot 验证查询