spring基于xml文件注入依赖对象


转载于:https://www.cnblogs.com/xzf007/archive/2012/07/28/2873829.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架是一种轻量级的Java开发框架,主要使用依赖注入和面向切面编程等技术来完成应用程序的开发和管理。在Spring框架中,注入Bean是非常重要的一个概念。在XML配置文件中,可以使用以下几种方式来注入Bean: 1. Constructor注入 使用Constructor注入是指在Bean实例化时,通过构造函数(Constructor)注入需要的依赖。在配置文件中使用 <constructor-arg> 标签来配置参数,例如: <bean id="foo" class="com.example.Foo"> <constructor-arg index="0" value="fooValue"/> <constructor-arg index="1" value="barValue"/> </bean> 2. Setter注入 使用Setter注入是指在Bean实例化后,通过Setter方法注入需要的依赖。在配置文件中使用 <property> 标签来配置参数,例如: <bean id="foo" class="com.example.Foo"> <property name="foo" value="fooValue" /> <property name="bar" value="barValue" /> </bean> 3. 静态工厂方法注入 使用静态工厂方法注入是指通过静态工厂方法(static factory method)来实例化Bean,并注入需要的依赖。在配置文件中使用 <bean> 标签的 factory-method 属性来指定工厂方法名称,例如: <bean id="foo" class="com.example.Foo" factory-method="createFoo"> <constructor-arg index="0" value="fooValue"/> <constructor-arg index="1" value="barValue"/> </bean> 4. 实例化工厂方法注入 使用实例化工厂方法注入是指在Bean实例化前,通过实例化工厂方法(factory method)来实例化Bean,并注入需要的依赖。在配置文件中使用 <bean> 标签的 factory-bean 和 factory-method 属性来指定工厂Bean和工厂方法名称,例如: <bean id="fooFactory" class="com.example.FooFactory"/> <bean id="foo" factory-bean="fooFactory" factory-method="createFoo"> <constructor-arg index="0" value="fooValue"/> <constructor-arg index="1" value="barValue"/> </bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值