Spring @ConfigurationProperties

1. Configuration file class
package chengf.spring.boot.config.test;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * Hello world!
 *
 */
@ConfigurationProperties
public class AppBootConfig {
	private String name;

	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	
}



2. Business code reference
package chengf.spring.boot.config.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties(AppBootConfig.class)
public class SampleApplication implements CommandLineRunner {

	@Autowired
	private AppBootConfig appBootConfig;
	
	public static void main(String[] args) throws Exception {
		SpringApplication.run(SampleApplication.class, args);
	}
	@Override
	public void run(String... args) throws Exception {
		System.out.println(appBootConfig.getName());
		
	}
	
}



configuration file
name=chengf
age=20


The execution result



proves that the data is correctly injected into the bean

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326706957&siteId=291194637