Spring Boot 4 Complete Guide · บทเรียน

การแยกคุณสมบัติแอปพลิเคชันออกจากโค้ด

กำหนดค่าแอปพลิเคชันโดยใช้ไฟล์ `application.properties` และ `application.yml` พร้อมจัดการโปรไฟล์

บทเรียน 3 จาก 412 ขั้นตอน

การแยกคุณสมบัติแอปพลิเคชันออกจากโค้ด เป็นบทเรียน Spring Boot 4 Complete Guide ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Complete Guide และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Complete Guide มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Keep Config Flexible

Imagine building an app for different environments like development, testing, and production. Each might need unique database credentials, API keys, or server ports.

Hardcoding these values directly into your code is a bad practice. It makes your app inflexible and difficult to manage.

Externalizing configuration means storing these changeable values outside your main application code, allowing you to modify them without rebuilding your app.

application.properties

Spring Boot makes external configuration easy. The most common way is using the application.properties file, located in your src/main/resources folder.

It uses a simple key-value pair format, where each property is on a new line:

  • server.port=8080
  • spring.datasource.url=jdbc:h2:mem:testdb
  • my.custom.greeting=Hello CoddyKit!

These values can then be injected into your Spring components.

Injecting with @Value

To use an externalized property in your Java code, Spring provides the @Value annotation.

You can apply @Value to fields, constructor parameters, or method parameters. It reads the value associated with a specific key from your properties files.

The syntax is @Value("${property.key}"). If the property is not found, you can provide a default value like @Value("${property.key:Default Value}").

@Value in Action

Let's see how @Value works. First, we'll create an application.properties file.

src/main/resources/application.properties:

app.message=Welcome to CoddyKit!

Now, run this Spring Boot application. It will inject the property and print it on startup.

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

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

@Component
class MessagePrinter implements CommandLineRunner {

  @Value("${app.message}")
  private String message;

  @Override
  public void run(String... args) throws Exception {
    System.out.println("Application Message: " + message);
  }
}

Structured YAML Files

Another popular format for external configuration is YAML (YAML Ain't Markup Language), typically in an application.yml file.

YAML offers a more human-readable, hierarchical structure compared to the flat key-value pairs of .properties files. It uses indentation to define nested properties.

  • Readability: Easier to grasp complex configurations.
  • Hierarchy: Naturally groups related properties.
  • Less Repetition: Avoids repeating common prefixes.

YAML for Clarity

Here's how the same message property from before would look in application.yml:

src/main/resources/application.yml:

app: message: Welcome to CoddyKit! (YAML)

The Java code remains exactly the same as Spring Boot automatically supports both formats. Run this to see it in action.

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

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

@Component
class MessagePrinter implements CommandLineRunner {

  @Value("${app.message}")
  private String message;

  @Override
  public void run(String... args) throws Exception {
    System.out.println("Application Message: " + message);
  }
}

Who Wins? Precedence

What if you have both application.properties and application.yml in your project? Which one takes precedence?

Spring Boot has a specific order for loading configuration sources. Generally, properties defined in application.properties take precedence over those in application.yml when both exist and define the same key.

This allows you to override default values with more specific ones when needed, without changing the original files.

Adapting with Profiles

Spring Profiles provide a way to segregate parts of your application configuration and make it available only in certain environments.

For example, you might have different database settings for your development, test, and production environments. Instead of manually changing configuration files, you can define profiles.

When a specific profile is active, only the beans and configuration associated with that profile are loaded.

Tailoring for Environments

To define profile-specific properties, you create additional configuration files following a specific naming convention:

  • application-{profile}.properties
  • application-{profile}.yml

For example:

  • application-dev.properties for development settings.
  • application-prod.yml for production settings.

These files contain properties that apply only when their corresponding profile is active.

Switching Profiles

You can activate a Spring profile in several ways:

  • JVM System Property: Pass -Dspring.profiles.active=dev when running your app.
  • Environment Variable: Set SPRING_PROFILES_ACTIVE=prod.
  • Inside application.properties/.yml: Set spring.profiles.active=test.

It's common to use JVM arguments or environment variables for production deployments, keeping your main application.properties clean.

Quick Check: Config

You have an application.properties file with app.environment=development. You also have an application-prod.properties file with app.environment=production.

How would you activate the prod profile to ensure app.environment resolves to production?

Config Mastery Recap

Great job! You've learned how to externalize application properties in Spring Boot, making your applications more flexible and easier to manage across different environments.

  • We explored application.properties for simple key-value pairs.
  • We saw application.yml for structured, hierarchical configuration.
  • You learned to inject properties into your code using the @Value annotation.
  • Finally, we covered Spring Profiles to tailor configurations for specific environments and how to activate them.

This knowledge is crucial for building robust and adaptable Spring Boot applications!

เริ่มต้นได้ฟรี

เรียนรู้ Java ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
21
บทเรียน
84

คำถามที่พบบ่อย

บทเรียน “การแยกคุณสมบัติแอปพลิเคชันออกจากโค้ด” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแยกคุณสมบัติแอปพลิเคชันออกจากโค้ด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Complete Guide ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Complete Guide มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การแยกคุณสมบัติแอปพลิเคชันออกจากโค้ด”

กำหนดค่าแอปพลิเคชันโดยใช้ไฟล์ `application.properties` และ `application.yml` พร้อมจัดการโปรไฟล์ คุณปฏิบัติ Spring Boot 4 Complete Guide ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Complete Guide หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Complete Guide บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การแยกคุณสมบัติแอปพลิเคชันออกจากโค้ด” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Complete Guide นี้ได้ไหม

ได้ บทเรียน Spring Boot 4 Complete Guide ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ทำความเข้าใจคอนเทนเนอร์ IoC ของ Spring
  2. การฉีดการพึ่งพาในการใช้งานจริง
  3. การแยกคุณสมบัติแอปพลิเคชันออกจากโค้ด
  4. ขอบเขตบีนและการจัดการวงจรชีวิต
← กลับไปที่ Spring Boot 4 Complete Guide