- Home
- Skills
- Giuseppe Trisciuoglio
- Developer Kit
- Spring Boot Dependency Injection
spring-boot-dependency-injection_skill
- Python
99
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill giuseppe-trisciuoglio/developer-kit --skill spring-boot-dependency-injection- SKILL.md8.7 KB
Overview
This skill provides a practical dependency injection guide for Spring Boot projects, promoting constructor-first design, explicit optional collaborator handling, and deterministic bean configuration. It focuses on keeping services framework-agnostic, testable, and easy to reason about during development and refactors.
How this skill works
The skill inspects dependency wiring patterns and prescribes concrete implementation steps: identify mandatory vs optional collaborators, prefer constructor injection, supply guarded setters or ObjectProvider for optional beans, and declare deterministic @Bean factories. It also covers resolving bean ambiguity with @Primary, @Qualifier, profiles, and conditional annotations, and validates wiring with focused unit and slice tests.
When to use it
- Creating new @Service, @Component, or @Repository classes to ensure explicit wiring
- Replacing legacy field injection and modernizing modules for better testability
- Configuring optional or pluggable collaborators behind feature flags or multi-tenant adapters
- Auditing bean definitions before adding integration tests or upgrading Spring Boot
- Resolving bean ambiguity when multiple implementations exist
Best practices
- Prefer constructor injection for mandatory dependencies and mark injected fields final
- Handle optional collaborators via guarded setters (@Autowired(required = false)), ObjectProvider, or no-op defaults
- Use @Primary, @Qualifier, profiles, and @ConditionalOn... annotations to make bean selection intentional
- Write unit tests that instantiate classes directly with mocks before running Spring slice or integration tests
- Avoid field injection, service locators, and excessive @Lazy; extract services if constructors grow beyond a few collaborators
Example use cases
- Implement UserService with @RequiredArgsConstructor and inject repositories and notification services via constructor
- Add a CacheService optional collaborator using a guarded setter and a CacheService.noOp() fallback
- Register NotificationService conditionally using @ConditionalOnProperty and provide a @ConditionalOnMissingBean noop implementation
- Migrate a legacy module by replacing @Autowired fields with constructor parameters and adding focused unit tests to validate wiring
- Resolve ambiguous implementations using @Qualifier and document qualifier names in shared constants
FAQ
Instantiate the service directly with mock dependencies (Mockito or similar) to validate behavior without starting the Spring context; add slice tests only after constructor contracts are verified.
What's the recommended pattern for optional beans?
Prefer ObjectProvider<T> or @Autowired(required = false) setter injection with a documented no-op/default implementation so behavior is deterministic when the bean is absent.