A, set injection method
value: value type injection
ref: the object type injection
sing
the Person class {public
Private String name;
Private Age Integer;
Private Car CAR ;
public the Person () {
Super ();
System.out.println ( "constructor calls");
}
public void the init () {
System.out.println ( "the Person is initialized ...");
}
public void the destroy () {
System.out.println ( "the Person destroyed ...");
}
bean configuration
<bean name="car" class="com.Spring.pojo.Car"> <property name="name" value="BWM"></property> <property name="color" value="red"></property> </bean> <bean name= "person1" class="com.Spring.pojo.Person"> <property name="name" value="helen"></property> <property name="age" value="18"></property> <property name="car" ref="car"></property> </bean>
Second, Constructor injection
There arg constructor of Person 1. Create
the Person public (String name, Car CAR) { Super (); System.out.println ( "construction method called the Person (String name, Car CAR)"); this.name = name; this.car = CAR; }
Configuring bean
<bean name="person2" class="com.Spring.pojo.Person"> <constructor-arg name="name" value="988" index="1" type="java.lang.Integer"></constructor-arg> <constructor-arg name="car" ref="car" index="0"></constructor-arg> </bean>
test
@Test @SuppressWarnings("resource") public void testConstructer() { AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-injection.xml"); Person person2=(Person) context.getBean("person2"); System.out.println(person2); }
2. Using the specified location index parameter
pojo the following two constructors
the Person public ( String name , Car CAR) { Super (); System.out.println ( "construction method called the Person (String name, Car CAR)"); this.name = name; this.car = CAR; } public the Person ( Car CAR , Integer name ) { Super (); System.out.println ( "construction method called the Person (Car CAR, int name)"); this.name = name + ""; this.car = CAR; }
Configuring bean
<bean name="person2" class="com.Spring.pojo.Person"> <constructor-arg name="name" value="988" index="1" type="java.lang.Integer"></constructor-arg> <constructor-arg name="car" ref="car" index="0"></constructor-arg> </bean>
3. Use the type parameter to specify the type of
the Person public (String name, Car CAR ) { Super (); System.out.println ( "construction method called the Person (String name, Car CAR)"); this.name = name; this.car = CAR; } public the Person (Car CAR, Integer name ) { Super (); System.out.println ( "construction method called the Person (Car CAR, int name)"); this.name = name + ""; this.car = CAR; }
bean configuration
<bean name="person2" class="com.Spring.pojo.Person">
<constructor-arg name="name" value="988" index="1" type="java.lang.Integer"></constructor-arg>
<constructor-arg name="car" ref="car" index="0"></constructor-arg>
</bean>
Three, p namespace injection
Import namespace
Typical Case: Configure Data Source
Four, spl injection