spring基于XML的依赖注入方式

一、xml自动装配
java代码:

public class Person {
	private Cat cat;
	public Person(Cat cat) {
		this.cat = cat;
	}
	public void setCat(Cat cat) {
		this.cat = cat;
	}
}


public class Cat {
	public Cat() {
		//nothing...
	}
}

xml配置:
①byName

<bean id="person" class="com.fly.spring_demo.Person" autowire="byName"></bean>
<bean id="cat" class="com.fly.spring_demo.Cat"></bean>

②byType:

<bean id="person" class="com.fly.spring_demo.Person" autowire="byType"></bean>
<bean id="helloKitty" class="com.fly.spring_demo.Cat"></bean>

③construct:

<bean id="person" class="com.fly.spring_demo.Person" autowire="constructor"></bean>
<bean id="cat" class="com.fly.spring_demo.Cat"></bean>

④no:

<bean id="person" class="com.fly.spring_demo.Person" autowire="no">
	<property name="cat"  ref="cat"/>
</bean>
<bean id="cat" class="com.fly.spring_demo.Cat"></bean>

这种方式缺点比较多,一般不用:
①如果一个属性用了这种方式,其他属性也得用这种方式;
②只能用同一种方式:ByName或者ByType;
③你甚至不知道哪个bean中注入了哪个bean中,维护代价太高。

二、setter注入

<!-- 2setter注入 -->
<bean id="person" class="com.fly.spring_demo.Person">
	<property name="cat" ref="cat"/>
</bean>
<bean id="cat" class="com.fly.spring_demo.Cat"></bean>

三、构造器注入

注:常量值、string的按类型注入,按索引注入,比较简单吗,这里就不说了。

<!-- 3.构造器注入 -->

<bean id="person" class="com.fly.spring_demo.Person">
	<constructor-arg name="cat" ref="cat"></constructor-arg>
</bean>
<bean id="cat" class="com.fly.spring_demo.Cat"></bean>

四、工厂方法注入(静态工厂和实例工厂)
1、静态工厂

public class CatFactory {
	//静态工厂方法
	public static Cat getCatInstance() {
		return new Cat();
	}
}
<bean id="person" class="com.fly.spring_demo.Person">
		<property name="cat" ref="cat"/>
</bean>
<bean id="cat" class="com.fly.spring_demo.CatFactory" factory-method="getCatInstance"></bean>

2、实例工厂方法

public class CatFactory {
	//实例工厂方法
	public Cat getCatInstance() {
		return new Cat();
	}
}

<bean id="person" class="com.fly.spring_demo.Person">
		<property name="cat" ref="cat"/>
</bean>
 <bean id="catFactory" class="com.fly.spring_demo.CatFactory"></bean>
 <bean id="cat" factory-bean="catFactory" factory-method="getCatInstance"></bean>
  • 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、付费专栏及课程。

余额充值