When writing interface implementation, sometimes there are multiple implementation classes. This article introduces specifying a specific implementation class by passing in a string when calling.
1. Interface and implementation classes:
// Interface public interface ServiceInterface { public void method();} // Two specific implementation classes @Service("aService")public class AServiceImpl implements ServiceInterface { @Override public void method() { System.out.println("the impl is A"); } @Override public String toString() { return "A"; }} @Service("bService")public class BServiceImpl implements ServiceInterface { @Override public void method() { System.out.println("the impl is B"); } @Override public String toString() { return "B"; } }The toString() method is rewritten in the implementation class, and you can customize the string. When you pass the specified string when called, you can get the corresponding bean.
2. Register writing:
@Service("register")public class Register implements InitializingBean, ApplicationContextAware { private Map<String, ServiceInterface> serviceImplMap = new HashMap<>(); private ApplicationContext applicationContext; // Get the context of spring @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } // Get all beans of the interface implementation class and put them into the map according to the rules you have set, @Override public void afterPropertiesSet() throws Exception { Map<String, ServiceInterface> beanMap = applicationContext.getBeansOfType(ServiceInterface.class); // The following code is to put the bean into the map according to the rules you have set, and my rule here is key: service.toString(); value:bean // When calling, the specific string of the parameter passed into service.toString() can get the corresponding bean // You can also not do the following operations here, just use beanMap directly. When calling, the name of the bean is passed (ServiceInterface) for (ServiceInterface serviceImpl : beanMap.values()) { serviceImplMap.put(serviceImpl.toString(), serviceImpl); } } public ServiceInterface getServiceImpl(String name) { return serviceImplMap.get(name); } } 3. Testing class:
@ResourceRegister register; @Testpublic void testService() { ServiceInterface service = register.getServiceImpl("A"); service.method(); ServiceInterface service2 = register.getServiceImpl("B"); service2.method();}The operation results are shown in the figure:
Remark:
After spring loading, get the method to applyContext:
Implementing the ApplicationContextAware interface bean, Spring's ApplicationContext can be obtained during the bean loading process. This is particularly important. ApplicationContext is the Spring application context. A large number of Spring container content and information including any bean can be obtained from the ApplicationContext.
@Component("informerRegistry")public final class InformerRegistry implements ApplicationContextAware{ private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; }} For spring's commonly used bean extension interface, please refer to: http://www.cnblogs.com/xrq730/p/5721366.html
Notice:
Spring is started when you use the following method to get the spring context. If you write the following method several times, multiple spring containers will be started to copy the code as follows: ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:META-INF/spring/*.xml");
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.