springboot系类代码:spring-boot-configuration-processor

在应用程序代码中插入日志请求需要相当多的计划和工作。观察显示,大约4%的代码用于日志记录。因此,即使是中等大小的应用程序也会在其代码中嵌入数千条日志语句。考虑到日志语句的数量,管理这些日志语句而不需要手动修改它们就变得非常必要。
log4net环境是完全可编程配置的。但是,使用配置文件配置log4net要灵活得多。目前,配置文件是用XML编写的。
让我们来体验一下如何在使用log4net的MyApp虚拟应用程序的帮助下实现这一点。

package com.programb.springboot.configuration;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

	public static void main(String[] args) throws InterruptedException {
		SpringApplication.run(Application.class, args);
		Thread.sleep(Long.MAX_VALUE);
	}
}

package com.programb.springboot.configuration.property;

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


@Configuration
@ConfigurationProperties(prefix = "my.config")
public class SimpleProperty {

    private String app;

    private String user;

    private int age;

    private String email;

    private String blog;

    private String github;

    public String getApp() {
        return app;
    }

    public void setApp(String app) {
        this.app = app;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getBlog() {
        return blog;
    }

    public void setBlog(String blog) {
        this.blog = blog;
    }

    public String getGithub() {
        return github;
    }

    public void setGithub(String github) {
        this.github = github;
    }
}

package com.programb.springboot.configuration.random;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RandomProperty {

    @Value("${my.secret}")
    private String randomValue;

    @Value("${my.number}")
    private int randomInt;

    @Value("${my.bignumber}")
    private long randomLong;

    @Value("${my.uuid}")
    private String randomUUID;

    @Value("${my.number.less.than.ten}")
    private int randomIntRange;

    @Value("${my.number.in.range}")
    private int randomIntMaxMinRange;

    public String getRandomValue() {
        return randomValue;
    }

    public int getRandomInt() {
        return randomInt;
    }

    public long getRandomLong() {
        return randomLong;
    }

    public String getRandomUUID() {
        return randomUUID;
    }

    public int getRandomIntRange() {
        return randomIntRange;
    }

    public int getRandomIntMaxMinRange() {
        return randomIntMaxMinRange;
    }

}

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-programb-examples</artifactId>
        <groupId>com.programb</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-boot-configuration</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

你可能感兴趣的:(springboot)