Spring Boot some basic configuration

1. custom banner, Spring Boot project at startup there will be a default boot pattern:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.9.RELEASE)

We can modify this pattern as they want. New banner.txt file in src / main / resources directory, and then paste into your own pattern can be. ASCII pattern by site http://www.network-science.de/ascii/ a key generation, such as input to a pattern generation after copying mrbird banner.txt, start the project, eclipse console output is as follows:

 _   _   _   _   _   _  
 / \ / \ / \ / \ / \ / \ 
( m | r | b | i | r | d )
 \_/ \_/ \_/ \_/ \_/ \_/ 
...
2017-08-12 10:11:25.952  INFO 7160 --- [main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-08-12 10:11:26.057  INFO 7160 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-08-12 10:11:26.064  INFO 7160 --- [main] com.springboot.demo.DemoApplication : Started DemoApplication in 3.933 seconds (JVM running for 4.241)

banner may be closed, the main method:

public static void main(String[] args) {
    SpringApplication app = new SpringApplication(DemoApplication.class);
    app.setBannerMode(Mode.OFF);
    app.run(args);
}

2. The global configuration file

In the src / main / resources directory, Spring Boot offers a program called application.properties global configuration file, you can modify the default configuration of some configuration values.

The official document:  https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html

Spring Boot allows us to customize some properties in application.properties, such as:

test.h1 = haha

Define a BlogProperties Bean, by @Value("${属性名}")loading the configuration file property values to:

@Component
public class BlogProperties {
    
    @Value("${mrbird.blog.name}")
    private String name;
    
    @Value("${mrbird.blog.title}")
    private String title;
    
    // get,set略    
}

Write IndexController, injected into the Bean:

@RestController
public class IndexController {
    @Autowired
    private BlogProperties blogProperties;
    
    @RequestMapping("/")
    String index() {
        return blogProperties.getName()+"——"+blogProperties.getTitle();
    }
}

Start the project, visit HTTP: // localhost: 8080 , the page appears as follows:

In the case where a lot of attributes, and can also define a corresponding configuration file Bean:

 

@ConfigurationProperties(prefix="guanbin")
public class ConfigBean {
    private String name;
    private String title;
    // get,set略
}

By annotation @ConfigurationProperties(prefix="guanbin")specifies the property's common prefix, attribute name plus common prefix attribute name and configuration files one by one.

In addition to the need to import category in the Spring Boot add comments @EnableConfigurationProperties({ConfigBean.class})to enable the configuration:

@SpringBootApplication
@EnableConfigurationProperties({ConfigBean.class})
public class Application {
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

After IndexController can be injected in the Bean, and uses:

@RestController
public class IndexController {
    @Autowired
    private ConfigBean configBean;
    
    @RequestMapping("/")
    String index() {
        return configBean.getName()+"——"+configBean.getTitle();
    }
}

Property between references

In application.properties configuration file, various attributes can refer to each other as follows:

blog.name=mrbird's blog
blog.title=Spring Boot
blog.wholeTitle=${blog.name}--${blog.title}

Custom Profile

In addition to the configuration properties in application.properties, we can also customize a configuration file. Create a test.properties in src / main under / resources directory:

test.name=KangKang
test.age=25

The definition of one pair should be configured Bean file:

@Configuration
@ConfigurationProperties(prefix="test")
@PropertySource("classpath:test.properties")
@Component
public class TestConfigBean {
    private String name;
    private int age;
    // get,set略
}

Annotation @PropertySource("classpath:test.properties")specifies which profile to use. To use the configuration Bean, also need to use annotations in the class entry @EnableConfigurationProperties({TestConfigBean.class})to enable the configuration.

 

Attribute value setting command line

When you run Spring Boot jar file, you can use the command java -jar xxx.jar --server.port=8081to change the value of the port. This command is equivalent to the application.properties we manually modify (if not, then this attribute is added) server.port property is 8081.

If the configuration item is not want to modify the command line, the following can be provided in the main file entry method:

public static void main(String[] args) {
    SpringApplication app = new SpringApplication(Application.class);
    app.setAddCommandLineProperties(false);
    app.run(args);
}

Use the xml configuration

Although Spring Boot does not recommend that we continue to use the xml configuration, but if the situation had to use xml configuration appears, Spring Boot allows us entrance class via annotations @ImportResource({"classpath:some-application.xml"})to introduce xml configuration file.

Profile Configuration

Profile for using a different configuration file, the configuration file must be multi-environment for different environments application-{profile}.propertiesformat life, which {profile}is identified environment. For example, define two profiles:

application-dev.properties: Development Environment

server.port=8080

application-prod.properties: the production environment

server.port=8081

As to which specific configuration file is loaded, the need application.properties file spring.profiles.activeto set the property, whose value corresponds to {profile}value.

Such as: spring.profiles.active=devwill load application-dev.properties configuration file contents. You can use the command at runtime jar files java -jar xxx.jar --spring.profiles.active={profile}switch to a different environment configuration.

The original address and link: https://mrbird.cc/Spring-Boot%20basic%20config.html

 

Guess you like

Origin www.cnblogs.com/guanbin-529/p/11974108.html