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>
接口注入:
。。。。。。
本文详细介绍了Spring框架中的三种依赖注入方式:set注入、构造方法注入和接口注入,通过实例展示了如何在应用上下文中配置这些注入方式。
2145

被折叠的 条评论
为什么被折叠?



