The structure directory for the entire example is as follows:
1. Customizing a resource file
Com.sxd.name = Shen 9th Wood com.sxd.secret = ${random.value}com.sxd.intvalue = ${random.int}com.sxd.uuid = ${random.uuid} Com.sxd.age= ${random.int (+)}com.sxd.resume = CV: ① name: ${com.sxd.name} ② Age: ${com.sxd.age}
2. Bind attributes from a resource file to a bean
PackageCom.sxd.beans;Importorg.springframework.boot.context.properties.ConfigurationProperties;ImportOrg.springframework.context.annotation.PropertySource;Importorg.springframework.stereotype.Component;/*** User entity * @Component declares that the user entity is a bean * @PropertySource the declaration corresponds to the test.properties file " Configurationproperties annotations should be canceled the attribute "* @ConfigurationProperties corresponding to the property name of the binding prefix COM.SXD*/@Component @propertysource (value= "Classpath:/test.properties") @ConfigurationProperties (prefix= "Com.sxd") Public classUser {PrivateString name; PrivateInteger age; //The resource file defines com.sxd.uuid instead of UU, where the UU field is only tested if the name does not correspond, will the value be assigned successfully PrivateString UU; PrivateString resume; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicInteger getage () {returnAge ; } Public voidsetage (Integer age) { This. Age =Age ; } PublicString Getuu () {returnUU; } Public voidsetuu (String uu) { This. UU =UU; } PublicString Getresume () {returnResume; } Public voidSetresume (String resume) { This. Resume =Resume; }}
View Code
3.spring Boot main Entrance
PackageCom.sxd.secondemo;ImportCom.sxd.beans.User;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.boot.context.properties.EnableConfigurationProperties;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController;/*** Spring Boot Main entry class * @RestController = @ResponseBody + @Controller * @SpringBootApplication the core annotations of spring boot * @Ena Bleconfigurationproperties activates the bean that binds the resource file, such as User.class here or more*/@RestController @springbootapplication@enableconfigurationproperties (User.class) Public classsecondemoapplication {/*** @Autowired Automatic injection, you need to declare an activated bean in @enableconfigurationproperties to automatically inject success*/@Autowired User User; /*** The request address is localhost:8080/to access this method *@return */@RequestMapping ("/") PublicString Hello () {/*** Idea in System.out.println () shortcut to Sout, then alt+enter to come out*/System.out.println (User.getresume ()); return"Print Resume:" +user.getresume () + "\ n" + "UU whether there are values:" +User.getuu (); } Public Static voidMain (string[] args) {Springapplication.run (secondemoapplication.class, args); }}
View Code
4. Operation Result:
"Spring Boot" 3.spring boot project, bind resource file to bean and use