报错:Not registered via @EnableConfigurationProperties, marked as Spring component, or scanned via @ConfigurationPropertiesScan 的解决方案
方法一:直接添加注解@Component,将其标注为Spring组件
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Data @Component @ConfigurationProperties(prefix = "my.server") public class MyServerProperties { private String name; private String ip = "127.0.0.1"; private int port = 9797; }
方法二:加注解@EnableConfigurationProperties ,将其标注为配置类
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; @Data @EnableConfigurationProperties(MyServerProperties.class) @ConfigurationProperties(prefix = "my.server") public class MyServerProperties { private String name; private String ip = "127.0.0.1"; private int port = 9797; }
方法三:在springboot启动类上添加 @ConfigurationPropertiesScan 注解,开启配置文件扫描
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.ConfigurationPropertiesScan; @ConfigurationPropertiesScan @SpringBootApplication public class MongodbApplication { public static void main(String[] args) { SpringApplication.run(MongodbApplication.class, args); } }
懵懵懂懂迷迷糊糊

浙公网安备 33010602011771号