Profiles และ Environment
Spring profiles (@Profile), application-{profile}.properties, การเปิดใช้งาน profile, ตัวแปรสภาพแวดล้อม
1annotation @Profile ทำอะไรได้บ้างใน Spring?
annotation @Profile ทำอะไรได้บ้างใน Spring?
คำตอบ
annotation @Profile ช่วยให้สามารถกำหนดเงื่อนไขการเปิดใช้งาน bean หรือการตั้งค่าตาม profile ที่เปิดใช้งานอยู่ เมื่อ profile หนึ่งเปิดใช้งานอยู่ (ผ่าน spring.profiles.active) เฉพาะ bean ที่ถูก annotation ด้วย profile นั้นเท่านั้นที่จะถูกโหลดเข้าสู่ Spring context สิ่งนี้ทำให้มีการตั้งค่าที่แตกต่างกันสำหรับ dev, test และ production โดยไม่ต้องเขียนโค้ดซ้ำ เช่น ใช้ฐานข้อมูล H2 ใน dev และ PostgreSQL ใน production
2พร็อพเพอร์ตี้ใดเปิดใช้งาน profile ของ Spring Boot เมื่อแอปพลิเคชันเริ่มทำงาน?
พร็อพเพอร์ตี้ใดเปิดใช้งาน profile ของ Spring Boot เมื่อแอปพลิเคชันเริ่มทำงาน?
คำตอบ
พร็อพเพอร์ตี้ spring.profiles.active เปิดใช้งาน profile หนึ่งหรือหลายตัวเมื่อเริ่มทำงาน สามารถกำหนดได้ใน application.yml, ผ่าน command line (--spring.profiles.active=prod) หรือผ่านตัวแปรสภาพแวดล้อม พร็อพเพอร์ตี้นี้เป็นวิธีมาตรฐานในการเลือกสภาพแวดล้อมการทำงาน (dev, test, prod)
3ตั้งชื่อไฟล์การตั้งค่าเฉพาะสำหรับ profile 'production' อย่างไร?
ตั้งชื่อไฟล์การตั้งค่าเฉพาะสำหรับ profile 'production' อย่างไร?
คำตอบ
ไฟล์การตั้งค่าเฉพาะ profile จะเป็นไปตามรูปแบบ application-{profile}.yml หรือ application-{profile}.properties สำหรับ profile 'production' ไฟล์จะเป็น application-production.yml ไฟล์เหล่านี้จะเขียนทับพร็อพเพอร์ตี้ของไฟล์ application.yml หลักเมื่อ profile ที่เกี่ยวข้องเปิดใช้งานอยู่
ความแตกต่างระหว่าง spring.profiles.active และ spring.profiles.include คืออะไร?
อินเทอร์เฟซ Environment ใน Spring คืออะไร?
+17 คำถามสัมภาษณ์
หัวข้อสัมภาษณ์ Spring Boot อื่นๆ
Spring Core - IoC & DI
Spring Boot Auto-Configuration
Spring Boot Starters
Application Properties & YAML
การทำ Logging ด้วย SLF4J และ Logback
Spring Boot DevTools
Spring MVC Basics
Spring REST Controllers
Request & Response Handling
การจัดการข้อยกเว้น
Bean Validation
พื้นฐาน Spring Data JPA
เอนทิตี JPA และความสัมพันธ์
การสืบค้น JPA
Spring Data Repositories
พื้นฐาน Spring Security
Spring Boot Actuator
การทดสอบหน่วยด้วย JUnit และ Mockito
การทดสอบ Spring Boot
RestTemplate และ WebClient
Async และ Scheduling
Caching ด้วย Spring
Spring WebFlux (Reactive)
ทรานแซกชัน Spring
การยืนยันตัวตนและการให้สิทธิ์ขั้นสูง
JWT และความปลอดภัยแบบ Stateless
OAuth2 และ Authorization Server
Spring Boot และ Docker
Microservices ด้วย Spring
Spring Cloud Config
การเพิ่มประสิทธิภาพการทำงาน
GraalVM Native Images
เชี่ยวชาญ Spring Boot สำหรับการสัมภาษณ์ครั้งถัดไป
เข้าถึงคำถามทั้งหมด flashcards แบบทดสอบเทคนิค แบบฝึกหัด code review และตัวจำลองสัมภาษณ์
เริ่มใช้ฟรี