普通注入
public interface TestService {
}
@Service
public class TestServiceImplA implements TestService {
}
@Service
public class TestServiceImplB implements TestService {
}
@Service
public class TestServiceImplC implements TestService {
}
@SpringBootApplication
public class ApacheCommonsApplication implements CommandLineRunner {
@Autowired
TestServiceImplA service;
@Override
public void run(String... args) {
System.out.println(service);
}
public static void main(String[] args) {
SpringApplication.run(ApacheCommonsApplication.class, args);
}
}
列表注入
// 同上
@SpringBootApplication
public class ApacheCommonsApplication implements CommandLineRunner {
@Autowired
List<TestService> testServices;
@Override
public void run(String... args) {
testServices.forEach(System.out::println);
}
public static void main(String[] args) {
SpringApplication.run(ApacheCommonsApplication.class, args);
}
}
Map 注入
// 同上
@SpringBootApplication
public class ApacheCommonsApplication implements CommandLineRunner {
@Autowired
Map<String, TestService> testServices;
@Override
public void run(String... args) {
testServices.forEach((key, value) -> System.out.println("key: " + key + " value: " + value));
}
public static void main(String[] args) {
SpringApplication.run(ApacheCommonsApplication.class, args);
}
}