"Spring Boot" 3.spring boot project, bind resource file to bean and use

Source: Internet
Author: User

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

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.