Skip to content
Back to skills

1017 Spring Official Dependency Injection 93e4e851

ASecurity

Source: https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-collaborators.html (retrieved via `u2m -v` on current date).

  • 9 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added October 11, 2026
testingjavaspringtesting

Security analysis

A100/100

Scanned October 11, 2026

npx -y skills add tools-only/X-Skills --skill 1017-spring-official-dependency-injection_93e4e851 --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 1017 Spring Official Dependency Injection 93e4e851?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 1017 Spring Official Dependency Injection 93e4e851
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/tools-only-1017-spring-official-dependency-injection-93e4e851/badge)](https://www.skillsdirectory.com/skills/tools-only-1017-spring-official-dependency-injection-93e4e851)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

SKILL.md
# Spring Framework Official Guidance: Dependency Injection (Clean Excerpt)

Source: https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-collaborators.html (retrieved via `u2m -v` on current date).

## Key Highlights
- Emphasize constructor-based dependency injection to make collaborators explicit and enable immutable design.
- Use setter injection only for optional dependencies or when a dependency can change after initialization.
- Field injection is supported but discouraged because it hides dependencies and complicates testing.
- The IoC container resolves constructor arguments by type, name, and order; prefer unique types or qualify arguments with `@Qualifier` or XML attributes when ambiguity exists.
- Static factory methods behave like constructors for dependency injection and can receive collaborators through arguments.

## Constructor-Based DI
```java
public class SimpleMovieLister {
    private final MovieFinder movieFinder;

    public SimpleMovieLister(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }
}
```
- The container selects the matching constructor and provides dependencies by type.
- When argument types are ambiguous, specify indexes (`@ConstructorProperties`, XML `index` attribute) or qualifiers.

## Setter-Based DI
```java
public class SimpleMovieLister {
    private MovieFinder movieFinder;

    @Autowired
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }
}
```
- Invoke only when a collaborator is optional or changeable.
- Use `@Autowired(required = false)` or `ObjectProvider<T>` to guard optional collaborators.

## Reference Snippets
```xml
<bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg ref="anotherExampleBean"/>
    <constructor-arg ref="yetAnotherBean"/>
    <constructor-arg value="1"/>
</bean>

<bean id="exampleBean" class="examples.ExampleBean">
    <property name="beanOne" ref="anotherExampleBean"/>
    <property name="beanTwo" ref="yetAnotherBean"/>
</bean>
```
- Spring treats constructor-arg entries as positional parameters unless `index` or `type` is provided.
- Setter injection uses `<property>` elements mapped by name.

## Additional Notes
- Combine configuration classes with `@Import` to wire dependencies declared in different modules.
- Lazy initialization (`@Lazy`) delays bean creation but defers error detection; prefer eager initialization unless startup time is critical.
- Profiles (`@Profile`) activate different wiring scenarios per environment (for example, `@Profile("test")`).
- Testing support allows constructor injection in production code while wiring mocks manually (no container required) or relying on the TestContext framework for integration tests.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.