Spring DI依赖注入的几种方式

构造注入

使用有参构造方式(三种)

都需要使用标签

第一种:使用下标(index)

​ index=“0” 就是有参构造方法里面的第一个参数,以此类推

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--使用有参构造第一种方式:使用下标-->
    <bean id="user" class="com.jie.ioc01.User">
        <constructor-arg index="0" value="李四"></constructor-arg>
        <constructor-arg index="1" value="19"></constructor-arg>
    </bean>

</beans>

测试,输出结果:

User{name='李四', age=19}	

第二种:根据类型来创建(type)

​ 不推荐这种,要是构造方法中参数的类型都一样的话容易分不清楚

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--使用有参构造第二种方式——不推荐:根据类型来创建-->
    <bean id="user" class="com.jie.ioc01.User">
        <constructor-arg type="java.lang.String" value="王五"></constructor-arg>
        <constructor-arg type="int" value="16"></constructor-arg>
    </bean>

</beans>

测试,输出结果:

User{name='王五', age=16}

第三种:使用参数名(name)

​ 推荐这种方式的,简单明了,一一对应类中的属性

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

   <!--使用有参构造第三种方式——常用的:使用参数名来创建-->
    <bean id="user" class="com.jie.ioc01.User">
        <constructor-arg name="name" value="张三"></constructor-arg>
        <constructor-arg name="age" value="22"></constructor-arg>
    </bean>

</beans>

测试,输出结果:

User{name='张三', age=22}

Set注入

创建Address类

package com.jie.di;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}

创建Student类

package com.jie.di;

import java.util.Arrays;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Student {

    private String name;
    private int age;
    private Address address;
    private String[] books;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;
    
    //省略get set方法
    
    @Override
    public String toString() {
        return
                "name='" + name + "\n" +
                "age=" + age +"\n" +
                "address=" + address +"\n" +
                "books=" + Arrays.toString(books) +"\n" +
                "hobbys=" + hobbys + "\n" +
                "card=" + card + "\n" +
                "games=" + games + "\n" +
                "wife='" + wife + "\n" +
                "info=" + info;
    }
}

bean.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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address" class="com.jie.di.Address">
        <property name="number" value="广东深圳"></property>
    </bean>

    <bean id="student" class="com.jie.di.Student">

        <!--第一种注入:普通注入,value-->
        <property name="name" value="张三" />
        <property name="age" value="22" />

        <!--第二种注入:Bean注入(引用数据类型),ref-->
        <property name="address" ref="address" />

        <!--第三种注入:数组-->
        <property name="books">
            <!--value-type就是数组的类型-->
            <array value-type="java.lang.String" >
                <value>java</value>
                <value>js</value>
                <value>mysql</value>
            </array>
        </property>

        <!--第四种注入:List-->
        <property name="hobbys">
            <!--value-type list集合的类型-->
            <list value-type="java.lang.String">
                <value>跑步</value>
                <value>听歌</value>
                <value>打游戏</value>
            </list>
        </property>

        <!--第五种注入:Map-->
        <property name="card">
            <!--
                key-type:key的类型
                value-type:value的类型
            -->
            <map key-type="java.lang.String" value-type="java.lang.String">
                <entry key="QQ号" value="23169845484"></entry>
                <entry key="微信号" value="1546752854"></entry>
            </map>
        </property>

        <!--第六种注入:Set-->
        <property name="games">
            <set value-type="java.lang.String">
                <value>守望先锋</value>
                <value>CSGO</value>
                <value>LOL</value>
            </set>
        </property>

        <!--第七种注入:null-->
        <property name="wife">
            <null/>
        </property>

        <!--第八种注入:Properties-->
        <property name="info">
            <props>
                <prop key="a">AAA</prop>
                <prop key="b">BBB</prop>
            </props>
        </property>
    </bean>
</beans>

测试注入:

package com.jie.di;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        Student student = (Student) context.getBean("student");
        //查看是否注入成功
        System.out.println(student.toString());
    }
}

运行结果:

name='张三
age=22
address=Address{address='广东深圳'}
books=[java, js, mysql]
hobbys=[跑步, 听歌, 打游戏]
card={QQ号=23169845484, 微信号=1546752854}
games=[守望先锋, CSGO, LOL]
wife='null
info={b=BBB, a=AAA}

很明显,我们全部注入成功了以上就是我们常见的几种注入方式了

其他方式注入

要想使用p命名空间和c命名空间的话必须在xml中的标签中加入:

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"

p命名空间(相当于是标签的语法糖)接下来演示下:

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
	
    <--省略了<property>标签改为了p:address-->
    <bean id="address" class="com.jie.di.Address" p:address="广东深圳"/>
<beans>    

测试运行:

package com.jie.di;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("bean02.xml");
        //加入javabean对应的class类对象就不需要每次都强转了
        Address address = context.getBean("address", Address.class);
        //查看是否注入成功
        System.out.println(address.toString());
    }
}

结果:

Address{address='广东深圳'}

c命名空间(就是构造注入标签的语法糖)

​ 注意一定要有 有参构造方法才能使用c命名空间

<?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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
	
    <--省略了<constructor-arg>标签改为了c:address-->
    <bean id="address2" class="com.jie.di.Address" c:address="湖南长沙"/>
<beans> 

测试运行结果:

Address{address='湖南长沙'}

bean的作用域

单例模式(singleton):(spring默认机制)

​ 不管获取多少个对象,都是使用着同一个对象

<?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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
	
    <--scope属性可以更改bean的作用域-->
    <bean id="address" class="com.jie.di.Address" scope="singleton"/>
<beans> 

测试:

package com.jie.di;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
    public static void main(String[] args) {
    
        ApplicationContext context = new ClassPathXmlApplicationContext("bean02.xml");
        Address address = context.getBean("address", Address.class);
        Address address2 = context.getBean("address", Address.class);
        //比较两个对象是否是同一个对象
        System.out.println(address == address2);  //输出结果是:true,证明使用着同一个对象
        
    }
}

原型模式(prototype)

​ 每次获取都创建了一个新的对象

<?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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
	
    <--scope属性可以更改bean的作用域-->
    <bean id="address2" class="com.jie.di.Address" scope="prototype"/>
<beans> 

测试:

package com.jie.di;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
    public static void main(String[] args) {
    
        ApplicationContext context = new ClassPathXmlApplicationContext("bean02.xml");
        Address address = context.getBean("address2", Address.class);
        Address address2 = context.getBean("address2", Address.class);
        //比较两个对象是否是同一个对象
        System.out.println(address == address2);  //输出结果是:false,证明每次都是new了一个新对象
        
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值