spring-boot-dependency-injection

Implement constructor-first dependency injection patterns in Spring Boot applications.

2|Updated Jan 5, 2024
One-click install
npx skills add https://github.com/wilfriedago/dotfiles --skill spring-boot-dependency-injection-wilfriedago
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: spring-boot-dependency-injection
Source: https://github.com/wilfriedago/dotfiles/tree/main/agents/skills/spring-boot-dependency-injection
Command: npx skills add https://github.com/wilfriedago/dotfiles --skill spring-boot-dependency-injection-wilfriedago

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill addresses the complexities of managing dependencies in Spring Boot applications, ensuring code is testable, maintainable, and robust by promoting best practices in dependency injection.

Core Features & Use Cases

  • Constructor Injection: Prioritizes constructor injection for mandatory dependencies, ensuring immutability and testability.
  • Optional Dependencies: Provides clear patterns for handling optional collaborators using setters or ObjectProvider.
  • Bean Resolution: Guides on resolving bean ambiguities with @Qualifier, @Primary, and profiles.
  • Validation: Emphasizes unit testing for wiring validation before integration tests.
  • Use Case: Refactor a legacy Spring Boot service that uses field injection to adopt constructor injection, making it easier to unit test with mocks and improving overall code quality.

Quick Start

Apply constructor injection to the UserService class by ensuring all mandatory dependencies are passed through its constructor.

Frequently Asked Questions about spring-boot-dependency-injection

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I use constructor injection in Spring Boot to refactor field injection?

Use constructor injection in Spring Boot by passing all mandatory dependencies through a class constructor, ensuring immutability and making the class easier to unit test with mocks. This replaces field injection for better code quality.

What is the best way to handle optional dependencies in Spring Boot?

Handle optional dependencies in Spring Boot using setter injection or ObjectProvider, allowing flexible collaborator wiring without forcing mandatory bean instantiation during application startup.

How do I resolve bean ambiguities when multiple implementations exist in Spring Boot?

Resolve bean ambiguities in Spring Boot by applying @Qualifier to specify target beans, using @Primary to set default implementations, or isolating configurations with profile-specific bean registrations.

Why does Spring Boot throw a circular dependency error and how do I fix it?

Circular dependency errors in Spring Boot occur when beans reference each other during initialization. Fix them using event-driven resolution patterns to break the initialization cycle and enable deterministic wiring.

Does constructor injection make unit testing Spring Boot services easier?

Constructor injection makes unit testing Spring Boot services easier because dependencies are explicit parameters, allowing you to inject mocks directly without needing Spring context or reflection frameworks.

Can I use profile-specific configurations for conditional bean registration in Spring Boot?

You can use profile-specific configurations in Spring Boot to conditionally register beans, ensuring deterministic wiring by activating specific implementations only when matching application profiles are enabled.