الملفات التعريفية للبيئات
بدّل الإعدادات لكل بيئة باستخدام الملفات التعريفية
الملفات التعريفية للبيئات درس مجاني في Spring Boot 4 Microservices & REST APIs على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Spring Boot 4 Microservices & REST APIs، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Profiles Exist
Real applications run in several environments — local, dev, staging, production — each needing different datasources, log levels, and feature toggles. Profiles let one codebase carry many configurations and activate the right set at runtime.
A profile is just a named label. Beans and property files can be tagged so they apply only when that profile is active.
Profile-Specific Property Files
Boot automatically loads application-{profile}.yml on top of the base application.yml. The profile file overrides matching keys while inheriting everything else.
# application.yml (base, always loaded)
spring:
jpa:
show-sql: false
# application-dev.yml (loaded when dev active)
spring:
jpa:
show-sql: true
datasource:
url: jdbc:h2:mem:devdbActivating a Profile
Set spring.profiles.active to choose which profiles run. You can do this in a file, as an environment variable, or on the command line.
# Command line
java -jar app.jar --spring.profiles.active=prod
# Environment variable
export SPRING_PROFILES_ACTIVE=prod
# Multiple profiles
java -jar app.jar --spring.profiles.active=prod,metricsConditional Beans with @Profile
@Profile on a bean or configuration class makes it register only when that profile is active. This is ideal for swapping implementations between environments.
@Configuration
public class PaymentConfig {
@Bean
@Profile("prod")
public PaymentGateway liveGateway() {
return new StripeGateway();
}
@Bean
@Profile("!prod")
public PaymentGateway fakeGateway() {
return new InMemoryGateway();
}
}Profile Expressions
The @Profile value supports simple logic: ! for NOT, & for AND, and | for OR. Group expressions with parentheses.
@Profile("!prod") // anything except prod
@Profile("prod & metrics") // both active
@Profile("dev | test") // either active
@Profile("prod & (eu | us)") // groupedThe Default Profile
When no profile is active, Spring enables the implicit default profile. You can target it explicitly with @Profile("default") for fallback beans used during plain local runs.
@Bean
@Profile("default")
public DataSeeder localSeeder() {
return new SampleDataSeeder();
}Profile Groups
A profile group bundles several profiles under one name, so activating the group activates them all. Great for composing cross-cutting concerns.
spring:
profiles:
group:
production:
- prod
- metrics
- swagger-off
# Now: --spring.profiles.active=production turns on all threeIncluding Profiles
spring.profiles.active sets the primary profiles, while spring.profiles.include adds extra ones unconditionally, regardless of which primary profile is selected.
spring:
profiles:
include:
- common-logging
- auditInspecting Active Profiles at Runtime
You can read active profiles from the Environment bean. This is useful for guard logic or for logging which configuration is live.
@Component
public class StartupLogger {
private final Environment env;
public StartupLogger(Environment env) { this.env = env; }
@PostConstruct
void log() {
System.out.println("Active: "
+ String.join(",", env.getActiveProfiles()));
}
}Profiles in Tests
In integration tests, annotate the class with @ActiveProfiles to force a specific configuration, such as an in-memory database profile.
@SpringBootTest
@ActiveProfiles("test")
class OrderServiceTest {
// loads application-test.yml + @Profile("test") beans
}Common Pitfalls
Watch for these mistakes:
- Forgetting to set
spring.profiles.activein production, so dev defaults leak in - Putting secrets in
application-dev.ymland committing it - Overusing
@Profilewhere a simple property toggle would do
Quick Check
Test your grasp of profile expressions.
Recap
Profiles tailor one codebase to many environments.
application-{profile}.ymloverrides base config- Activate via
spring.profiles.active(file, env var, or CLI) @Profileconditionally registers beans, with!,&,|expressions- Profile groups bundle related profiles
- Use
@ActiveProfilesin tests
الأسئلة الشائعة
هل درس «الملفات التعريفية للبيئات» مجاني؟
نعم — نص درس «الملفات التعريفية للبيئات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Spring Boot 4 Microservices & REST APIs، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.
ماذا ستتعلم في «الملفات التعريفية للبيئات»؟
بدّل الإعدادات لكل بيئة باستخدام الملفات التعريفية تتمرن على Spring Boot 4 Microservices & REST APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Spring Boot 4 Microservices & REST APIs؟
لا تُشترط خبرة سابقة. Spring Boot 4 Microservices & REST APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «الملفات التعريفية للبيئات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Spring Boot 4 Microservices & REST APIs هذا؟
نعم. كل درس في Spring Boot 4 Microservices & REST APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- application.properties وYAML
- الملفات التعريفية للبيئات
- إعداد آمن نوعيًا باستخدام @ConfigurationProperties
- الإعدادات الخارجية وإعادة الكتابة