Spring Boot 2 Use custom configuration

After application.yml custom configuration, you can use Environment to read the configuration can also be used to make business @Value annotation code to read configuration.
If the property more, you can define the property map object.

Development Environment: IntelliJ IDEA 2019.2.2
the Spring the Boot Version: 2.1.8

Create a new name for the demo of Spring Boot project.

First, the use @Value comment

1, application.yml configured

jdbc:
  url: localhost:3306

2, add a class ValueProp.cs

@Component the annotation to registered class ValueProp Spring container, corresponding to the value @ Value application.yml configuration.

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ValueProp {

    @Value("${jdbc.url}")
    private String jdbcUrl;

    public String getJdbcUrl() {
        return jdbcUrl;
    }
}

3, the class code to modify the startup DemoApplication.cs

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication  {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Autowired
    private ValueProp valueProp;

    @RequestMapping("/")
    public String index(){
        return valueProp.getJdbcUrl();
    }
}

Program structure.

 

Visit: http: // localhost: 8080 /
page displays: localhost: 3306

Second, the definition property map object

If the above example the following jdbc application.yml inside a plurality of attributes, used directly @Value cause code redundancy.
You can create a class map to specify the configuration properties prefix jdbc.

1, application.yml configured

NOTE: roles The following is a collection of string is required - format.

jdbc:
  url: localhost:3306
  user: root
  password: 123456
  db:
    name: mysql
    version: 1.0
    roles:
      - manager
      - client

2, a new attribute mapping class JdbcProp.cs

Use annotations @ConfigurationProperties declaration of the class configured prefix "jdbc".

package com.example.demo;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.List;

@ConfigurationProperties(prefix = "jdbc")
public class JdbcProp {
    private String url;
    private String user;
    private String password;
    private Database db;

    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getUser() {
        return user;
    }
    public void setUser(String user) {
        this.user = user;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Database getDb() {
        return db;
    }
    public void setDb(Database db) {
        this.db = db;
    }

    public static class Database{
        private String name;
        private String version;
        private List<String> roles;

        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getVersion() {
            return version;
        }
        public void setVersion(String version) {
            this.version = version;
        }
        public List<String> getRoles() {
            return roles;
        }
        public void setRoles(List<String> roles) {
            this.roles = roles;
        }
    }
}

3, a new configuration class JdbcConfig.cs

The purpose is to let the Spring container aware of the property map object such a custom.

package com.example.demo;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(value = JdbcProp.class)
public class JdbcConfig {
}

4, to modify the startup class code DemoApplication.cs

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication  {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Autowired
    private JdbcProp jdbcProp;

    @RequestMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
    public JdbcProp index(){
        return jdbcProp;
    }
}

Program structure.

  

Visit: http: // localhost: 8080 /

Page displays:

{"url":"localhost:3306","user":"root","password":"123456","db":{"name":"mysql","version":"1.0","roles":["manager","client"]}}

 

Guess you like

Origin www.cnblogs.com/gdjlc/p/11588350.html