Spring 几种注入方法

来源:互联网 发布:小米6相机数据怎么清除 编辑:程序博客网 时间:2024/04/29 18:57

1、set注入方式

2、构造方式注入

3、接口注入

 

set注入:在当前需要注入类中,提供注入属性和对应属性set方法,在applicationContext.xml文件当前需要注入类中配置<property>即可:

<bean id="helloService" class="com.wq.service.impl.HelloServiceImpl"></bean>

 <bean name="/hello" class="com.wq.web.action.HelloAction" autowire="constructor">
  <property name="helloService" ref="helloService"></property>     //红色为当前类中属性,黑色粗体为上面实例化对象ID
 </bean>

 

构造方法注入:在当前需要注入的类中,提供注入属性和当前类构造方法,构造方法中参数为注入属性。在applicaionContext.xml文件的当前类中。加入属性autowire="constructor"即可:

<bean name="/hello" class="com.wq.web.action.HelloAction" autowire="constructor">  //黑色粗体表明构造方式为构造方法注入。
 </bean>

 

接口注入:

。。。。。。

 

 

 

 

原创粉丝点击