Exploring SpringBoot configuration techniques, this article covers essential setup and customiaztion steps.
I. Fundamental Setup
- Customizing the Banner
(1). Create a file named banner.txt in src/main/resources.
(2). Visit http://patorjk.com/software/taag to generate a custom text style, then copy it into the banner.txt file. Upon restarting, the new banner will be displayed.
(3) Disabling the Banner
Modify the main method to set the banner mode to OFF.
package com.example.demo;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(DemoApplication.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
}
View Code2. SpringBoot Configuration Files
SpringBoot uses application.properties as the global configuration file. It supports both properties and YAML formats.
server.port=8081
server.servlet.context-path=/cywtest
This changes the default port and context path. The startup logs reflect these modifications.
- Using XML Configuration
Although SpringBoot discuorages XML usage, it can still be integrated. Create a HelloService class in com.example.cywtest, which is not scanned by default due to package mismatch.
***************************
APPLICATION FAILED TO START
***************************
Description:
Field helloService in com.example.demo.HelloController required a bean of type 'com.example.cywtest.HelloService' that could not be found.
Action:
Consider defining a bean of type 'com.example.cywtest.HelloService' in your configuration.
View Code(1). Create application-bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloService" class="com.example.cywtest.HelloService">
</bean>
</beans>
View Code(2). Import the XML file in the main package to allow SpringBoot to scan it.
package com.example.demo;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource(locations = {"application-bean.xml"})
public class ConfigClass {
}
View CodeWith this setup, the service is correctly loaded.
HelloService: This class is located outside the main package, so it requires explicit scanning.
package com.example.cywtest;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
public HelloService() {
System.out.println("Service configured via XML");
}
}
View CodeConfigClass: This class imports the XML configuration.
package com.example.demo;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource(locations = {"application-bean.xml"})
public class ConfigClass {
}
View CodeHelloController: This class injects HelloService.
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.cywtest.HelloService;
@RestController
@RequestMapping("/sbs")
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping("/hello")
public String Hello() {
return "Hello World";
}
}
View CodeAfter integrating the configuration, SpringBoot scans the service correct, and no errors occur.
II. External Configuration
- Standard Property Configuration
Define properties in application.properties and use @Value to inject them.
Test.Name=cuiyw
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.cywtest.HelloService;
@RestController
@RequestMapping("/sbs")
public class HelloController {
@Autowired
HelloService helloService;
@Value("${Test.Name}")
private String Name;
@RequestMapping("/hello")
public String Hello() {
return "Hello World," + Name;
}
}
View Code2. Type-Safe Properties Configuration
SpringBoot provides a way to associate properties with a Bean using @ConfigurationProperties for type-safe access.
(1). Create a test.properties file in src/main/resources.
person.Name=cyw
person.Age=18
(2). Create a PersonSetting class in com.example.demo to map the properties.
package com.example.demo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = "classpath:/test.properties")
@ConfigurationProperties(prefix="person")
public class PersonSetting {
private String name;
private Long age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getAge() {
return age;
}
public void setAge(Long age) {
this.age = age;
}
}
View Code(3). Inject PersonSetting into HelloController.
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.cywtest.HelloService;
@RestController
@RequestMapping("/sbs")
public class HelloController {
@Autowired
HelloService helloService;
@Value("${Test.Name}")
private String Name;
@Autowired
PersonSetting personSetting;
@RequestMapping("/hello")
public String Hello() {
return "Hello World," + Name + "Person Name:" + personSetting.getName();
}
}
View CodeUpon starting the application, accessing http://localhost:8081/cywtest/sbs/hello will show the property values from the configuration file.
III. Profile Configuration
In development, different environments may require different configurations. Use application-{profile}.properties files to manage environment-specific settings.
(1). Create application-dev.properties and application-prod.properties files with distinct port and context path values.
server.port=8082
server.servlet.context-path=/devtest
server.port=8083
server.servlet.context-path=/prodtest
(2). Set the active profile in application.properties.
spring.profiles.active=dev
IV. Summary
This article covered several basic configuration methods. There are many more aspects, such as logging, which can be explored in future articles.