您的位置:首页 > 编程语言 > Java开发

Spring 几种注入方法

2013-11-12 12:05 393 查看
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>

接口注入:

。。。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: