美文网首页工作生活
Spring 的@Resource注解

Spring 的@Resource注解

作者: 安在成丶 | 来源:发表于2019-07-04 10:03 被阅读0次

@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>

相关文章

网友评论

    本文标题:Spring 的@Resource注解

    本文链接:https://www.haomeiwen.com/subject/khxlhctx.html