How to use @Resource annotation_study notes

This article is organized from the blog garden

  1. Import the namespace in the spring configuration file
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
  1. Introduce annotation parser
 <context:annotation-config></context:annotation-config>
  1. Introduce the bean in the spring configuration file
<beans>
	<bean id="">
		...
	</bean>
</beans>
  1. Add @Resourceannotations to the attributes of a class
public class xxx {
    
    
  @Resource(name="student_annotation")  //1:该注解可以放在属性或者方法上;2:有一个name属性默认值为"";
  private Student student;
  ...
}
  1. Analyze the entire process:

1. When the spring container is started, the spring container loads the configuration file
2. In the spring configuration file, as long as the bean configuration is encountered, an object will be created for the bean
3. Find all beans within the scope of the spring container , See which bean attributes or methods have @Resource
4. After finding the @Resource annotation, judge 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 given Match with the value of ID in spring. If the match is successful, assign a value.
If the match is unsuccessful, it will match according to the type. If the match is unsuccessful, an error will be reported.

Guess you like

Origin blog.csdn.net/qq_40084325/article/details/111387393