Spring boot application main方法执行两次->端口已在使用中

来源:爱站网时间:2021-09-16编辑:网友分享
这是我第一次从头开始配置Spring Boot应用程序,并且有2天的时间我一直在困扰着这个问题。当我尝试启动应用程序时,它看起来像是...

问题描述


这是我第一次从头开始配置Spring Boot应用程序,并且有2天的时间我一直在困扰着这个问题。当我尝试启动该应用程序时,它看起来好像试图启动两次,并且每次我收到一个错误消息,即该端口已在使用中,但是该过程并未停止,并且如果我尝试使用邮递员调用终结点计算机,作品。

这是控制台输出:

Starting Gradle Daemon...
Connected to the target VM, address: '127.0.0.1:61857', transport: 'socket'
Gradle Daemon started in 1 s 473 ms
> Task :processResources UP-TO-DATE
> Task :compileJava UP-TO-DATE
> Task :classes UP-TO-DATE
Connected to the VM started by ':VcrawlPortalBackendApplication.main()' (localhost:61879). Open the debugger session tab

> Task :VcrawlPortalBackendApplication.main()

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.4.RELEASE)

2020-02-25 15:48:19.817  INFO 471912 --- [  restartedMain] c.v.v.VcrawlPortalBackendApplication     : Starting VcrawlPortalBackendApplication on VOQCLUJWEBTS with PID 471912 
2020-02-25 15:48:19.822 DEBUG 471912 --- [  restartedMain] c.v.v.VcrawlPortalBackendApplication     : Running with Spring Boot v2.2.4.RELEASE, Spring v5.2.3.RELEASE
2020-02-25 15:48:19.823  INFO 471912 --- [  restartedMain] c.v.v.VcrawlPortalBackendApplication     : The following profiles are active: dev
2020-02-25 15:48:22.237 DEBUG 471912 --- [  restartedMain] c.v.v.config.DatabaseConfiguration       : Configuring Datasource
.........
2020-02-25 15:48:25.065  INFO 471912 --- [  restartedMain] c.v.v.VcrawlPortalBackendApplication     : Started VcrawlPortalBackendApplication in 6.273 seconds (JVM running for 6.994)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.4.RELEASE)

2020-02-25 15:48:25.178  INFO 471912 --- [  restartedMain] c.v.v.VcrawlPortalBackendApplication     : Starting VcrawlPortalBackendApplication on VOQCLUJWEBTS with PID 471912 
2020-02-25 15:48:25.179 DEBUG 471912 --- [  restartedMain] c.v.v.VcrawlPortalBackendApplication     : Running with Spring Boot v2.2.4.RELEASE, Spring v5.2.3.RELEASE
2020-02-25 15:48:25.179  INFO 471912 --- [  restartedMain] c.v.v.VcrawlPortalBackendApplication     : The following profiles are active: dev
2020-02-25 15:48:25.765 DEBUG 471912 --- [  restartedMain] c.v.v.config.DatabaseConfiguration       : Configuring Datasource
2020-02-25 15:48:25.904  INFO 471912 --- [  restartedMain] c.v.v.VcrawlPortalBackendApplication     : Running with Spring profile(s) : [dev]
2020-02-25 15:48:26.166 ERROR 471912 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Web server failed to start. Port 8187 was already in use.

Action:

Identify and stop the process that's listening on port 8187 or configure this application to listen on another port.

此日志都在同一运行中。

这是我的build.gradle文件:

plugins {
id 'org.springframework.boot' version '2.2.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'war'
}

bootWar {
    mainClassName = 'com.voquz.vcrawlportalbackend.VcrawlPortalBackendApplication'
    excludeDevtools = true
}

springBoot {
    mainClassName = 'com.voquz.vcrawlportalbackend.VcrawlPortalBackendApplication'
}

bootRun {
    sourceResources sourceSets.main

    if (System.getProperty('DEBUG', 'false') == 'true') {
        jvmArgs '-Xdebug',
                '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9009'
    }
}

if (project.hasProperty('prod')) {
    apply from: 'profile_prod.gradle'
} else if (project.hasProperty('uat')) {
    apply from: 'profile_uat.gradle'
} else {
    apply from: 'profile_dev.gradle'
}

