انتقل إلى المحتوى
أمين أبوزيد
JNR-010

حاوية Spring

  • 6 درس
  • 2 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • ApplicationContext
  • نطاقات الـ Bean
  • فحص المكونات (Component scanning)
  • الأنماط الوسمية (Stereotypes)

تعلّم أولًا

2 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Explain the ApplicationContext and how beans are discovered by component scanning.
  2. Choose between @Component, @Service, @Repository, @Controller, and @Bean methods in @Configuration classes.
  3. Choose singleton or prototype scope and explain the thread-safety implication of singletons.
  4. Diagnose a bean that was not found because of package layout.

الدروس

  1. 01الـ ApplicationContext
  2. 02فحص المكونات والأنماط الوسمية
  3. 03@Configuration و@Bean
  4. 04نطاقات الـ Bean
  5. 05الـ Singleton عديم الحالة وأمان الخيوط (Thread safety)
  6. 06عرض عملي: حقن Clock لاختبار منطق يعتمد على الوقت

المختبر والتمارين

Lab

JNR-010-LAB1 · Third-party beans

Outcomes: 1–4 · Time: ~2 h

  1. Create a @Configuration class with @Bean methods for a Clock and a currency client (a simple third-party-style class).
  2. Inject Clock into the promotion and order services; replace every now() call.
  3. Write a test using Clock.fixed(...) for a time-dependent rule.
  4. Move the main class to a sub-package, observe the missing-bean failure, then fix the layout.

Acceptance: no static now() calls in services; time-dependent tests are deterministic; the layout failure is explained.

Exercises (assessment: "why is this bean missing?" debugging exercise)

IDOutcomeTask
JNR-010-EX011Print the number of beans and the names starting with product from the ApplicationContext.
JNR-010-EX022For each class choose a stereotype or @Bean: ProductService, ProductRepository, ProductController, a third-party HttpClient.
JNR-010-EX032Write a @Configuration class exposing a Clock bean and inject it into a service.
JNR-010-EX043Add a private int counter to a singleton, hit the endpoint twice, and explain the result; then remove the state.
JNR-010-EX053Show that two injections of a prototype bean differ and two of a singleton do not (compare references).
JNR-010-EX064Given a project whose main class is in com.shop.app and a service in com.shop.service, explain the startup failure and give two fixes.
أظهر الحلول
  • EX01 ctx.getBeanDefinitionCount() and Arrays.stream(ctx.getBeanDefinitionNames()).filter(n -> n.startsWith("product")).
  • EX02 @Service, @Repository, @RestController, @Bean in a @Configuration.
  • EX03 @Bean Clock clock(){ return Clock.systemUTC(); } and a constructor parameter Clock clock.
  • EX04 The counter grows across requests because one instance serves all; keep services stateless.
  • EX05 a == b is true for singletons and false for prototypes.
  • EX06 The service package is outside com.shop.app, so it is not scanned: NoSuchBeanDefinitionException. Fix: move the main class to com.shop, or set scanBasePackages (the first is preferred).

الاختبار

Outcomes: O1 ApplicationContext and scanning · O2 stereotypes and @Bean · O3 scope and thread safety · O4 missing-bean diagnosis.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1The ApplicationContext is:O1
  2. 2Component scanning finds beans in:O1
  3. 3Best annotation for business logic:O2
  4. 4To register a third-party class you cannot edit:O2
  5. 5@Repository additionally:O2
  6. 6The default scope is:O3
  7. 7Why must singleton services be stateless?O3
  8. 8A prototype bean is requested:O3
  9. 9A service in com.shop.service is missing and the main class is in com.shop.app. Likely cause:O4
  10. 10The simplest layout fix:O4

الجزء العملي

Six exercises JNR-010-EX01 to EX06 plus the lab. The assessment is the "why is this bean missing?" exercise (EX06). Pass: quiz at least 8/10, EX06 diagnosed with a correct fix, lab acceptance met.

أخطاء شائعة

Mutable state in singleton services; main class in the wrong package.

التقييم

"why is this bean missing?" debugging exercise.

الصلة بالمسار الوظيفي

Concurrency awareness (MID-004) starts with stateless singletons.

اكتب للبحث في المستويات والمراحل والوحدات والدروس.