Solution of introducing @ Resource annotation null pointer into Spring framework
- 2021-12-12 08:31:44
- OfStack
Introducing @ Resource annotation null pointer
Recently, I just started to learn Spring framework. When using annotation @ Resource, I report null pointer no matter how I apply it. The main reason is that JDK version does not support it.
Solutions
1. Introduce dependencies in the maven configuration file pom. xml file
org.apache.tomcat
tomcat-annotations-api
9.0.13
2. Replace the local JDK version, preferably above 1.8. Note that the 1.9 JDK version is not supported, but BUG is available
The @ Resource annotation cannot be introduced for the spring project
Problem
When introducing @ Resource annotation in spring project, there is a red underline error, and automatic code completion cannot appear when entering annotation.
Solution
The dependency on the javax. annotation package is missing from the Spring project. Add dependencies to the maven configuration file pom. xml.
<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency>
1. Import the namespace in the configuration file of spring
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
2. Introducing an annotation parser
context:annotation-config></context:annotation-config>
3. Introduce bean in the configuration file of spring
4. Add the attribute of 1 class
@Resource(name="student_annotation")
private Student student;
From the annotation itself
@Target({TYPE, FIELD, METHOD})
@Retention(RUNTIME)
public @interface Resource {
String name() default "";
}
This annotation can be used on properties or methods, but 1 is generally used on properties
This annotation has one attribute name, and the default value is ""
5. Analyze the whole process
1. When the spring container is started, the spring container loads the configuration file 2. In the spring configuration file, whenever a configuration of bean is encountered, an object is created for that bean 3. Find all bean within the scope of the spring container and see which bean attributes or methods have @ Resource added to them 4. After finding the @ Resource annotation, determine whether the attribute of the annotation name is "" (name is not written)If the name attribute is not written, the value of the attribute name will be matched with the value of ID in spring, and if the match is successful, the value will be assigned
If the match is unsuccessful, it will be matched according to the type, and if the match is unsuccessful, an error will be reported
If there is an name attribute, it will be matched according to the value of name attribute and ID in bean of spring. If the match is successful, the value will be assigned, and if it is unsuccessful, an error will be reported