依赖注入DI

依赖IOC环境,注入属性值 首先需要有bean的配置,然后才能往里面弄注入属性

spring在创建类的过程中将类的属性设置进去。

注入方式

如果属性是基本数据类型: 那么就使用 value 注意:在这里 String也是基本数据类型

如果是引用数据类型,那么就使用 ref 需要创建对应的类对象注入
首先先写属性注入需要用到的实体类
两个类都要生成get和set方法 全参 无参 toStiring


public class User implements Serializable {

    private Integer id;
    private String username;
    private String address;
    private Date birthday;
    private boolean gender;

    private Role role;

public class Role implements Serializable {

    private Integer id;
    private String roleName;
    private String roleDesc;
    private String roleUpdate;

注入会用到的共用代码

 <!--配置Date对象,。,给上面的birthday使用-->
    <bean id="date" class="java.util.Date"></bean>
    <!--配置role的对象-->
    <bean id="role" class="com.zhiyou100.pojo.Role">
        <property name="id" value="1"></property>
        <property name="roleDesc" value="打扫卫生"></property>
        <property name="roleName" value="保洁阿姨"></property>
        <property name="roleUpdate" value="2020-1-3"></property>
    </bean>

1.set注入方法:必须保证实体类中有set方法
xml代码

<!--DI  注入-->
    <!--方式一:set方法注入 主要是用 property  首先是需要有bean环境-->
    <bean name="user" class="com.zhiyou100.pojo.User">
        <!--name  表示实体类里卖弄的属性    value  就是赋值-->
        <property name="address" value="郑州"></property>
        <property name="birthday" ref="date"></property>
        <property name="username" value="小孙"></property>
        <property name="id" value="1"></property>
        <property name="gender" value="true"></property>
        <property name="role" ref="role"></property>
    </bean>

2.构造方法注入
xml代码

<!--方式二:构造方法注入 使用 constructor-arg   -->
    <bean id="user2" class="com.zhiyou100.pojo.User">
        <!--  name 参数名称(属性名称)   value 参数赋值   ref  表示需要映入的bean对象   index 全参里面参数的索引  type  参数类型  -->
        <constructor-arg name="id" value="1" index="0" type="java.lang.Integer"></constructor-arg>
        <constructor-arg name="username" value="aaa" index="1" type="java.lang.String"></constructor-arg>
        <constructor-arg name="address" value="bbb" index="2" type="java.lang.String"></constructor-arg>
        <constructor-arg name="birthday" ref="date" index="3" type="java.util.Date"></constructor-arg>
        <constructor-arg name="gender" value="true" index="4" type="boolean"></constructor-arg>
        <constructor-arg name="role" ref="role" index="5" type="com.zhiyou100.pojo.Role"></constructor-arg>
    </bean>

3.P名称空间注入
xml代码

<!--方式三:P名称空间注入  直接在属性里面写  p: 报红直接创建  p: 属性 = "赋值"   如果是基本数据类型或者String就写属性名    如果是其他类型就写  属性—ref   -->

    <bean name="user3" class="com.zhiyou100.pojo.User" p:id = "1" p:address="111" p:birthday-ref="date" p:role-ref="role" p:username="444" ></bean>

4.表达式注入
xml代码

<!--方式四:表达式注入 用property-->
    <bean id="user4" class="com.zhiyou100.pojo.User">
        <!--表示里面可以写表达式-->
        <property name="id" value="#{role.id == 1 ? 1 : 2 }"></property>
        <property name="username" value="#{user.username.equals('小孙') ? '小赵' : '小周' }"></property>
        <property name="address" value="平顶山"></property>
        <property name="gender" value="true"></property>
        <property name="birthday" ref="date"></property>
        <property name="role" ref="role"></property>
    </bean>

测试类代码

public class BeanDITest {

    /**
     * DI  set注入
     */
    @Test
    public void testSet(){
        //set方法注入
        //创建容器
        ApplicationContext container = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从容器当中获取到想要的对象
        User user = container.getBean("user", User.class);
        System.out.println(user);

    }

    /**
     * DI  全参构造注入
     */
    @Test
    public void testConstructor(){
        //set方法注入
        //创建容器
        ApplicationContext container = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从容器当中获取到想要的对象
        User user = container.getBean("user2", User.class);
        System.out.println(user);

    }


