注入:ByName和ByType(重点)

Spring的set注入中,autowire的选值有五种,比较常用的是byType属性和byName属性。下面我们通过一个简单的项目来对他们进行大致说明。
在这里插入图片描述
autowire=byName,顾名思义,它是依靠名称来对bean里面的内容来将“依赖对象”和“被依赖对象”进行联系。(也就是我们常说的引用)
下面我们通过工程来说明。
本次以来我们采用四个不同依赖注入呈环状来说明:
byType注入分别为:同类注入,继承类注入,接口注入
在这里插入图片描述
我们根据需求,创建下面的类和接口:

在这里插入图片描述
下面我们用下图的思想来表现他们之间的关系
1、Menu是接口,而MemuIpl是Menu的实现类。
2、Perm与Permission是继承关系,Perm为父类,Permission是子类。
在这里插入图片描述
3、MenuImpl、Permission、Role、Person是依赖关系的被注入对象(可以理解为我们项目中需要导的包)。

下面:我们来对所需的类进行创建
Menu

package cn.kgc.entity;

public interface Menu {
	public void delet();
}

MenuImpl

package cn.kgc.entity;

public class MenuImpl implements Menu{
    private Integer id;
    private String memuName;
    private Person person;
    @Override
    public void delet() {
        System.out.println("接口实现方法");
    }
}

Perm

package cn.kgc.entity;

public class Perm{
    private Integer id;
    private String pname;
    private Menu menu;
    }

Permission

package cn.kgc.entity;

public class Permission extends Perm{
    public void add(){
        System.out.println("子类特有的方法");
    }
}

Person

package cn.kgc.entity;

public class Person {
    private Integer id;
    private String name;
    private Role role;
}

Role

package cn.kgc.entity;

public class Role {
    private Integer id;
    private String roleName;
    private Perm perm;
    }

注意:以上的类我们均省略了私有属性的getter,setter方法。

接下来,我们来编写DOC容器

<?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="menu" class="cn.kgc.entity.MenuImpl" autowire="byName">
        <property name="id" value="101"/>
        <property name="memuName" value="鱼香肉丝"/>
        <property name="person" ref="person"/>
    </bean>
    <bean id="person" class="cn.kgc.entity.Person" autowire="byType">
        <property name="id" value="201"/>
        <property name="name" value="tom猫🐱"/>
    </bean>
    <bean id="role" class="cn.kgc.entity.Role" autowire="byType">
        <property name="id" value="301"/>
        <property name="roleName" value="系统操作者"/>
    </bean>
    <bean id="permission" class="cn.kgc.entity.Permission" autowire="byType">
        <property name="id" value="401"/>
        <property name="pname" value="超级管理员"/>
    </bean>
</beans>

通过以上类与类之间的关系,我们可以更清晰的理解ByType的依赖注入的三种情况:分别是同一类型注入、父子继承关系中的调用注入、接口与实现类关系注入。

接下来我们来进行测试

package cn.kgc.test;

import cn.kgc.entity.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AllTest {
    @Test
    public void run(){
        String config="applicationContext.xml";
        ApplicationContext ac=new ClassPathXmlApplicationContext(config);
        Person person=(Person) ac.getBean("person");
        Role role=person.getRole();
        Perm permission=person.getRole().getPerm();
        Menu menu=person.getRole().getPerm().getMenu();
        System.out.println("引用自动注入ByName:"+person.getName()
                +"\n引用自动注入ByType同源(一致性):"+role.getRoleName()
                +"\n引用自动注入ByType同源(父子类):"+permission.getPname()
                +"\n引用自动注入ByType同源(接口):"+((MenuImpl) menu).getMemuName());
        ((Permission) permission).add();
        menu.delet();
    }
}

总结,在注入中,我们给父类注入什么样的子类(前提:该子类继承了父类且不是抽象类),给接口注入什么什么样的实现类(前提:该类实现了接口)就能通过转型获取相应的方法。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值