حاوية Spring
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- ApplicationContext
- نطاقات الـ Bean
- فحص المكونات (Component scanning)
- الأنماط الوسمية (Stereotypes)
تعلّم أولًا
2 ساعة تدريب (تقدير)
مخرجات التعلم
- Explain the
ApplicationContextand how beans are discovered by component scanning. - Choose between
@Component,@Service,@Repository,@Controller, and@Beanmethods in@Configurationclasses. - Choose singleton or prototype scope and explain the thread-safety implication of singletons.
- Diagnose a bean that was not found because of package layout.
الدروس
المختبر والتمارين
Lab
JNR-010-LAB1 · Third-party beans
Outcomes: 1–4 · Time: ~2 h
- Create a
@Configurationclass with@Beanmethods for aClockand a currency client (a simple third-party-style class). - Inject
Clockinto the promotion and order services; replace everynow()call. - Write a test using
Clock.fixed(...)for a time-dependent rule. - 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)
| ID | Outcome | Task |
|---|---|---|
| JNR-010-EX01 | 1 | Print the number of beans and the names starting with product from the ApplicationContext. |
| JNR-010-EX02 | 2 | For each class choose a stereotype or @Bean: ProductService, ProductRepository, ProductController, a third-party HttpClient. |
| JNR-010-EX03 | 2 | Write a @Configuration class exposing a Clock bean and inject it into a service. |
| JNR-010-EX04 | 3 | Add a private int counter to a singleton, hit the endpoint twice, and explain the result; then remove the state. |
| JNR-010-EX05 | 3 | Show that two injections of a prototype bean differ and two of a singleton do not (compare references). |
| JNR-010-EX06 | 4 | Given 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()andArrays.stream(ctx.getBeanDefinitionNames()).filter(n -> n.startsWith("product")). - EX02
@Service,@Repository,@RestController,@Beanin a@Configuration. - EX03
@Bean Clock clock(){ return Clock.systemUTC(); }and a constructor parameterClock clock. - EX04 The counter grows across requests because one instance serves all; keep services stateless.
- EX05
a == bis 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 tocom.shop, or setscanBasePackages(the first is preferred).
الاختبار
Outcomes: O1 ApplicationContext and scanning · O2 stereotypes and @Bean · O3 scope and thread safety · O4 missing-bean diagnosis.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
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.