SSH and SSM learning Spring04spring Property Injection Set method injection a property injection three car class four user class five configuration file Applicationcontextxml Six sample code seven test result eight source address nine IOC reference blog
The set method injection of spring04--spring attribute injection in SSH and SSM learning First, attribute injection
Before we configure the XML by configuring the way, but the property is not assigned, but this is necessary, see how the property values are configured (attribute injection)
Attribute Injection Method
Set Method Injection
Constructor injection
P Name Space Injection
Spel Injection
The above can solve simple injection, such as ordinary value type, reference type injection, but the collection of injection will be a bit inappropriate. Arrays, collections, and properties are injected into the complex type of injection.
Ii. description of Set method injection
The Set method injection, as its name implies, is injected by assigning a value to the object's set method. This approach is the most important one. For example, configuration file injection
<!--==========set method injected ==============-->
<bean name= "user1" class= "Com.qwm.spring1.bean.User" >
<!--value type injection: Assign a value of Wiming to the Name property of the user object, and the Age property is assigned a value of 18. Call the Set method--
<property name= "name" value= "wiming"/>
<property name= "age" value= "
/>" <!--reference type injection: Injects the car object under configuration for car properties--
<property name= "car" ref= "car"/>
</bean>
Identify attributes by tag. which
Name property: Object's property name (name= "age")
Value property: Values of the properties of the object (value= "18")
Ref attribute: Instance of reference type (ref= "car")
The test begins below. The entity classes we use are Car and User. three, Car class
Package Com.qwm.spring1.bean;
/**
* @author: wiming
* @date: 2017-09-27 14:59:50 Wednesday
* @decription: */public
class Car {
private String color;
private String name;
Public String GetColor () {return color;}
public void SetColor (String color) {this.color = color;}
Public String GetName () {return name;}
public void SetName (String name) {this.name = name;}
@Override public String toString () {return ' car{color= ' + color + ' \ ' + ', name= ' + name + ' \ ' + '} ';}
Iv. User Class
Package Com.qwm.spring1.bean;
Import java.io.Serializable;
/** * @author: wiming * @date: 2017-09-25 14:45:56 Monday * @decription: */public class User implements Serializable {
private String name;
private int age;
Private car car;
Public user () {System.out.println ("User instantiated----" + this);
The public User (String name, int age) {this.name = name;
This.age = age;
System.out.println ("Construction Method: User (String name, int age)");
} public User (int name, int age) {this.name = name + "";
This.age = age;
System.out.println ("Construction Method: User (int name, int age)");
Public User (int age, String name) {this.name = name;
This.age = age;
System.out.println ("Construction Method: User (int age, String name)");
} public User (String name, Int. age, Car car) {this.name = name;
This.age = age;
This.car = car; System.out.println ("Construction Method: User (String name, int age, Car CAR) ");
} public void Init () {SYSTEM.OUT.PRINTLN ("---User---init---");}
public void Destory () {System.out.println ("---User---destory---");}
Public String GetName () {return name;}
public void SetName (String name) {this.name = name;}
public int getage () {return age;}
public void Setage (int.) {this.age = age;}
Public Car Getcar () {return car;}
public void Setcar (car car) {This.car = car;} @Override public String toString () {return ' user{name= ' + name + ' \ ' + ', age= "+ Age +", car= "+ Car + '}
';
}
}
v. Configuration file Applicationcontext.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns=" Http://www.springframework.org/schema/beans "xsi:schemalocation=" Http://www.sprin Gframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> <!--======= ===set mode injection ==============--> <bean name= "user1" class= "Com.qwm.spring1.bean.User" > <!--value type injection: for User The image's Name property is assigned a value of wiming, and the Age property is assigned a value of 18.
Call the Set method--<property name= "name" value= "wiming"/> <property name= "age" value= "/>" <!--reference type injection: Injects the car object under configuration for car properties--<property name= "car" ref= "car"/> </bean> <!--car configuration --<bean name= "car" class= "Com.qwm.spring1.bean.Car" > <!--value type injected--<property name= " Name "value=" Lamborghini "/> <property name=" color "value=" red "/> </bean> </beans>
Vi. Sample Code
/**
* Set mode injection Test
*
/@Test public
void Test1 () {
ApplicationContext ac = new Classpathxmlapplicationcontext ("Com/qwm/spring1/c_inject/applicationcontext.xml");
User User1 = (user) Ac.getbean ("user1");
System.out.println (user1);
}
Vii. Test Results
User instantiated----user{name= ' null ', Age=0, car=null}
user{name= ' wiming ', age=18, car=car{color= ' Red ', name= ' Lamborghini '}
Eight, the source address
Https://github.com/wimingxxx/spring01/tree/master/src/com/qwm/spring1/c_inject IX, the IOC reference blog
We know that the main ideas in Spring are IOC and AOP.
Architect's Road (-IOC) framework
Dependency Injection
Spring AOP Detailed