    /**
     * DI  p名称空间注入
     */
    @Test
    public void testP(){
        //set方法注入
        //创建容器
        ApplicationContext container = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从容器当中获取到想要的对象
        User user = container.getBean("user3", User.class);
        System.out.println(user);

    }




    /**
     * DI  表达式注入
     */
    @Test
    public void testSPel(){
        //set方法注入
        //创建容器
        ApplicationContext container = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从容器当中获取到想要的对象
        User user = container.getBean("user4", User.class);
        System.out.println(user);

    }


}

补充复杂类型注入

分为数组类型和集合类型 List Map Set Properties
备注: 以上使用的复杂类型,主要的存储结构相同的,里面的嵌套标签可以混搭,例如 Array List Set 等都是单列数据结构就可以搭配使用

首先先写需要用到的实体类

public class ComplexObject implements Serializable {
    /**
     *  数组类型  []
     *
     *  集合类型 List Set Map Properties
     */
    private Object[] arr;
    private List<Object> myList;
    private Set<Object> mySet;
    private Map<Object,Object> myMap;
    private Properties properties;



public class User implements Serializable {

  
    private String username;
    private String address;


然后去applicationContext.xml主配置文件里面进行复杂类型的配置

<!--配置user对象 一-->
    <bean name="user1" class="com.zhiyou100.pojo.User" >
        <property name="address" value="河南郑州"></property>
        <property name="username" value="小孙"></property>
     </bean>

    <!--配置user对象 二 -->
   <bean name="user2" class="com.zhiyou100.pojo.User">
       <property name="address" value="河南开封"></property>
       <property name="username" value="小王"></property>
   </bean>

    <!--复杂类型注入-->
    <!--id  表示实体类的别名 class 表示实体类的具体位置  -->
    <bean id = "complexModel" class="com.zhiyou100.pojo.ComplexObject">
        <!--数组类型注入-->
        <property name="arr">
            <array>
                <!--往arr属性中注入具体的值 值可以注入一个,也可以注入多个,数量不限-->
                <!--如果是基本类型就使用value -->
                <value>Java</value>
                <value>C</value>
                <!--往arr中注入一个对象类型引用类型就使用   <ref  bean = "引用类型别名"     -->
                <ref bean="user1"></ref>
            </array>
        </property>

        <!--List类型注入-->
        <property name="myList">
            <!--List集合  list标签   -->
            <list>
                <!--注入基本类型value-->
                <value>Python</value>
                <value>Php</value>
                <!--注入引用类型  ref标签-->
                <ref bean="user2"></ref>
            </list>
        </property>

        <!--set类型注入-->
        <property name="mySet">
            <!--set集合使用标签set-->
            <set>
                <!--注入基本类型  用标签value-->
                <value>C++</value>
                <value>JavaScript</value>
                <!--注入引用类型  用标签 ref -->
                <ref bean="user1"></ref>
            </set>
        </property>

        <!--Map类型注入-->
        <property name="myMap">
            <!--map用标签 map   因为是键值对存储,所以后面都是使用键值对-->
            <map>
                <!--注入基本类型-->
                <!--内部使用标签  <entry key="需要复制的属性" value="赋值,值是基本数据类型"></entry> -->
                <!-- <entry key = "需要赋值的属性" value-ref="赋值,值是引用数据类型"></entry>  -->
                <entry key="课程" value="英语"></entry>
                <entry key="专业" value="Java"></entry>
                <!--注入引用类型  value-ref-->
                <entry key = "计算机系" value-ref="user1"></entry>
            </map>
        </property>

        <!--Properties-->
        <property name="properties">
            <!--集合properties  使用标签 props-->
            <props>
                <!--内部标签使用  <prop key="需要复制的属性" >需要赋的值</prop>  -->
                <prop key="sex">男性</prop>
                <prop key="gender">女性</prop>
                <prop key="sexuality">未知</prop>
            </props>
        </property>
    </bean>

最后去测试一波

public class DIComplexTest {


    /**
     * 测试 复杂类型注入
     */
    @Test
    public void testDI() {
        // 加载容器   这里是为了读取配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 从容器中取出想要的对象  complexModel就是我们读取bean的别名    ComplexObject.class表示我们引用的类的字节码对象
        ComplexObject complexModel = context.getBean("complexModel", ComplexObject.class);
        System.out.println(complexModel);
    }
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值