2.ioc注入

本文详细介绍了Spring Boot中的IoC(Inversion of Control)和依赖注入技术,包括set方法注入、有参构造方法注入以及XML配置实例。展示了如何使用`@Autowired`注解和XML `<bean>`标签为对象注入属性,以及外部bean和内部bean的级联赋值操作。
摘要由CSDN通过智能技术生成

实体类:

public class IOCIntro {
    private String bName;
    private String bAuthor;
    public void setAddress(String address) {
        this.address = address;
    }

    private String address;
    public void setbAuthor(String bAuthor) {
        this.bAuthor = bAuthor;
    }

    public void setbName(String bName) {
        this.bName = bName;
    }
    public IOCIntro() {
    }
    public IOCIntro(String bName, String bAuthor) {
        this.bName = bName;
        this.bAuthor = bAuthor;
    }
    public void testHere(){
        System.out.println(bName+"//"+bAuthor+"//"+address);
    }
}

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:util="http://www.springframework.org/schema/util"
       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 http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
    <!--set方法注入属性-->
    <bean id="book" class="day502IOC.IOCIntro"><!--此方法其实是使用了无参构造创建对象-->
        <!--使用property完成属性的注入
        name:名称 value:值
        -->
        <property name="bName" value="set方式注入"></property>
        <!--向属性里设置空值(虽然不写也是空值)-->
        <property name="bAuthor">
            <null/>
        </property>
        <!--向属性里设置特殊值<>-->
        <property name="address">
            <value><![CDATA[<<南京>>]]]></value>
        </property>
    </bean>
    <!--有参构造方法注入属性-->
    <bean id="book2" class="day502IOC.IOCIntro">
        <constructor-arg name="bAuthor" value="有参构造输入"></constructor-arg>
        <constructor-arg name="bName" value="有参构造输入"></constructor-arg>
        <!--还可以通过索引值来构造index="0" value="..."代表第一个参数-->
    </bean>
    <bean id="book3" class="day502IOC.IOCIntro" p:bAuthor="p方式注入"></bean>
    <!--null值的设置-->
</beans>

测试类

    @Test
    public void test01(){
        //测试set方法注入属性xml 此方法必须要使用set函数
        ApplicationContext context=new ClassPathXmlApplicationContext("502Bean2.xml");
        IOCIntro book=context.getBean("book",IOCIntro.class);
        System.out.println(book);
        book.testHere();
    }
    /**
     * 测试有参构造方法的注入
     */
    @Test
    public void test02() {
        //测试set方法注入属性xml 此方法必须要使用set函数
        ApplicationContext context=new ClassPathXmlApplicationContext("502Bean2.xml");
        IOCIntro book=context.getBean("book2",IOCIntro.class);
        System.out.println(book);
        book.testHere();
    }
    //p方式注入
    @Test
    public void test03() {
        ApplicationContext context=new ClassPathXmlApplicationContext("502Bean2.xml");
        IOCIntro book=context.getBean("book3",IOCIntro.class);
        System.out.println(book);
        book.testHere();
    }

外部bean方法的注入

接口和实现类

public interface UserDao {
     void update();
}

public class UserDaoImpl implements UserDao {
    @Override
    public void update() {
        System.out.println("dao update");
    }
}

service:

public class UserService {
    //创建userdao类型的属性,生成set方法
    private  UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void add(){
        System.out.println("service的add");
        userDao.update();
        //普通方法
//        UserDao userDao=new UserDaoImpl();//接口多态
//        userDao.update();
    }
}

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对象的创建-->
    <bean id="userService" class="day502IOC.UserService">
        <!--注入userDao对象-->
        <property name="userDao" ref="userDaoImpl"></property>
    </bean>
    <!--bean在外面所以是外部bean-->
    <bean id="userDaoImpl" class="day502IOC.UserDaoImpl"></bean>
</beans>

内部bean和级联赋值

实体类

public class Department {
    int did;

    public void setDid(int did) {
        this.did = did;
    }

    @Override
    public String toString() {
        return "Department{" +
                "did=" + did +
                '}';
    }
}
public class Employee {
    private String ename;
    private Map<String,String> map;
    private Set<Department> departmentSet;

