Spring Dependency Injection

Source: Internet
Author: User

Spring Simple Type Dependency injection is available in 4 ways:1.1 Constructor Injection
<bean name= "Car"class= "Com.roxy.spring.pojo.Car" > <property name= "name" value= "Little Yellow Car" ></property> <property Name= "Color" value= "yellow" ></property> </bean>

<bean name= "Personbyconstructor"class= "Com.roxy.spring.pojo.Person" > <Constructor-arg name= "name" value= "Draco" ></constructor-arg> <Constructor-arg name= "Age" value= "></constructor-arg> <Constructor-arg name= "Car"ref= "Car" ></constructor-arg> </bean>

6.1.1 if the constructor method is the same, but the order of the passed parameters is different, you can determine the position of the parameter by index:
    
Public person (String name, Integer age, Car car) {}
Public person (Integer age, String name, car car) {}
  
<bean name= "PersonByConstructor1" class= "Com.roxy.spring.pojo.Person" >
<constructor-arg name= "name" value= "Harry"index= "1" ></constructor-arg>
<constructor-arg name= "Age" value= " index=" 0 "></constructor-arg>
<constructor-arg name= "Car" ref= "car" index= "2" ></constructor-arg>
</bean>
6.1.2 if the constructor method is the same, but the type of the passed parameter is different, you can use the type to determine the position of the parameter:
    
Public person (Integer age, car car, String name)
Public person (integer age, car car, integer name)

<bean name= "PersonByConstructor2" class= "Com.roxy.spring.pojo.Person" >
<constructor-arg name= "name" value= " type=" Java.lang.Integer "></constructor-arg>
<constructor-arg name= "Age" value= "></constructor-arg>"
<constructor-arg name= "Car" ref= "car" ></constructor-arg>
</bean>
1.2 Setter Method Injection
    class= "Com.roxy.spring.pojo.Person" >        < propertyname= "name" value= "Draco" ></property>        <name= "age" value= "+" ></property>        <ref= "car" ></property>     
1.3 P Namespace Injection
 xmlns:p= "http://www.springframework.org/schema/p"

  <bean name= "Personbyp" class= "Com.roxy.spring.pojo.Person"p:car-ref= "Car" ></bean>

Typical cases:
Configure the data source
<bean id= "myDataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close"
P:driverclassname= "Com.mysql.jdbc.Driver"
P:url= "Jdbc:mysql://localhost:3306/mydb"
P:username= "Root"
p:password= "Masterkaoli"/>
1.4 Spel Expression Injection
    class= "Com.roxy.spring.pojo.Person" >        <property name= "name" value= "#{personbyset.name}" ></ property>        <property name= "age" value= "#{personbyset.age}" ></property>        ref= "Car" ></property>    </bean>  
Dependency injection for spring complex object types:
<bean name= "mycollection"class= "Com.roxy.spring.pojo.MyCollection" > <property name= "arr" > <Array> <value>ios1</value> <value>ios1</value> &LT;/ARRAY&G        T </property> <property name= "Stringset" > <Set> <value>linux2</value> <value>linux2</value> </set > </property> <property name= "Stringlist" > <List> <value>java3</value> <value>java3</value> </list&        Gt </property> <property name= "Stringmap" > <Map> <entry> <key> <value>1</value>                </key> <value>html5</value> </entry> <entry> <key> <value>2</value> <        /key> <value>javascript</value> </entry> </map> </property> <property name= "Stringproperties" > <Props> <prop key= "001" >001</prop> <prop key= "010" >010</prop> </props> </property> </bean>

Spring Dependency Injection

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.