Spring Notes--Dependency injection: Several injection methods for different types of variables

Source: Internet
Author: User

Control inversion and dependency injection are all a concept, but stand in different angles, so-called dependency injection:

  Refers to the dynamic injection of dependent objects into a component by an external container at run time. When the spring container is started, the spring container initializes, creates and manages the Bean object, and destroys it. So we just get the bean object directly from the container, instead of writing a code to create the Bean object. This behavior is called inversion of control, where the application itself is not responsible for the creation and maintenance of dependent objects, and the creation and maintenance of dependent objects is the responsibility of the external container. Thus control is transferred from the application to the external container, and the transfer of control is called reversal.

Here's how to inject various types of properties:

New entity class Student:

 Public classStudent {//variables for basic data types    PrivateString name; //object type variable, address class code omitted    Privateaddress address; //variable of array type    Privatestring[] hobby; //variables of list type    PrivateList<string>Books; //variables of type map    PrivateMap<string,string>cards; //variable of set type    PrivateSet<string>movies; //variables of the properties type    PrivateProperties Info;  Public voidShow () {System.out.println ("Name:" +name+ "Address:" +address.getaddr ()); System.out.println ("Hobbies are:");  for(inti=0;i) {System.out.println (hobby[i]); } System.out.println ("Like the book:" +books); System.out.println ("Owned bank card" +cards); System.out.println ("Like the movie" +movies); System.out.println ("Identity information:" +info); }//Set method omitted}

Configuration file Beans.xml:

<?xml version= "1.0" encoding= "UTF-8"?>
<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <BeanID= "Address"class= "Com.wang.entity.Address"> < Propertyname= "Addr"value= "Qingdao"></ Property> </Bean> <BeanID= "Student"class= "Com.wang.entity.Student"> <!--Configure basic data Types - < Propertyname= "Name"value= "Zhang San Feng"></ Property> <!--Configuration Object Type - < Propertyname= "Address"ref= "Address"></ Property> <!--Configuring array Types - < Propertyname= "Hobby" > <Array> <value>Eat</value> <value>Go to bed</value> <value>Hit the peas.</value> </Array> </ Property> <!--Configuring the list type - < Propertyname= "Books"> <List> <value>The wife of the time traveler</value> <value>White Hair Witch Biography</value> <value>Whose youth is not lost</value> </List> </ Property> <!--Configuring the Map type - < Propertyname= "Cards"> <Map> <entryKey= "ABC Card"value= "62284832128534"></entry> <entryKey= "CCB card"value= "62284867867984"></entry> </Map> </ Property> <!--Configuring set Types - < Propertyname= "Movies"> <Set> <value>Crazy Animal City</value> <value>Yeh Q 3</value> <value>Kung Fu Panda</value> </Set> </ Property>
<!--Configure the properties type-
< Propertyname= "Info"> <Props> <propKey= "study number">20124280</prop> <propKey= "Class">Three-year class two</prop> <propKey= "Gender">Unknown</prop> </Props> </ Property> </Bean></Beans>

Test code:

@Test      Public void test1 () {        ApplicationContext context=new classpathxmlapplicationcontext ("Beans.xml");        Student s= (Student) Context.getbean (Student.  Class);        S.show ();    }

Printing results:

Name: Zhang Sanfeng Address: Qingdao
Hobbies are:
Eat
Go to bed
Hit the peas.
Favorite books: [Time Traveler's wife, white hair witch biography, who's youth not confused]
Owned bank card {ABC card = 62284832128534, CCB card = 62284867867984}
Favorite movies [Crazy Animal City, leaf Q 3, Kung Fu Panda]
Identity information: {Study no. = 20124280, gender = unknown, class = three years Second class}

Spring Notes--Dependency injection: Several injection methods for different types of variables

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.