group = 'com.voquz.vcrawl-portal-backend'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation ('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
    }
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.liquibase:liquibase-core'
    implementation 'com.fasterxml.jackson.datatype:jackson-datatype-hibernate4'
    implementation 'org.hibernate:hibernate-core'
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation 'org.springframework.security:spring-security-test'
    compile group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '8.2.0.jre8'
    compile 'com.github.sabomichal:liquibase-mssql:1.5'
    compile group: 'com.hynnet', name: 'sqljdbc-chs', version: '4.0.2206.100'
}

compileJava.dependsOn(processResources)

clean {
    delete "target"
}

task stage(dependsOn: 'bootRepackage') {
}

这里是开发配置文件的build.gradle文件:

ext {
    logbackLoglevel = "DEBUG"
}

dependencies {
    compile group: 'org.springframework.boot', name: 'spring-boot-devtools'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat', version: '2.2.4.RELEASE'
}

bootRun {
    args = ["--spring.profiles.active=dev"]
}

task setProdProperties(dependsOn: bootRun) {
    doFirst {
        System.setProperty('spring.profiles.active', 'dev')
    }
}

processResources {
    filesMatching('**/logback-spring.xml') {
        filter {
            it.replace('@logback.loglevel@', logbackLoglevel)
        }
    }
}

和主要班级:

@SpringBootApplication
@EnableConfigurationProperties({LiquibaseProperties.class})
public class VcrawlPortalBackendApplication {

    private static final Logger log = LoggerFactory.getLogger(VcrawlPortalBackendApplication.class);

    @Autowired
    private Environment environment;

    public static void main(String[] args) throws UnknownHostException {
        SpringApplication app = new SpringApplication(VcrawlPortalBackendApplication.class);
        SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(args);
        addDefaultProfile(app, source);
        SpringApplication.run(VcrawlPortalBackendApplication.class, args);
        Environment env = app.run(args).getEnvironment();
        log.info("Access URLs:\n----------------------------------------------------------\n\t" +
                        "Local: \t\thttp://127.0.0.1:{}\n\t" +
                        "External: \thttp://{}:{}\n----------------------------------------------------------",
                env.getProperty("server.port"),
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("server.port"));

    }

    /**
     * If no profile has been configured, set by default the "dev" profile.
     */
    private static void addDefaultProfile(SpringApplication app, SimpleCommandLinePropertySource source) {
        if (!source.containsProperty("spring.profiles.active") &&
                !System.getenv().containsKey("SPRING_PROFILES_ACTIVE")) {

            app.setAdditionalProfiles(Constants.SPRING_PROFILE_DEVELOPMENT);
        }
    }

    @PostConstruct
    public void initApplication() {
        if (environment.getActiveProfiles().length == 0) {
            log.warn("No Spring profile configured, running with default configuration");
        } else {
            log.info("Running with Spring profile(s) : {}", Arrays.toString(environment.getActiveProfiles()));
            Collection activeProfiles = Arrays.asList(environment.getActiveProfiles());
            if (activeProfiles.contains(Constants.SPRING_PROFILE_DEVELOPMENT) && activeProfiles.contains(Constants.SPRING_PROFILE_PRODUCTION)) {
                log.error("You have misconfigured your application! " +
                        "It should not run with both the 'dev' and 'prod' profiles at the same time.");
            }
            if (activeProfiles.contains(Constants.SPRING_PROFILE_DEVELOPMENT) && activeProfiles.contains(Constants.SPRING_PROFILE_UAT)) {
                log.error("You have misconfigured your application! " +
                        "It should not run with both the 'dev' and 'uat' profiles at the same time.");
            }
            if (activeProfiles.contains(Constants.SPRING_PROFILE_PRODUCTION) && activeProfiles.contains(Constants.SPRING_PROFILE_UAT)) {
                log.error("You have misconfigured your application! " +
                        "It should not run with both the 'prod' and 'uat' profiles at the same time.");
            }
        }
    }

}

我的想法是我的构建文件有问题,但找不到解决方案。有没有人遇到过同样的问题?有解决方案的主意吗?预先感谢。

思路一:


您两次调用SpringApplication::run静态方法。

首先使用SpringApplication.run(VcrawlPortalBackendApplication.class, args);及其来自初始化的SpringApplication实例Environment env = app.run(args).getEnvironment();的它们

思路二:


您的应用程序已在端口8187上开始执行。由于您已设置run方法的重复调用,因此它再次尝试在端口8187上开始执行。解决方案->删除运行函数的重复调用。

上一篇:将IllegalArgumentException抛出到IDE中但未打印出来

下一篇:如何在Spring 5 MVC中将FilePart转换为byte []

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载