current position:Home>Springboot Foundation (18): third party bean attribute binding

Springboot Foundation (18): third party bean attribute binding

2022-06-24 09:20:58There is no retreat when sailing against the current

Preface

Attribute binding

@EnableConfigurationProperties

@EnableConfigurationProperties Annotations can be used to @EnableConfigurationProperties The class corresponding to the annotation is loaded into Spring In the container

@Data
@ConfigurationProperties(prefix = "servers")
public class ServerConfig {
    
}
@SpringBootApplication
@EnableConfigurationProperties(ServerConfig.class)
public class SpringbootHotdeployApplication {
    

	public static void main(String[] args) {
    
// System.setProperty("spring.devtools.restart.enabled","false");
		ConfigurableApplicationContext context=SpringApplication.run(SpringbootHotdeployApplication.class, args);
		ServerConfig config=context.getBean(ServerConfig.class);
		System.out.println(config.getClass().getName());
	}

}

Execute code
 Insert picture description here

Be careful :@EnableConfigurationProperties and @Component Can't be used at the same time

Will prompt ServerConfig Can find two Bean
 Insert picture description here

@ConfigurationProperties

Release from use @ConfigurationProperties Note warning

 Insert picture description here
Introduce dependencies and refresh dependencies

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

copyright notice
author[There is no retreat when sailing against the current],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/175/202206240802284250.html

Random recommended