Read the core configuration file
The core configuration file refers to the resources root directory application.properties
or application.yml
configuration file, read the two configuration files in two ways, are relatively simple.
The contents of the core configuration file application.properties
are as follows:
Server.port=9090test.msg=hello World springboot!
- How to use
@Value
(common)
1 @RestController2 Public classWebcontroller {3 4@Value ("${test.msg}")5 PrivateString msg;6 7@RequestMapping (value = "Index", method =requestmethod.get)8 PublicString Index () {9 return"The 1:" +msg;Ten } One}
Note: the @Value
key name in the core configuration file is included in the ${}. adding to the controller class @RestController
means that all views in this class are displayed in JSON, similar to adding on a view method @ResponseBody
.
Access: Http://localhost:9090/index will getThe Way 1 : Hello World Springboot!
@RestController Public class Webcontroller { @Autowired Private environment env; = "Index2", method = requestmethod.get) public String index2 () { return "The 2:" + env.getproperty ("test.msg");} }
Note: This approach is dependent on injecting evnironment
to complete, create the member variable private Environment env
plus @Autowired
Note to complete the dependency injection and then use the env.getproperty ("Key Name")
Access: HTTP://LOCALHOST:9090/INDEX2 will getThe Way 2 : Hello World Springboot!
To read a custom configuration file
- Using
@ConfigurationProperties
annotations to inject and get configuration through getter, setter methods
In order not to destroy the original ecology of the core file, but also need to have the custom configuration information exists, in general will choose the custom configuration file to put these custom information, here in the resources/config
directory to create the configuration filemy-web.properties
resources/config/my-web.properties
The contents are as follows:
Web.jdbc.password=root
To create an entity class that manages the configuration:
@ConfigurationProperties (locations = "Classpath:config/my-web.properties", prefix = "web") @Component Public classmywebconfig{PrivateString name; Privatejdbc JDBC; classJDBC {PrivateString username; PrivateString password; //getter ... } PublicInteger Geport () {return This. Port; } PublicJdbc Getjdbc () {return This. jdbc; }}
Attention:
@ConfigurationProperties
There are two properties in the note:
locations
: Specify the location of the configuration file
prefix
: Specifies the prefix of the key name in the configuration file (I have all the key names in the configuration file to web.
begin with)
Use @Component
is to allow the class to be used elsewhere, using @Autowired
annotations to create instances.
- Through
@PropertySource
annotations, and then using a per- @Value
injection configuration
1 @Configuration2@PropertySource ("Classpath:test.properties")3 Public classElconfig {4 5@Value ("${book.name}")6 PrivateString bookname;7 8 //Propertysourcesplaceholderconfigurer This bean, this bean is mainly used to solve the ${used in @value ...} Placeholder. If you don't use ${...} placeholder, you can not use this bean. 9 @BeanTen Public Staticpropertysourcesplaceholderconfigurer propertyconfigure () { One return Newpropertysourcesplaceholderconfigurer (); A } - - Public voidOutputsource () { the System.out.println (bookname); - } -}
Springboot-read the core configuration file and custom configuration file