Sping Structure Architecture 4 Core components
Beans:bean is the bject data that wraps our application custom objects object.
Core:context Some of the tools that are needed to establish and maintain relationships between beans. such as the loading of resources, abstraction of resources and so on.
Context:context is a collection of bean relationships.
expression:spring expression Language.
Construction Injection:
The premises are constructed
Student class
Public classStudent {
@Override
PublicString toString () {
return"student{"+
"Name= '"+name+'\ ''+
", age="+ Age+
", car="+Car+
'} ';
}
PrivateStringname;
private int Age;
Structure
PrivateCarCar;
PublicStudent (String name,intAge, car car) {
This.name= name;
This. Age= age;
This.Car= Car;
}
PublicString GetName () {
returnname;
}
Public voidSetName (String name) {
This.name= name;
}
public intGetage () {
return Age;
}
Public voidSetage (intAge) {
This. Age= age;
}
PublicCar Getcar () {
returnCar;
}
Public voidSetcar (car car) {
This.Car= Car;
}
}
Car class
Public classCar {
PrivateStringBrand;
PrivateStringColor;
@Override
PublicString toString () {
return"car{"+
"Brand= '"+Brand+'\ ''+
", color= '"+Color+'\ ''+
'} ';
}
PublicString Getbrand () {
returnBrand;
}
Public voidSetbrand (String brand) {
This.Brand= Brand;
}
PublicString GetColor () {
returnColor;
}
Public voidSetColor (String color) {
This.Color= color;
}
}
Configuration:
Applicationcontextspring03xmldl.xml
<?XML version= "1.0"encoding= "UTF-8"?>
<Beansxmlns= "Http://www.springframework.org/schema/beans"
xmlns:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xmlns:XSI= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:Context= "Http://www.springframework.org/schema/context"
XSI: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd/http Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
<!--01.dao-->
<BeanID= "Car"class= "Cn.happy.spring03xmldi.Car">
< Propertyname= "Brand"value= "Porsche"></ Property>
< Propertyname= "Color"value= "Blue"></ Property></Bean>
<!--construction injection --
<BeanID= "Stu"class= "Cn.happy.spring03xmldi.Student">
<Constructor-argIndex= "0"value= "Wang Wang"></Constructor-arg>
<Constructor-argIndex= "1"value= "+"></Constructor-arg>
<Constructor-argIndex= "2"ref= "Car"></Constructor-arg></Bean>
</Beans>
Test class:
/Construction Injection
public void test01 () {
ApplicationContext ctx=New classpathxmlapplicationcontext ("Applicationcontextspring03xmldl.xml" );
Student stu= (Student) Ctx.getbean ("Stu");
System. out. println (Stu);
Results :
P Namespace Introduction
xmlns: P = "http://www.springframework.org/schema/p"
<!--P-namespace injection--
<Bean id= "Stu" class= "Cn.happy.spring03xmldi.Student" p : Name = "Egg" P : Age = "+" P : Car-ref = "Car"></Bean>
Spring Construction Injection