Tag Archives: annotations

Solution of idea using @ Autowired annotation to report errors

1. If the compiler does not report an error, but the idea generates an error, it can be solved as follows:
file-> Settings – & gt; Editor-> Inspections click on the search bar to enter Spring Core
Spring Core -> Code -> The Autowring for Bean Class changes the Severity level from the previous error to warning, so that the idea will not report an error

2. If you can’t compile it, check to see if the package is not imported, or if it is injected as static type
In the Spring Framework, we cannot @Autowired static variable, create Spring beans, for example, without that:

@Autowired
private static YourClass yourClass;

Can have a try, yourClass in such a state can not be dependency injection, throws an exception during execution. Java lang. NullPointerException, why?Static variables/class variables are not properties of an object, but properties of a class. Spring is based on dependency injection at the object level.
The use of static variables/class variables extends the use of static methods. Static methods are not recommended in Spring. The main purpose of Dependency Injection is to allow the container to generate instances of an object and then use them throughout the life cycle, while also making testing easier.
Once you use static methods, you no longer need to generate instances of the class, which makes testing more difficult, and you can’t generate multiple instances with different dependencies for a given class using injection methods. This static field is an implicit shared static field. It is a global state. Spring also does not recommend this.