【问题标题】:Spring-boot application not getting auto-deployed on startupSpring-boot 应用程序在启动时未自动部署
【发布时间】:2019-12-22 21:53:03
【问题描述】:

我按照在线教程在 Eclipse IDE 中创建了一个 Spring Boot 基本应用程序。当我尝试将应用程序作为 java 应用程序运行时,该应用程序没有部署在嵌入式 tomcat 中。我用谷歌搜索了很多,并尝试了一些找到的解决方案。但是,没有一个对我有用。我在这里粘贴代码。请让我知道任何可能的问题。 代码:-

pom.xml:-

<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.test</groupId>
<artifactId>spring-boot-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring Boot Example</name>
<packaging>jar</packaging>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.7.RELEASE</version>
</parent>



<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>

</dependencies>

SpringBootExample.java:-

package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.test.config.AppConfiguration;

@SpringBootApplication
public class SpringBootExample {

    public static void main(String[] args) {
        System.out.println("Started main");
        SpringApplication.run(AppConfiguration.class, args);
        System.out.println("Ending main");
    }

}

AppConfiguration.java:-

package com.test.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.test.beans.SampleBean;

@Configuration
public class AppConfiguration {

    @Bean
    public SampleBean getSampleBean() {
        return new SampleBean();
    }
}

SampleBean.java:-

package com.test.beans;

public class SampleBean {

    public SampleBean() {
        System.out.println("In constructor of SampleBean");
    }

}

请注意,正在创建 SampleBean 类型的 bean。但是,此后应用程序将关闭。以下是输出日志:-

【问题讨论】:

  • 我的代码不断从代码标签中弹出。请建议在代码标签中正确嵌入的原因和方法。
  • 尝试写一个控制器。此应用程序没有理由继续存在...
  • SpringApplication.run(AppConfiguration.class, args); 更改为SpringApplication.run(SpringBootExample.class, args); --- 我能找到的所有示例都使用包含main 并在run() 参数中使用@SpringBootApplication 注释的类。您在哪里看到使用另一个类的示例。也许这是一个你应该停止使用的网站,因为它看起来有缺陷。
  • @Andreas 我尝试使用相同的类,但结果仍然相同。另外,很多网站建议我应该传递具有 spring bean 配置的类的名称而不是 self,所以我尝试了。
  • @Turo 我创建了一个控制器,创建了一个 GET 方法,仍然得到相同的结果。

标签: java spring maven spring-boot spring-mvc


【解决方案1】:

我将注释 @EnableAutoConfiguration 添加到 AppConfiguration.java 并开始工作。应用程序无法确定要创建哪种类型的 ServletWebServerFactory。添加上述注释后,它为已存在于类路径中的嵌入式 tomcat 创建它。

【讨论】:

    猜你喜欢
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    • 2021-10-13
    • 2016-09-10
    • 2020-01-16
    • 2022-10-20
    相关资源
    最近更新 更多