@Resource注解,可以相当于@Required,@AutoWired注解组合。
可以在字段或者setter方法中应用@Resource注解,遵循 by-name 自动连接语义。
通过 “name” 属性到Spring容器中匹配相关Bean,来为相关属性注入依赖。
Student.java文件如下:
@Resource(name= " student1")
private Student student ;
@Resource(name= "student1")
public void setSpellChecker( Studentstudent)
{ this.Student= student; }
ApplicationContext.xml
<bean id="student1" class="com.tutorialspoint.Student">
<property name="name" value="Zara" />
<property name="age" value="11"/>
</bean>
<bean id="student2" class="com.tutorialspoint.Student">
<property name="name" value="Nuha" />
<property name="age" value="2"/>
</bean>
网友评论