Spring Boot Learning 2 properties configuration file read

Source: Internet
Author: User
Tags bind

At Spring boot learning 1 o'clock, you know that spring boot will read the configuration application.properties by default. So if we add a custom configuration item directly to the Application.properties, how to read it. Or do not want to put all the configuration in the application.properties, but instead of customizing a properties file, how to read it. Do you have to write your own code to load the read configuration file?

Note: The following code is written under the spring Boot 1.5.2 version.

Pom.xml

<parent>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId> spring-boot-starter-parent</artifactid>
	    <version>1.5.2.RELEASE</version>
	</parent >   
	<dependencies>
	    <dependency>
	        <groupid>org.springframework.boot</groupid >
	        <artifactId>spring-boot-starter-web</artifactId>
	    </dependency>
		</ Dependencies>


@ValueUse @value to inject configuration items written in Application.properties application.properties
Logging.config=classpath:logback.xml
logging.path=d:/logs


# #tomcat set###
server.port=80
# Session Timeout
server.session-timeout=60
########### hello=hello China

!!!

Class.schoolname=china School
Class.classname=second Grade three class
Class.students[0].name=tom
Class.students[1].name=jack

@value injected into a property in code
@Controller
@RequestMapping ("/") public
class Hellocontroller {public

	static Logger LOG = Loggerfactory.getlogger (hellocontroller.class);
	
	@Value ("${hello}")
	private String hello;
	
	@Value ("${class.schoolname}")
	private String schoolname;

@ConfigurationPropertiesIf you want to inject some kind of related configuration into a configuration class, such as the CLASS.XX configuration item in the application.properties above, you want to inject into the Classconfig configuration class
Package com.fei.springboot.config;

Import java.util.ArrayList;
Import java.util.List;

Import org.springframework.boot.context.properties.ConfigurationProperties;
Import org.springframework.stereotype.Component;

@Component ("Classconfig")
@ConfigurationProperties (prefix= "class") public
class Classconfig {

	private String Schoolname;
	
	Private String className;
	
	Private list<studentconfig> students = new arraylist<studentconfig> ();

	Public String Getschoolname () {
		return schoolname;
	}

	public void Setschoolname (String schoolname) {
		this.schoolname = schoolname;
	}

	public void Setclassname (String className) {
		this.classname = className;
	}

	public void Setstudents (list<studentconfig> students) {
		this.students = students;
	}

	Public String GetClassName () {
		return className;
	}

	Public list<studentconfig> getstudents () {
		return students;
	}
	
	
}
This will inject the application.properties into the configuration item that begins with class.
We see in the code that @configurationproperties does not specify the properties, and the default is Application.properties. Is there a place to indicate the path of the properties? Before version 1.5.1 can be @configurationproperties (prefix= "Class", location= "Classpath:/customer.properties"), But 1.5.2 version has no location, need to use @propertysource ("Classpath:/student.properties")
Package com.fei.springboot.config;

Import org.springframework.boot.context.properties.ConfigurationProperties;
Import Org.springframework.context.annotation.PropertySource;
Import org.springframework.stereotype.Component;

@Component ("Studentconfig")
@ConfigurationProperties
@PropertySource ("Classpath:/student.properties") Public
class Studentconfig {

	private String name;
	

	Public String GetName () {
		return name;
	}
	
	public void SetName (String name) {
		this.name = name;
	}
	
}

Under test
Package Com.fei.springboot.controller;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.beans.factory.annotation.Value;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.bind.annotation.ResponseBody;
Import Com.fei.springboot.config.ClassConfig;

Import Com.fei.springboot.config.StudentConfig; @Controller @RequestMapping ("/") public class Hellocontroller {public static Logger LOG = Loggerfactory.getlogger (Hello
	
	Controller.class);
	
	@Value ("${hello}") Private String Hello;
	
	@Value ("${class.schoolname}") Private String schoolname;
	
	@Autowired private Classconfig Classconfig;
	
	@Autowired private Studentconfig Studentconfig; @RequestMapping (value= "/hello") @ResponseBody public String Hello () {System.out.println ("======= using @value injection gets ...
		========="); System.out.println ("hello="+hello+ "schoolname=" + schoolname);
		SYSTEM.OUT.PRINTLN ("====== use @configurationproperties injection to get ... ============");
		System.out.println ("schoolname=" + classconfig.getschoolname ());
		System.out.println ("classname=" + classconfig.getclassname ());
		
		System.out.println ("student[0].name=" + classconfig.getstudents (). Get (0). GetName ());
		
		System.out.println ("studentconfig...name=" + studentconfig.getname ());
	return "Hello";
 }
}
Print results
2017-05-17 15:20:49.677  INFO 72996---[p-nio-80-exec-1] o.s.web.servlet.dispatcherservlet        : Frameworkservlet ' Dispatcherservlet ': initialization completed in MS
2017-05-17 15:20:49.677 [ Http-nio-80-exec-1] INFO  o.s.web.servlet.dispatcherservlet-frameworkservlet ' dispatcherservlet ': Initialization completed in MS
======= using @value injection to get ... ===========
hello=hello China!!!   Schoolname=china School
====== using @configurationproperties injection to get ... ============
Schoolname=china School
Classname=second Grade three class
Student[0].name=tom
Studentconfig...name=xiao Hong


SOURCE download


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.