When writing an interface implementation, there are sometimes multiple implementation classes. This article describes specifying the implementation class by passing in a string when it is invoked.
I. Interface and IMPLEMENTATION classes:
Interface public interface Serviceinterface {public void method (); Specific two implementation classes @service ("Aservice") public class Aserviceimpl implements Serviceinterface {@Overridepublic void method () { System.out.println ("The Impl is A"); @Overridepublic String toString () {return "A";}} @Service ("bservice") public class Bserviceimpl implements Serviceinterface {@Overridepublic void method () { System.out.println ("The Impl is B"); @Overridepublic String toString () {return ' B ';}}
The ToString () method is overridden in the implementation class, and the string can be customized, and the corresponding bean can be obtained when the specified string is passed in the invocation.
Two. Register writing:
@Service ("register") public class register implements Initializingbean, Applicationcontextaware {private map<string, serviceinterface> serviceimplmap = new hashmap<> ();p rivate ApplicationContext applicationcontext;//Gets the context of spring @overridepublic void Setapplicationcontext (ApplicationContext ApplicationContext) throws Beansexception {this.applicationcontext = ApplicationContext;} Get all the beans for the interface implementation class and place them in the MAP according to their own rules @overridepublic void Afterpropertiesset () throws Exception {map<string, serviceinterface> Beanmap = Applicationcontext.getbeansoftype (Serviceinterface.class);// The following code puts the bean in the map according to its own rules, where my rule is key:service.toString (); value:bean//is called, the parameter is passed into the service.tostring () The specific string can be obtained to the corresponding bean//here can also do not do the following, directly using Beanmap, when called, the name of the incoming bean for (serviceinterface serviceImpl:beanMap.values ()) { Serviceimplmap.put (Serviceimpl.tostring (), Serviceimpl);}} Public Serviceinterface Getserviceimpl (String name) {return serviceimplmap.get (name);}}
Three. Test class:
@ResourceRegister register; @Testpublic void Testservice () {Serviceinterface service = Register.getserviceimpl ("A"); Service.method (); Serviceinterface Service2 = Register.getserviceimpl ("B"); Service2.method ();}
Run the results,
------------------------------------------------------------------------------------------
Note:
After spring is loaded, get the ApplicationContext method:
RealizeApplicationcontextawareinterface of the Bean, in the process of bean loading can get to spring applicationcontext, this is particularly important, ApplicationContext is the spring application context, A large amount of spring container content and information, including arbitrary beans, can be obtained from ApplicationContext
@Component ("Informerregistry") public final class Informerregistry implements applicationcontextaware{ private ApplicationContext ApplicationContext; @Override public void Setapplicationcontext (ApplicationContext applicationcontext) throws Beansexception { This.applicationcontext = ApplicationContext; }}
Refer to the Spring Common bean extension Interface: http://www.cnblogs.com/xrq730/p/5721366.html
Note: Spring is started when you use the following method to get the spring context. Multiple spring containers are started by writing the following method multiple times
ApplicationContext CTX = new Classpathxmlapplicationcontext ("Classpath:meta-inf/spring/*.xml");
Spring dynamically specifies the specific implementation class