Spring Boot. 4--Custom Spring Boot Configuration "2"

Source: Internet
Author: User

In addition to the automatic configuration changes used in the first article using overwrite, you can also change the runtime configuration of your app with the Application.properties file provided in Spring Boot. This configuration is very granular in the way it is configured.

application.properties

In the Spring Boot application, the Application.properties file is in the resource/directory, and the application.properties is blank in the initial state. This file allows you to compare fine-grained controls on some of the configuration in spring Boot, such as database links, specific parameter configurations, and so on. By default, after the app starts listening on a port of 8080, if you want to set the strong port to 7000, then just add server.port=7000 to the Application.properties file, then launch the app and use it in the browser LOCALHOST:7000 will be able to access the page.

     application.properties files have some default configurable parameters, such as spring.datasource.* You can use these configurations to configure the DataSource that are used in your app. These existing configurations can be found in the documentation. Using the autonomic configuration and using the original configuration, the method is basically consistent. For example, if you want to use the data that is configured in Application.properties in a class. You need to use the annotation @ConfigurationProperties (prefix= "test")

@Controller @requestmapping ("/"= "Test")  Public class Readinglistcontroller {     private  String property;         System.out.println ("value" + property );           Public void SetProperty (String property) {        the property= property;     }} 

in Application.properties, simply specify the value of the property:

Test.property=just a test

of course, if you want to initialize a class object in your code using the parameters in Application.properties, it is also good to use it directly as an instance in other places. The way of use is basically the same as above. First, define the required data in the Application.properties file:

dealer.item=balldealer.fashion=truedealer.money=100dealer.home=shop

Define a class dealer, using the currently defined configuration:

@ConfigurationProperties (prefix = "Dealer")
@Configuration Public classDealer {PrivateString Item; Private BooleanFashion; Private intMoney ; PrivateString Home; PublicString GetItem () {returnitem; } Public voidSetItem (String item) { This. Item =item; } Public Booleanisfashion () {returnFashion; } Public voidSetfashion (Booleanfashion) { This. Fashion =Fashion; } Public intGetmoney () {returnMoney ; } Public voidSetmoney (intMoney ) { This. Money =Money ; } PublicString gethome () {returnhome; } Public voidSethome (String home) { This. Home =home; }}

Dealer is used in the controller:

@Controller Public classHellocontroller {@AutowiredPrivatereaderrepository readerrepository; PrivateDealer Dealer; @Autowired PublicHellocontroller (Dealer Dealer) { This. Dealer =dealer; } @RequestMapping (Value= "/prop")     PublicString Testprop () {System.out.println ("Here" +dealer.gethome ()); return"Index"; }}

This is basically the way you use the routines.

Spring Boot. 4--Custom Spring Boot Configuration "2"

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.