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