Spring boot Properties file read

Spring boot Properties file read


@Configuration // Equivalent to defining an XML file
@ConfigurationProperties(prefix = "Test",locations = "classpath:test.properties")
// prefix, what does the desired field start with
// Locations, if not, read the application.properties file by default, and read the corresponding properties file after setting
public class TestConfig {
	private String id; // It needs its set method to import the contents of the Properties file
	private String name;

	@Bean(name = "testBean") // Equivalent to configuring beans in XML, if there is no name, it is the name of the bean (the first letter is lowercase)
	public TestBean testBean() {

		TestBean client = new TestBean(id, name);
		return client;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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



@ConfigurationProperties can also be placed outside the method
@Configuration // Equivalent to defining an XML file
public class TestBean3Config {
	@Bean(name = "testBean3") // Equivalent to configuring beans in XML
	@ConfigurationProperties(prefix = "Test", locations = "classpath:test.properties") // What do the required fields start with
	public TestBean2 testBean2() {
		return new TestBean2();
	}
}



@Configuration // Equivalent to defining an XML file
//@ImportResource("classpath:dubbo-provider.xml")//Include the xml file
public class TestBean4Config {

	@Value("${Test.}") //This method can only use application.properties and cannot be placed elsewhere
	private String id;
	@Value("${Test.name}")
	private String name;

	@Bean(name = "testBean4") // Equivalent to configuring beans in XML
	
	public TestBean testBean4() {
		System.out.println("TestBean4Config id == " + id);
		System.out.println("TestBean4Config name == " + name);
		return new TestBean(id, name);
	}
}



Reference original text: http://www.cnblogs.com/softidea/p/5683522.html

Guess you like

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