SpringBoot- SpringBoot Configurations

5.SpringBoot –Configurations

How to change Spring Boot Banner Text

The banner that is printed on startup can be changed by adding a banner.txt file to src\main\resources folder or your classpath, or by setting banner.location to the location of such a file.

You can also add a banner.gif, banner.jpg or banner.png image file to your classpath, or set a banner.image.location property. Images will be converted into an ASCII art representation and printed above any text banner.

  1. Go to any ANCII Text generator website & generate your logo. for ex: http://patorjk.com/

  2. Create banner.txt under Proj_Home\src\main\resources, paste the logo text

  3. Refresh the project & run Spring Boot Application.the banner will change as below

██████╗███╗   ███╗██╗      ██████╗ ██████╗ ██████╗ ███████╗███████╗
██╔════╝████╗ ████║██║     ██╔════╝██╔═══██╗██╔══██╗██╔════╝██╔════╝
███████╗██╔████╔██║██║     ██║     ██║   ██║██║  ██║█████╗  ███████╗
╚════██║██║╚██╔╝██║██║     ██║     ██║   ██║██║  ██║██╔══╝  ╚════██║
███████║██║ ╚═╝ ██║███████╗╚██████╗╚██████╔╝██████╔╝███████╗███████║
╚══════╝╚═╝     ╚═╝╚══════╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝╚══════╝
2017-02-01 14:07:22.957  INFO 72716 --- [           main] c.s.SpringBootHelloWorldApplication

application.properties

Spring Boot provides a very neat way to load properties for an application. we can define properties in application.properties (PROJ_HOME\src\main\resources\application.properties) file the following way

db.name=smlcodesdb
db.username=smlcodes
db.password=wEB20R1XPJtg9

In traditional Spring application would have loaded up the properties in the following way

public class SmlcodesPropTest {
    @Value("${db.name}") //dbname is KEY here
    private String dbname;

    @Value("${db. username }")
    private String server_port;
}

In Spring boot it takes application. properties file to define a bean that can hold all the related properties in following way

@ConfigurationProperties(prefix = "db")
@Component
public class DBConfig {

	public String dbname;
	public String username;
	public String password;
	
	public String getDbname() {
 return dbname;
	}
	public void setDbname(String dbname) {
 this.dbname = dbname;
	}
	public String getUsername() {
 return username;
	}
	public void setUsername(String username) {
 this.username = username;
	}
	public String getPassword() {
 return password;
	}
	public void setPassword(String password) {
 this.password = password;
	}
	
}

@ConfigurationProperties is used to bind and validate some external Properties. If we want to validate before going to use, we have to place @Validated & place type of validation on the filed

@ConfigurationProperties(prefix="foo")
@Validated
public class FooProperties {

    @NotNull
    private InetAddress remoteAddress;
    // ... getters and setters
}