Spring中获取某一类型下所有Bean实例的方法


转载:Spring中获取某一类型下所有Bean实例的两种方法

案例

一、使用@Resource(@Autowired)注解

@Resource(@Autowired)
Map<String, Object> map 
#String为bean的名字, Object为父类,或接口

1. 定义一个接口,两个实现类

public interface Person {
    void run();
}
@Component
public class Man implements Person {
    @Override
    public void run() {
        System.out.println("我是man,快跑!");
    }
}

@Component
public class Women implements  Person {
    @Override
    public void run() {
        System.out.println("我是women,慢跑!");
    }
}

2. 定义一个类,两个子类

public class Vehicle {

    void method(){
        System.out.println("使用交通工具。。。");
    }
}
@Component
public class Car extends Vehicle {
    public void methd(){
        super.method();
        System.out.println("开车!");
    }
}

@Component
public class Bike extends Vehicle {

    public void methd(){
        super.method();
        System.out.println("骑自行车!");
    }
}

3. 测试

@SpringBootTest(classes = App.class)
@RunWith(SpringRunner.class)
public class Test {

    @Resource
    Map<String , Person> personMap;

    @Resource
    Map<String , Vehicle> vehicleMap;

    @org.junit.Test
    public void test1(){
        System.out.println("personMap size:" + personMap.size());
        Set<Map.Entry<String, Person>> entries = personMap.entrySet();
        for (Map.Entry<String, Person> entry : entries){
            System.out.println("beanMame: " +entry.getKey());
            System.out.println("bean: " +entry.getValue());
            entry.getValue().run();
            System.out.println("******");
        }

        System.out.println("###################################");
        System.out.println("vehicleMap size:" + vehicleMap.size());
        Set<Map.Entry<String, Vehicle>> entriess = vehicleMap.entrySet();
        for (Map.Entry<String, Vehicle> entry : entriess){
            System.out.println("beanMame: " +entry.getKey());
            System.out.println("bean: " +entry.getValue());
            entry.getValue().method();
            System.out.println("******");
        }
    }
}

4. 结果

personMap size:2
beanMame: man
bean: com.zhuang.test.Man@778d82e9
我是man,快跑!
******
beanMame: women
bean: com.zhuang.test.Women@408e96d9
我是women,慢跑!
******
###################################
vehicleMap size:2
beanMame: bike
bean: com.zhuang.test.Bike@59901c4d
使用交通工具。。。
骑自行车!
******
beanMame: car
bean: com.zhuang.test.Car@168cd36b
使用交通工具。。。
开车!
******

二、使用ApplicationContext

@Resource
ApplicationContext applicationContext;

1. 测试

    @Test
    public void test2(){
        Map<String, Person> personMapContext = applicationContext.getBeansOfType(Person.class);
        Map<String, Vehicle> vehicleMapContext = applicationContext.getBeansOfType(Vehicle.class);
        System.out.println("personMapContext: " + personMapContext);
        System.out.println("vehicleMapContext: " + vehicleMapContext);
    }

2. 结果

personMapContext: {man=com.zhuang.beans.Man@111610e6, women=com.zhuang.beans.Women@4ad4936c}
vehicleMapContext: {bike=com.zhuang.beans.Bike@29d37757, car=com.zhuang.beans.Car@4fcc529}

三、实现ApplicationContextWare接口

1. 实现ApplicationContextAware

@Component
public class GetBeans implements ApplicationContextAware {
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, Person> personMap = applicationContext.getBeansOfType(Person.class);
        Map<String, Vehicle> vehicleMap = applicationContext.getBeansOfType(Vehicle.class);
        System.out.println("personMap: " + personMap);
        System.out.println("vehicleMap: " + vehicleMap);
    }
}

2. 结果

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.0)

[2023-01-06 22:37:30.513] - 10560 信息 [main] --- com.zhuang.BeanApplication: Starting BeanApplication using Java 11.0.15 on MrzhuangdeMacBook-Pro.local with PID 10560 (/Users/mrzhuang/Downloads/dev-works/beans/target/classes started by mrzhuang in /Users/mrzhuang/Downloads/dev-works)
[2023-01-06 22:37:30.539] - 10560 信息 [main] --- com.zhuang.BeanApplication: No active profile set, falling back to 1 default profile: "default"
personMap: {man=com.zhuang.beans.Man@7db0565c, women=com.zhuang.beans.Women@54ec8cc9}
vehicleMap: {bike=com.zhuang.beans.Bike@52eacb4b, car=com.zhuang.beans.Car@5528a42c}
[2023-01-06 22:37:30.985] - 10560 信息 [main] --- com.zhuang.BeanApplication: Started BeanApplication in 0.733 seconds (JVM running for 1.234)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值