When spring is used, the injected attributes are specified class instances, list instances, or arrays, for this type of applications, we usually use spring's default bean ID or name to search in the container, however, in some special applications, some attributes of a bean are not fixed. In this case, another injection method of spring is autowire = "bytype ". Example:
One interface class and several implementation classes myidprovider. Java
Public interface myidprovider {<br/> string GETID (); <br/>}< br/> class myidproviderimpl1 implements myidprovider {<br/> Public String GETID () {<br/> return "provider1 _" + system. currenttimemillis (); <br/>}< br/> class myidproviderimpl2 implements myidprovider {<br/> Public String GETID () {<br/> return "provider2 _" + system. currenttimemillis (); <br/>}< br/> class myidproviderimpl3 implements myidprovider {<br/> Public String GETID () {<br/> return "provider3 _" + system. currenttimemillis (); <br/>}< br/>}
Myidprovidermanager. Java is a management class that defines some references to myidprovider.
Import Java. util. list; <br/> Import Org. springframework. context. applicationcontext; <br/> Import Org. springframework. context. support. classpathxmlapplicationcontext; <br/> public class myidprovidermanager {</P> <p> private string name; </P> <p> private list <myidprovider> myidproviders; <br/> Public String getname () {<br/> return name; <br/>}< br/> Public void setname (string name) {<br/> This. name = Name; <br/>}< br/> public list <myidprovider> getmyidproviders () {<br/> return myidproviders; <br/>}< br/> Public void setmyidproviders (list <myidprovider> myidproviders) {<br/> This. myidproviders = myidproviders; <br/>}< br/>
Test. Java is a test class.
Import Java. util. list; <br/> Import Org. springframework. context. applicationcontext; <br/> Import Org. springframework. context. support. classpathxmlapplicationcontext; <br/> public class test {</P> <p> Public static void main (string [] ARGs) throws exception {<br/> applicationcontext CTX = new classpathxmlapplicationcontext ("/org/garbagecan/springstudy/beansfactory/spring-provider.xml"); <br/> myidprovidermanager manager = (myidprovidermanager) CTX. getbean ("myidprovidermanager"); <br/> system. out. println (Manager. getname (); </P> <p> List <myidprovider> myidproviders = manager. getmyidproviders (); <br/> for (myidprovider: myidproviders) {<br/> system. out. println (myidprovider. GETID (); <br/>}< br/>
Spring. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <beans xmlns = "http://www.springframework.org/schema/beans" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xmlns: AOP = "http://www.springframework.org/schema/aop" <br/> xmlns: lang = "http://www.springframework.org/schema/lang" <br/> xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop <br/> http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/lang <br/> http://www.springframework.org/schema/lang/spring-lang-2.0.xsd" <br/> default-Lazy-init = "true"> <br/> <bean id = "myidprovidermanager" class = "myidprovidermanager" autowire = "bytype"> <br/> <property name = "name"> <value> Hello </value> </property> <br /> </bean> <br/> <bean id = "myidprovider1" class = "myidproviderimpl1"/> <br/> <bean id = "myidprovider2" class = "myidproviderimpl2" /> <br/> <bean id = "myidprovider3" class = "myidproviderimpl3"/> <br/> </beans>
Run the test class to obtain the following output:
Hello
Provider00001290559324155
Provider2_1290559324155
Provider3_1290559324155
For bytype injection, I personally think it should be less useful, because it does provide enough flexibility, but it does sacrifice the clarity of class dependency, which may lead to problems that are hard to find, therefore, we should be cautious when using it. We should be cautious when byname cannot solve the problem.