List, Map of Spring @Autowired injection class

A callback interface has multiple callback logics. When the project starts, it is necessary to put the implementation classes of the callback interface into a list and then call back the specific method of each implementation class. In Springboot, all implementation classes of the callback interface can be injected through @Autowired.

Demo

Callback interface: ServiceTest

public interface ServiceTest {
    
    
    public String getName();
}

Implement class 1

@Component
public class ServiceTestImpl1 implements ServiceTest {
    
    
    @Override
    public String getName() {
    
    
        return "ServiceTestImpl1";
    }
}

Implement class 2

@Component
public class ServiceTestImpl2 implements ServiceTest {
    
    
    @Override
    public String getName() {
    
    
        return "ServiceTestImpl2";
    }
}

Implement class 3

@Component
public class ServiceTestImpl3 implements ServiceTest {
    
    
    @Override
    public String getName() {
    
    
        return "ServiceTestImpl3";
    }
}

test class

@Component
public class Test implements InitializingBean {
    
    

    @Autowired
    private List<ServiceTest> serviceTests;

    public String serviceTest() {
    
    
        StringBuffer sb = new StringBuffer();
        for (ServiceTest serviceTest : serviceTests) {
    
    
            sb.append(serviceTest.getName());
            sb.append(" ");
        }
        return sb.toString();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
    
    
        System.out.println(serviceTest());
    }
}

output

ServiceTestImpl1 ServiceTestImpl2 ServiceTestImpl3 


This is a special injection feature of Spring

When injecting a Map, the generic type of value is T, then Spring will put the instantiated bean into the value after injection, and the key is the name of the injected bean

When injecting a List, the generic type of List is T, then Spring will put the instantiated bean into the List after injection

define an interface

public interface UserService {
    
    
    
}

two implementation classes

@Service("beijing")
public class BeijingUserServiceImpl implements UserService{
    
    
    
}

@Service("shanghai")
public class ShanghaiServiceImpl implements UserService {
    
    
    
}

test class

@Autowired
Map<String, UserService> map ;

@Autowired
List<UserService> list;

public void test(){
    
    
     for (Map.Entry m : map.entrySet()){
    
    
            System.out.println("key : " + m.getKey()+" =value:" + m.getValue());
     }
        
           for (Map.Entry m : map.entrySet()){
    
    
            System.out.println("key : " + m.getKey()+"; value:" + m.getValue());
        }

        list.stream().forEach(l ->{
    
    
            System.out.println(l.toString());
        });
}

Print result:

key : beijing; value:com.test.controller.BeijingUserServiceImpl@188c5d23
key : shanghai; value:com.test.controller.ShanghaiServiceImpl@183e329d

com.test.controller.BeijingUserServiceImpl@188c5d23
com.test.controller.ShanghaiServiceImpl@183e329d

Usage scenarios for injecting maps:

Complete the simple version of the strategy pattern, obtain different implementation classes in the map, and call the corresponding method

Guess you like

Origin blog.csdn.net/qq_43842093/article/details/132657619