spring5基础学习之ioc

接上昨天的随笔,继续ioc操作练习

今天主要继续xml配置中的内部bean,外包bean,级联赋值操作

1.注入外部bean(将外部的对象注入userservice)

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

<!--    service和dao对象创建-->
    <bean id="userService" class="service.UserService">
<!--        注入userDaodui对象-->
<!--        name属性值:类里面的属性名称-->
        <property name="userdao" ref="userDao_1"></property>
    </bean>
    <bean id="userDao_1" class="dao.Userdaoimpl">
    </bean>
</beans>
View Code

类:

package dao;


public interface Userdao {
    public void updata();
}
package dao;

public class Userdaoimpl implements Userdao{
    @Override
    public void updata(){
        System.out.println("dao updata.......");
    }
}
View Code
package service;

import dao.Userdao;

public class UserService {
    private Userdao userdao;

    public void setUserdao(Userdao userdao) {
        this.userdao = userdao;
    }

    //创建userdao类型属性,生成set方法
    public void add(){
        System.out.println("service add......");
        userdao.updata();
    }
}
View Code

测试:

@Test
    public void testadd() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
        //获取配置的创建对象
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();

    }
View Code

 

2.注入属性-内部 bean(将内部的dept对象注入emp对象中)

 

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

<!--    内部bean-->
    <bean id="emp" class="bean.emp">
<!--        设置两个普通的属性-->
        <property name="ename" value="chenghaixiang"></property>
        <property name="gender" value="男"></property>

<!--        设置对象类型属性-->
        <property name="dept">
            <bean id="dept" class="bean.Dept">
                <property name="dname" value="财务"></property>
            </bean>
        </property>
    </bean>
</beans>
View Code

类:

package bean;

public class Dept {
    private String dname;

    public void setDname(String dname) {
        this.dname = dname;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "dname='" + dname + '\'' +
                '}';
    }
}
package bean;

public class emp {
    private String ename;
    private String gender;
    //员工属于某个部门,使用对象形式表示
    private Dept dept;
    public void setEname(String ename) {
        this.ename = ename;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }
    public void add(){
        System.out.println(ename+","+gender+","+dept);
    }
}
View Code

测试就不放上去了

3.级联赋值(与内部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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--   级联赋值-->
    <bean id="emp" class="bean.emp">
        <!--        设置两个普通的属性-->
        <property name="ename" value="chenghaixiang"></property>
        <property name="gender" value="男"></property>

        <!--        设置对象类型属性-->
        <property name="dept" ref="dept"></property>
    </bean>
    <bean id="dept" class="bean.Dept">
        <property name="dname" value="财务"></property>
    </bean>
</beans>
View Code

posted @ 2022-04-08 15:36  chenghaixinag  阅读(20)  评论(0编辑  收藏  举报