Spring Boot Tutorial: Project Creation, Configuration, and Customization
This article provides a step‑by‑step guide to creating a Spring Boot project, explains core features such as embedded servlet containers and starters, demonstrates how to configure the main class, customize banners, manage properties, set up type‑safe beans, logging, and profile‑specific settings, all with runnable code examples.
The article introduces Spring Boot as a solution to the heavy configuration required by Spring and Spring MVC, highlighting its core capabilities: independent runnable JARs, embedded servlet containers, starter dependencies, automatic configuration, production‑ready monitoring, and elimination of XML.
1. Independent Spring project execution
Spring Boot applications can be launched directly with java -jar xx.jar , simplifying deployment.
2. Embedded servlet container
It embeds Tomcat, allowing execution without a WAR file.
3. Starter POMs to simplify Maven configuration
Starters bundle common dependencies, reducing the need to list each one manually.
4. Automatic configuration, production‑grade monitoring, and no XML
The article then walks through creating a Spring Boot project using IntelliJ IDEA, showing screenshots of the Spring Initializr wizard, project metadata entry, dependency selection (Web), and final project naming.
After project generation, the generated entry class Test19SpringBoot2Application is shown, annotated with @SpringBootApplication . Adding @RestController and a simple request mapping creates a hello endpoint:
@RestController
@SpringBootApplication
public class Test19SpringBoot2Application {
public static void main(String[] args) {
SpringApplication.run(Test19SpringBoot2Application.class, args);
}
@RequestMapping(value = "/", produces = "text/plain;charset=UTF-8")
String index() {
return "Hello Spring Boot!";
}
}The article explains how to run the application from IntelliJ and access the endpoint via a browser.
Entry Class and @SpringBootApplication Annotation
The annotation combines @SpringBootConfiguration , @EnableAutoConfiguration , and @ComponentScan . If not using @SpringBootApplication , the three annotations can be applied individually.
Disabling Specific Auto‑Configuration
Specific auto‑configurations can be excluded, e.g., @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) .
Customizing the Banner
Spring Boot prints a banner on startup. To change it, create src/main/resources/banner.txt with ASCII art generated from an online tool. To disable the banner, modify the main method:
public static void main(String[] args) {
SpringApplicationBuilder builder = new SpringApplicationBuilder(Test19SpringBoot2Application.class);
// Turn banner off
builder.bannerMode(Banner.Mode.OFF).run(args);
}Configuration Files
Spring Boot uses application.properties or application.yml under src/main/resources . Examples include changing the server port and context path:
server.context-path=/helloboot
server.port=8081Properties can be injected with @Value :
@Value("${book.author}")
private String bookAuthor;
@Value("${book.name}")
private String bookName;
@Value("${book.pinyin}")
private String bookPinYin;The controller then returns these values in the response.
Type‑Safe Configuration
Instead of injecting many individual properties, a bean can be bound to a properties file using @ConfigurationProperties . The article shows a BookBean class bound to book.properties and demonstrates autowiring it in a controller to expose the data via /book .
Logging Configuration
Spring Boot defaults to Logback. Logging output location and level can be set in application.properties :
logging.file=/home/sang/workspace/log.log
logging.level.org.springframework.web=debugProfile Configuration
Environment‑specific settings are placed in application‑dev.properties and application‑prod.properties . The active profile is chosen with spring.profiles.active in the main application.properties . Switching the profile changes the server port accordingly.
Finally, a download link to the complete example project is provided.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.