-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first draft for Injecting data into static fields is not supported by…
… Spring
- Loading branch information
1 parent
a25cec9
commit 723e32b
Showing
2 changed files
with
33 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,53 @@ | ||
FIXME: add a description | ||
== Why is this an issue? | ||
|
||
// If you want to factorize the description uncomment the following line and create the file. | ||
//include::../description.adoc[] | ||
Spring dependency injection framework does not support injecting data into static fields. When @Value, @Inject, or @Autowired are applied to static fields, they are ignored. | ||
|
||
== Why is this an issue? | ||
What is the potential impact? | ||
|
||
FIXME: remove the unused optional headers (that are commented out) | ||
* Null Values: Uninitialized static fields annotated with @Value, @Inject, or @Autowired will not be initialized by Spring, potentially causing NullPointerException at runtime. | ||
* Confusing Code: The presence of injection annotations on static fields can mislead developers into believing that the fields will be populated by Spring. | ||
//=== What is the potential impact? | ||
This rule raises an issue when a static will is annotated with @Value, @Inject, or @Autowired. | ||
|
||
== How to fix it | ||
//== How to fix it in FRAMEWORK NAME | ||
|
||
Either use an instance field instead of a static field or remove the @Value, @Inject, or @Autowired annotation and initialize the field. | ||
|
||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,java,diff-id=1,diff-type=noncompliant] | ||
---- | ||
FIXME | ||
@Component | ||
public class MyComponent { | ||
@Autowired | ||
private static SomeDependency dependency; // Noncompliant, @Autowired will be ignored and no value will be injected | ||
// ... | ||
} | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,java,diff-id=1,diff-type=compliant] | ||
---- | ||
FIXME | ||
---- | ||
@Component | ||
public class MyComponent { | ||
//=== How does this work? | ||
private final SomeDependency dependency; | ||
//=== Pitfalls | ||
|
||
//=== Going the extra mile | ||
@Autowired | ||
public ExampleClass(SomeDependency dependency) { | ||
this.dependency = dependency; | ||
} | ||
// ... | ||
} | ||
---- | ||
|
||
//== Resources | ||
== Resources | ||
//=== Documentation | ||
//=== Articles & blog posts | ||
//=== Conference presentations | ||
//=== Standards | ||
//=== External coding guidelines | ||
//=== Benchmarks | ||
=== Articles & blog posts | ||
* Java Guides - https://www.baeldung.com/spring-inject-static-field |