为了账号安全,请及时绑定邮箱和手机立即绑定

设置 Spring Boot Servelet Initializer 时遇到问题

设置 Spring Boot Servelet Initializer 时遇到问题

慕哥6287543 2021-12-01 14:58:03
我正在尝试使用 Spring Boot 设置一个简单的 Web CRUD 应用程序。我知道我应该是 spring boot servelet 初始值设定项,但是在日志中,我怀疑它没有正常运行,因为它没有记录我写的内容。最终目标是让我能够访问http://localhost:8080/LNU-Project/和 home.jsp 显示。这是 github 上的链接。https://github.com/rjpruitt16/LNU-Project/tree/master/src/mainWebAppInitializer.javapackage 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 =        } }
查看完整描述

2 回答

?
慕神8447489

TA贡献1780条经验 获得超1个赞

请按照以下步骤在外部 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);

   }


}


查看完整回答
反对 回复 2021-12-01
?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html


该文档明确指出您必须在 pom.xml中将包装标签从pom更改为war


<?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>war</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>


查看完整回答
反对 回复 2021-12-01
  • 2 回答
  • 0 关注
  • 167 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信