简体   繁体   English

Spring 启动 java 配置不起作用

[英]Spring Boot java configuration not working

I'm not sure what's going on here.我不确定这里发生了什么。 I've been building spring apps for years and years and feel like I've just missed something.多年来,我一直在构建 spring 应用程序,感觉就像我错过了一些东西。

I have a spring boot app created through the spring initializer.我有一个通过 spring 初始化程序创建的 spring 引导应用程序。 I created a configuration class like this.我像这样创建了一个配置 class 。

@Configuration
public class WfwwebConfiguration {


    @Bean
    public WebClient webClientBean() {
        return new WebClient(MpesaPaymentService.MPESA_HOST, MpesaPaymentService.MPESA_PORT);
    }
}

The goal, of course, here is to create my WebClient class myself.当然,这里的目标是自己创建我的 WebClient class。 The problem is that spring kept running the no-argument constructor on it, which was trying to go to localhost.问题是 spring 一直在上面运行无参数构造函数,它试图将 go 连接到 localhost。

So I deleted that constructor and now my spring boot app won't start because WebClient doesn't have a no-arg constructor.所以我删除了那个构造函数,现在我的 spring 启动应用程序将无法启动,因为 WebClient 没有无参数构造函数。

So what did I miss?那么我错过了什么? I have我有

@SpringBootApplication
public class WfwwebApplication {
    public static void main(String[] args) {
        SpringApplication.run(WfwwebApplication.class, args);
    }
}

Which is supposed to turn on auto configuration and all the research I've done says it will now pick up the configuration class automatically.应该打开自动配置,我所做的所有研究都表明它现在会自动选择配置 class 。

I double checked my @Configuration to make sure it wasn't loading it from another library.我仔细检查了我的@Configuration 以确保它没有从另一个库加载它。 ;) ;)

Ideas?想法?

Please consider the following working minimal example that will print out "Hello, StackOverflow" on start-up using a @Configuration producing an object of a class with no-arg constructor.请考虑以下工作最小示例,该示例将在启动时使用@Configuration打印出“Hello,StackOverflow”,生成 object 的 class 和无参数构造函数。

Application.java应用.java

package org.example;

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

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

ApplicationConfiguration.java ApplicationConfiguration.java

package org.example;

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

@Configuration
public class ApplicationConfiguration {

    @Bean
    public Greeter greeter() {
        return new Greeter("StackOverflow");
    }
}

AppStartupRunner.java AppStartupRunner.java

package org.example;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class AppStartupRunner implements ApplicationRunner {

    private final Greeter greeter;

    public AppStartupRunner(Greeter greeter) {
        this.greeter = greeter;
    }

    @Override
    public void run(ApplicationArguments args) {
        this.greeter.greet();
    }
}

Greeter.java迎宾员.java

package org.example;

public class Greeter {

    private final String name;

    public Greeter(String name) {
        this.name = name;
    }

    public void greet() {
        System.out.println("Hello, " + this.name);
    }
}

I was running inside the intelliJ idea environment when I found this issue.当我发现这个问题时,我正在 IntelliJ IDEA 环境中运行。 After trying many things, I tried turning debug on in the run configuration and there was nothing helpful in the messages generated.在尝试了很多事情之后,我尝试在运行配置中打开调试,但生成的消息没有任何帮助。

When I turned the setting back off and re-ran the app it started working perfectly on its own.当我关闭设置并重新运行应用程序时,它开始自行完美运行。

Please post a comment if this helps someone else.如果这对其他人有帮助,请发表评论。 I'm curious if this was it.我很好奇是不是这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM