Spring引导java配置不工作
我不知道这里发生了什么。我多年来一直在开发spring应用程序,感觉自己错过了一些东西 我有一个通过spring初始化器创建的spring启动应用程序。我创建了一个这样的配置类Spring引导java配置不工作,java,spring,spring-boot,Java,Spring,Spring Boot,我不知道这里发生了什么。我多年来一直在开发spring应用程序,感觉自己错过了一些东西 我有一个通过spring初始化器创建的spring启动应用程序。我创建了一个这样的配置类 @Configuration public class WfwwebConfiguration { @Bean public WebClient webClientBean() { return new WebClient(MpesaPaymentService.MPESA_HOST,
@Configuration
public class WfwwebConfiguration {
@Bean
public WebClient webClientBean() {
return new WebClient(MpesaPaymentService.MPESA_HOST, MpesaPaymentService.MPESA_PORT);
}
}
当然,这里的目标是自己创建WebClient类。问题是spring一直在运行无参数构造函数,该构造函数试图转到localhost
所以我删除了这个构造函数,现在我的spring boot应用程序无法启动,因为WebClient没有无参数构造函数
我错过了什么?我有
@SpringBootApplication
public class WfwwebApplication {
public static void main(String[] args) {
SpringApplication.run(WfwwebApplication.class, args);
}
}
它应该打开自动配置,我所做的所有研究都表明它现在会自动选择配置类
我仔细检查了@Configuration,以确保它不是从另一个库加载的。;)
思想?< /P> < P>请考虑下面的工作极小的例子,它将在启动时打印出“hello,StAdvOutlook”,使用<代码> @配置生成没有ARG构造函数的类的对象。
Application.java ApplicationConfiguration.java AppStartupRunner.java Greeter.java当我发现这个问题时,我正在intelliJ idea环境中运行。在尝试了许多事情之后,我尝试在运行配置中打开调试,但生成的消息中没有任何帮助 当我关闭设置并重新运行应用程序时,它开始自己完美工作
如果这对其他人有帮助,请发表评论。我很好奇是否是这样。您可能需要添加使用
WebClient bean的代码
如果WebClient是Spring提供的接口,那么您应该使用return WebClient.create(“)创建bean;//自然地替换主机和端口…哇!谢谢你的辛勤工作。我很谦卑。不幸的是,这就是我正在做的,它不起作用。
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);
}
}
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");
}
}
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();
}
}
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);
}
}