Autowiring beans

Insert image description here
Insert image description here

package com.elf.spring.test;

import com.elf.spring.bean.Monster;
import com.elf.spring.web.OrderAction;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author 45~
 * @version 1.0
 */

public class SpringBeanTest {
    
    
    //通过自动装配来对属性赋值
    @Test
    public void setBeanByAutowire() {
    
    
        ApplicationContext ioc =
                new ClassPathXmlApplicationContext("beans033.xml");

        OrderAction orderAction = ioc.getBean("orderAction", OrderAction.class);
        //验证是否自动装配上OrderService
        System.out.println(orderAction.getOrderService());
        //验证是否自动装配上了OrderDao
        System.out.println(orderAction.getOrderService().getOrderDao());
    }

//    @Test
//    public void getMonster(){
    
    
//        ApplicationContext ioc =
//                new ClassPathXmlApplicationContext("beans.xml");
//        //Object monster01 = ioc.getBean("monster01");
//        Monster monster01 = (Monster) ioc.getBean("monster01");
//        System.out.println("monster"+ monster01+"运行类型"+monster01.getClass());
//        System.out.println("ok~~~");
//    }
}

<?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: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/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--配置OrderDao对象-->
    <bean class="com.elf.spring.dao.OrderDao" id="orderDao"/>
    <!--配置OrderService对象
        老师解读
        1. autowire="byType" 表示 在创建 orderService时
           通过类型的方式 给对象属性 自动完成赋值/引用
        2. 比如OrderService 对象有 private OrderDao orderDao
        3. 就会在容器中去找有没有 OrderDao类型对象
        4. 如果有,就会自动的装配, 老师提示如果是按照 byType 方式来装配, 这个容器中,不能有两个OrderDao类型对象
        5. 如果你的对象没有属性,  autowire就没有必要写
        6. 其它类推..

        7. 如果我们设置的是 autowire="byName" 表示通过名字完成自动装配
        8. 比如下面的 autowire="byName" class="com.elf.spring.service.OrderService"
           1) 先看 OrderService 属性 private OrderDao orderDao
           2) 再根据这个属性的setXxx()方法的 xxx 来找对象id
           3) public void setOrderDao() 就会找id=orderDao对象来进行自动装配
           4) 如果没有就装配失败

    -->
    <bean autowire="byName" class="com.elf.spring.service.OrderService"
          id="orderService"/>

    <!--配置OrderAction-->
    <bean autowire="byName" class="com.elf.spring.web.OrderAction" id="orderAction"/>
    <!--指定属性文件
     老师说明
     1. 先把这个文件修改成提示All Problem
     2. 提示错误,将光标放在context 输入alt+enter 就会自动引入namespace
     3. location="classpath:my.properties" 表示指定属性文件的位置
     4. 提示,需要带上 classpath
     5. 属性文件有中文,需要将其转为unicode编码-> 使用工具
     -->
    <!--<context:property-placeholder location="classpath:my.properties"/>-->
    <!--配置Monster对象
    1.通过属性文件给monster对象的属性赋值
    2. 这时我们的属性值通过${
    
    属性名}
    3. 这里说的 属性名 就是 my.properties文件中的 k=v 的k
    -->
    <!--<bean class="com.elf.spring.bean.Monster" id="monster1000">-->
    <!--    <property name="monsterId" value="${monsterId}"/>-->
    <!--    <property name="skill" value="${skill}"/>-->
    <!--    <property name="name" value="${name}"/>-->
    <!--</bean>-->

</beans>
package com.elf.spring.dao;

/**
 * @author 45~
 * @version 1.0
 * Dao类
 */
public class OrderDao {
    
    
    //方法
    public void saveOrder(){
    
    
        System.out.println("保存一个订单...");
    }
}


package com.elf.spring.service;

import com.elf.spring.dao.OrderDao;

/**
 * @author 45~
 * @version 1.0
 * Service类
 */
public class OrderService {
    
    
    //OrderDao属性
    private OrderDao orderDao;

    //getter方法和setter方法
    public OrderDao getOrderDao() {
    
    
        return orderDao;
    }
    public void setOrderDao(OrderDao orderDao) {
    
    
        this.orderDao = orderDao;
    }
}

package com.elf.spring.web;

import com.elf.spring.dao.OrderDao;
import com.elf.spring.service.OrderService;

/**
 * @author 45~
 * @version 1.0
 * Servlet就是Controller
 */
public class OrderAction {
    
    
    //属性OrderService,即Action要用到service
    private OrderService orderService;

    //getter
    public OrderService getOrderService() {
    
    
        return orderService;
    }

    //setter
    public void setOrderService(OrderService orderService) {
    
    
        this.orderService = orderService;
    }
}

Guess you like

Origin blog.csdn.net/weixin_45036508/article/details/132711765