Spring依赖注入的方式

在Spring中配置一个bean时,如果需要给该bean提供一些初始的值,则需要通过依赖注入方式实现。
依赖注入就是通过Spring将bean所需要的一些参数传递给bean实例对象的过程。
spring的依赖注入常用的3种方式:
使用属性的setter方法注入 ,这是最常用的方式;
使用构造器注入;
使用Filed注入(用于注解方式)

一、属性注入
属性注入是通过setXxx()方法注入Bean的属性值或依赖对象。
属性注入方式的优点:
**·**具有可选择性
**·**灵活性高
创建一个user类

   private String name;

    private Integer age;

    private Integer tall;

    private Car car; //引用类型

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getTall() {
        return tall;
    }

    public void setTall(Integer tall) {
        this.tall = tall;
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

创建applicationContext.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
 <!--  property注入,name为属性变量名,value为注入值 -->
 <bean id="User01" name="user1 user2" class="com.wjg.model.User">
     <property name="age" value="22"></property>
     <property name="name" value="小明"></property>
     <property name="tall" value="170"></property>
     <!-- Ref可以注入一个bean对象 -->
<property name="car" ref="car"></property>
 </bean>

<bean id="car" class="com.wjg.model.Car">
    <!-- set属性注入 -->
    <property name="carName" value="奔驰迈巴赫"/>
    <property name="carMoney" value="200w"/>
</bean>

2、 使用构造器注入
构造器注入与setter方法注入类似,需要一个具有属性参数的构造器,通过constructor-arg标签注入,该标签有5个属性
一、name属性:通过参数名找到参数列表中对应参数;
二、index属性:通过参数在参数列表中的索引找到参数列表中对应参数,index从0开始;
三、type属性:通过参数数据类型找到参数列表中对应参数;
四、value属性:设置参数列表参数对应的值,用于设定基本数据类型和String类型的数据;
五、ref属性:如果参数值为非基本数据类型,则可通过ref为参数注入值,其值为另一个bean标签id或name 属性的属性值;

<!-- 构造函数注入 使用Name属性 -->
public User(String name,Integer age,double tall,Car car){
    this.name=name;
    this.age=age;
    this.tall=tall;
    this.car=car;
}
<bean id="constructorName" class="com.wjg.model.User">
  <constructor-arg name="age" value="20" />
  <constructor-arg name="name" value="小新" />
  <constructor-arg name="tall" value="1.75" />
<constructor-arg name="weight" value="120" /
  <constructor-arg name="car" ref="car" />
</bean>
<!-- 构造函数注入 使用Index属性-->
*这两个构造器的第二个参数都为浮点型,当使用index注入时会存在歧义,比如你想注入的是第一个构造器,但Spring会大部分情况会默认匹配到后一个构造器,因此要使用type或name指定准确的构造器注入
 public User(String name,float weight,Car car){
    this.name=name;
    this.weight=weight;
    this.car=car;
}
public User(String name,double tall,Car car){
    this.name=name;
    this.tall=tall;
    this.car=car;
}

<bean id="constructorIndex" class="com.wjg.model.User">
   <constructor-arg index="0" value="小白" />
    <constructor-arg index="1" value="120" name="weight" type=”float”/>
    <constructor-arg index="2" ref="car" />
</bean>
<!-- 构造函数注入 使用type属性 -->
public User(String name,Integer age,float weight,Car car){
    this.name=name;
    this.age=age;
    this.car=car;
    this.weight=weight;
}
<bean id="constructorType" class="com.wjg.model.User">
    <constructor-arg type="java.lang.String" value="小智" />
    <constructor-arg type="java.lang.Integer" value="20" />
    <constructor-arg type="float" value="120.0" />
    <constructor-arg type="com.wjg.model.Car" ref="car" />
</bean>

(扩展) P名称空间注入、复杂类型注入和EL的使用
一、P名称空间注入:
使用P名称空间注入时要对配置文件添加xmlns:p=http://www.springframework.org/schema/p

<!-- p名称空间注入 -->
<bean name="pName" class="com.wjg.model.User" p:name="p名称空间注入" p:age="20" p:car-ref="car"/>

二、复杂类型注入:

private Object[] arr;//数组类型注入
private List list;//list类型注入
private Set set;//set 类型注入
private Map map;//map类型注入
private Properties prop;//properties类型注入
<!-- 复杂类型的注入 -->
<bean id="FzTypezr" class="com.wjg.model.FzType">
    <!--数组类型-->
    <!-- 如果数组中只准备注入一个值(对象),直接使用value|ref即可 -->
    <!--<property name="arr" value="tom" />-->
    <!--数组多个值-->
    <property name="arr">
        <array>
            <value>字符串</value>
            <value>0</value>
            <value>0.1</value>
            <value>2</value>
            <ref bean="car"/>
        </array>
    </property>
    <!--list的注入-->
    <!--<property name="list" ref="personPns"/>--><!--单个值的注入可以使用value 或ref-->
    <!--多个值的注入-->
    <property name="list">
          <list>
              <value>1</value>
              <value>2</value>
              <ref bean="car2"/>
          </list>
    </property>
    <!--set的注入-->
    <!--<property name="set" ref="personPns"/>--><!--单个值的注入可以使用value 或ref-->
    <!-- 注入重复的值虽不会报错,但不会被注入进去 -->
    <!--多个值的注入-->
    <property name="set">
        <set>
            <value>3</value>
            <value>4</value>
            <ref bean="car"/>
        </set>
    </property>
    <!--map的注入-->
    <!--map必须key-value 不能通过value 或ref单个值的注入-->
    <!--多个值的注入-->
    <property name="map">
        <map>
            <!--
            key="键值"
            key-ref="对象型值"
            value="值"
            value-ref="对象值"
            -->
            <entry key="string" value="你好"/>
            <entry key="int" value="5"/>
            <entry key="obj" value-ref="car"/>
        </map>
    </property>
    <property name="prop">
        <props>
            <prop key="string">Hello</prop>
            <prop key="name">prop</prop>
            <prop key="password">root</prop>
        </props>
    </property>
</bean>

三、依赖注入时 Spring EL的使用:

<!-- 依赖注入时Spring EL的使用 要依赖于get的获取方法才能获取-->
    <bean id="els" class="com.wjg.model.User">
        <property name="name" value="#{User01.name}"/>
        <property name="age" value="#{User01.age}"/>
        <property name="car" value="#{User01.car}"/>
   <!--<property name="car" value="#{car}"/>  可以直接引入bean对象-->
    </bean>

测试类对以上几种注入方式注入Bean

public class Demo implements Serializable {
    ApplicationContext context=null;
    
    @Before
    public void before(){
        //注入配置文件
        context=new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @Test 
public void text(){
        User user=(User) context.getBean("User01");   //setter方法注入
        User user2=(User) context.getBean("constructorName");  //name属性注入构造器
        User user3=(User) context.getBean("constructorIndex"); //index属性注入构造器
        User user4=(User) context.getBean("constructorType");   //Type属性注入构造器
        User user5=(User) context.getBean("pName");   // P标签注入
       FzType fzType=(FzType) context.getBean("FzTypezr");  //复杂类型注入
       User user6=(User) context.getBean("els");  //EL类型注入
        System.out.println(user);
        System.out.println(user2);
        System.out.println(user3);
        System.out.println(user4);
        System.out.println(user5);
        System.out.println(user6);
        System.out.println(fzType);
     }
}

@Before和@Test为Junit4的注解,在Spring Bean配置方式中提到
3、 使用Filed注入(用于注解方式)
Filed注入(全注解方式)
以下使用了java的注解,在Spring Bean配置方式中提到
Dao实现层:
在这里插入图片描述
Service实现层:
在这里插入图片描述
@Autowired与@Resource的区别:
@Autowired注解优先按照类型匹配,若有两个或两个以上相同的类型则会通过filed定义的变量名去匹配相应的name或id,若找不到则出错
@Qualifier注解辅助@Autowired注解,传入指定的bean name来匹配
@Autowired
@Qualifier(value = “StudentDao”)

@Resource注解优先按照bean name匹配,若没有指定name属性,则按照filed定义的变量名去匹配,使用filed变量名去匹配时若匹配的类型只有一个,即使变量名匹配不上也没问题会自动匹配上类型相同的那一个,若有两个或两个以上相同的类型则会出错。当指定了name属性,若找不到则会出错该注解还有一个type类型属性,可以匹配相应的类型

接着在applicationContext.xml配置文件,扫描目录
在这里插入图片描述
然后便可在测试类中操作:
在这里插入图片描述
注意在使用Filed中,要使用接口注入(IUserDao、IUserService)不能使用实现类

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值