0
点赞
收藏
分享

微信扫一扫

Spring 子类注入

兵部尚输 2022-02-15 阅读 46


1,准备

一个父类接口,三个子类接口,一个子类的子类接口

public interface FatherInterface {}
public class InterfaceOne implements FatherInterface{}
public class InterfaceThree implements FatherInterface{}
public class InterfaceTwo implements FatherInterface{}
public class SanOne extends InterfaceOne {}

测试:

import com.g7go.jwt.common.reg.FatherInterface;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RunWith(SpringRunner.class)
@SpringBootTest
public class JwtApplicationTests {

@Autowired
List<FatherInterface> fatherInterfaces1;

@Autowired
Map<String, FatherInterface> fatherInterfaces2;

@Autowired
Set<FatherInterface> fatherInterfaces3;


@Test
public void test() {
System.out.println(fatherInterfaces1);
System.out.println(fatherInterfaces2);
System.out.println(fatherInterfaces3);
}
}

执行结果

[com.g7go.jwt.common.reg.InterfaceOne@4b1abd11, com.g7go.jwt.common.reg.InterfaceThree@3f36b447, com.g7go.jwt.common.reg.InterfaceTwo@6443b128]
{interfaceOne=com.g7go.jwt.common.reg.InterfaceOne@4b1abd11, interfaceThree=com.g7go.jwt.common.reg.InterfaceThree@3f36b447, interfaceTwo=com.g7go.jwt.common.reg.InterfaceTwo@6443b128}
[com.g7go.jwt.common.reg.InterfaceOne@4b1abd11, com.g7go.jwt.common.reg.InterfaceThree@3f36b447, com.g7go.jwt.common.reg.InterfaceTwo@6443b128]

结论:只能注入其直接子类。


举报

相关推荐

0 条评论