    public Set<Department> getDepartmentSet() {
        return departmentSet;
    }

    public void setDepartmentSet(Set<Department> departmentSet) {
        this.departmentSet = departmentSet;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public int[] getNumber() {
        return number;
    }

    public void setNumber(int[] number) {
        this.number = number;
    }

    private List<String> list;
    private int[] number;

    public Department getDep() {
        return dep;
    }

    private Department dep;

    public void setEname(String ename) {
        this.ename = ename;
    }

    public void setDep(Department dep) {
        this.dep = dep;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "ename='" + ename + '\'' +
                ", dep=" + dep +
                '}';
    }
    public void show(){
        System.out.println(map);
        System.out.println(list);
        System.out.println(number);
    }
    public void showSet(){
        departmentSet.forEach(System.out::println);
    }
}

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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util.xsd">
    <bean id="emp" class="day502IOC.Employee">
        <property name="ename" value="charles"></property>
        <!--内部bean嵌套创建对象-->
        <property name="dep">
            <bean id="dept" class="day502IOC.Department">
                <property name="did" value="1"></property>
            </bean>
        </property>
    </bean>
    <!--级联赋值1(和外部bean相似)-->
    <bean id="emp2" class="day502IOC.Employee" scope="prototype">
        <property name="ename" value="charles"></property>
        <!--内部bean嵌套创建对象-->
        <property name="dep" ref="dept2"></property>
    </bean>
    <bean id="dept2" class="day502IOC.Department">
        <property name="did" value="2"></property>
    </bean>

    <!--级联赋值2(需要get方法)-->
    <bean id="emp3" class="day502IOC.Employee">
        <property name="ename" value="charles"></property>
        <!--内部bean嵌套创建对象-->
        <property name="dep" ref="dept3"></property>
        <property name="dep.did" value="3"></property>
    </bean>
    <bean id="dept3" class="day502IOC.Department">
    </bean>
    <!--特殊的类型属性注入-->
    <bean id="emp4" class="day502IOC.Employee">
        <property name="number">
            <!--使用array也可以-->
            <list>
                <value>1</value>
                <value>2</value>
            </list>
        </property>
        <property name="list">
            <list>
                <value>ni</value>
                <value>hao</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="Java" value="good"></entry>
            </map>
        </property>
    </bean>
    <!--值是对象的集合注入并且变成公共部分-->
    <bean id="emp5" class="day502IOC.Employee">
        <property name="departmentSet" >
           <list>
                <ref bean="deptset1"></ref>
                <ref bean="deptset2"></ref>
            </list>
        </property>
        <!--外部注入-->
        <property name="list" ref="departmentList"></property>
    </bean>
    <!--创建多个dept对象-->
    <!--这样其他的bean也能用这个list-->
    <util:list id="departmentList">
        <value>外部注入1</value>
        <value>外部注入2</value>
    </util:list>
    <bean id="deptset1" class="day502IOC.Department">
        <property name="did" value="11"></property>
    </bean>
    <bean id="deptset2" class="day502IOC.Department">
        <property name="did" value="22"></property>
    </bean>
</beans>

测试类:

    //测试内部bean
    @Test
    public void test05() {
        ApplicationContext context=new ClassPathXmlApplicationContext("502bean4.xml");
        Employee emp = context.getBean("emp", Employee.class);
        System.out.println(emp);
    }
    //测试级联赋值 以及特殊属性的赋值
    @Test
    public void test06() {
        ApplicationContext context=new ClassPathXmlApplicationContext("502bean4.xml");
        Employee emp = context.getBean("emp2", Employee.class);
        System.out.println(emp);
        System.out.println("------------------");
        Employee emp3 = context.getBean("emp3", Employee.class);
        System.out.println(emp3);
        System.out.println("------------------");
        Employee employee=context.getBean("emp4",Employee.class);
        employee.show();
        System.out.println("-------集合的元素是对象-----------");
        Employee emp5 = context.getBean("emp5", Employee.class);
        emp5.showSet();
        //外部注入
        emp5.show();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值