DI依赖注入


依赖注入

通过get,set方法来实现依赖注入

我们创建一个类后,需要把该类中的属性通过get,set方法来配置,然后当给变量赋值时

1:在xml文件中配置

value给变量直接赋值

<property name="" value=""/

ref引用其他bean的配置,通常当变量是对象时使用

<property name="" ref=""/>

2:直接在java类中使用注解配置

@Value("")

通过构造体实现依赖注入

name为属性名

<constructor-arg name="" value=""/>

index为索引下标

<constructor-arg index="" value=""/>