Spring combat autowire= "Bytype"

Source: Internet
Author: User
Tags aop

Typically, when we use spring, the attributes injected are the specified class instances or list or array, and for this type of application we usually use spring to look in the container by default based on the bean's ID or name. But sometimes in special applications where one of the bean's properties is not fixed, another injection of spring can be autowire= "Bytype". The following example:

An interface class and several implementation classes Myidprovider.java

Public interface Myidprovider {string getId ();} class MyIdProviderImpl1 implements Myidprovider {public String getId () {return "provider1_" + System.currenttimemillis ();} Class MYIDPROVIDERIMPL2 implements Myidprovider {public String getId () {return "provider2_" + System.currenttimemillis (); Class MyIdProviderImpl3 implements Myidprovider {public String getId () {return "provider3_" + System.currenttimemill Is (); } }

Myidprovidermanager.java a management class that defines some references to Myidprovider

Import java.util.List; Import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.ClassPathXmlApplicationContext; public class Myidprovidermanager {private string name, private list<myidprovider> myidproviders, public string GETN Ame () {return name.} public void SetName (String name) {this.name = name:} public list<myidprovider> Getmyidprovi DERs () {return myidproviders.} public void Setmyidproviders (list<myidprovider> myidproviders) { This.myidproviders = myidproviders; } }

Test.java a test class

Import java.util.List; Import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {public static void main (string[] args) throws Exception {ApplicationContext ctx = new Classpathxmlapp Licationcontext ("/org/garbagecan/springstudy/beansfactory/spring-provider.xml"); Myidprovidermanager manager = (Myidprovidermanager) ctx.getbean ("Myidprovidermanager"); System.out.println (Manager.getname ()); list<myidprovider> myidproviders = Manager.getmyidproviders (); for (Myidprovider myidprovider:myidproviders) {System.out.println (Myidprovider.getid ());}} }

Spring.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" Xmlns:lang = "Http://www.springframework.org/schema/lang" 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 http ://www.springframework.org/schema/aop/spring-aop-2.0.xsd Http://www.springframework.org/schema/lang http:// Www.springframework.org/schema/lang/spring-lang-2.0.xsd "default-lazy-init=" true "> <bean id=" Myidprovidermanager "class=" Myidprovidermanager "autowire=" Bytype "> <property name=" name "><value> hello</value></property> </bean> <bean id= "MyIdProvider1" class= "MyIdProviderImpl1"/> < Bean id= "MyIdProvider2" class= "MyIdProviderImpl2"/> <bean id= "MyIdProvider3" class= "MyIdProviderImpl3"/> </beans> 

Run the test class and get the following output

Hello

provider1_1290559324155

provider2_1290559324155

provider3_1290559324155

For Bytype injected personally think or should be less use, because he did provide enough flexibility, but the sacrifice of the clarity of class dependency, the end may cause problems are difficult to find, so should be used sparingly, only in byname can not solve the problem in the use of caution.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.