First, Concept introduction
1. One-stop frame: Manage objects in your project. Spring Framework properties are containers (object containers)
2. The core is control inversion (IOC) and aspect-oriented (AOP)
IOC: Invert control--reverses the way the object is created
Create and maintain objects yourself--created, injected, and completed by spring
Inversion control reverses the way the object is created, from its own creation back to the program
DI: Dependency Injection--implementing IOC requires DI for support
Injection mode: Set, construction method, field injection
Injection type: value type (8 large data type), reference type (object) injection
Second, simple implementation
1. Guide Package
First, according to the diagram to find the required jar package, and then import the log package
Project structure after importing package
2. Create an Object bean
User.xml
Package Com.dic.bean;
public class User {
private String name;
Private String age;
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String Getage () {
return age;
}
public void Setage (String age) {
This.age = age;
}
}
User.xml
3. Configuring the core XML file
No specific requirements for name and location, generally named Applicationcontext.xml placed under SRC
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.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-4.2.xsd ">
<!--give the user object to spring--
<!--
Name: Names used when calling
Class: Path
-
<bean name= "user" class= "Com.dic.bean.User" ></bean>
</beans>
Applicationcontext.xml
4. JUnit tests the following code, the console print is not empty and no error is the success
Demo1.java
Package com.dic.text;
Import Org.junit.Test;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Com.dic.bean.User;
public class Demo1 {
@Test
public void Fun1 () {
1 Creating a Container object
ApplicationContext ac = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
2 to Container "want" user object
User U = (user) Ac.getbean ("User");
3 Printing the User object
SYSTEM.OUT.PRINTLN (U);
}
}
Demo1.java
Third, <bean> elements of the comments are written in the code, you can also directly see the fourth part
1. Element interpretation
<?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.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> <!--bean element: Use this element to describe objects that require spring container management -<!--name : The name of the class: path ID: The same as the Name property, but the name is not repeatable and the special character scope property cannot be used : Singleton (default): Singleton object, created when started in spring container, only has one prototype: multiple prototypes, each time it is acquired, creates a new one-- <bean Name = "User" class= "com.dic.bean.user></bean></beans>
2. Modular configuration: The bean will be distributed across multiple files, add a reference to Applicationcontext.xml
<!--Import other spring configuration files--><import resource= "Com/dic/bean/applicationcontext.xml"/>
3. Set injection of attribute injection
is divided into set injection, object injection two kinds, in the XML injected content as follows
<!--set injection-- <bean name= "user1" class= "Com.dic.bean.User1" > <property name= "name" Value= "Tom" ></property> <property name= "age" value= "all" ></property> </bean> <!--object Injection --<!--1-- <bean name= "car" class= "Com.dic.bean.Car" > <property Name= "name" value= "healthy" ></property> <property name= "color" value= "Yellow" ></property> </bean> <!--2-- <bean name= "user2" class= "Com.dic.bean.User2" > < Property name= "Name" value= "Tom" ></property> <property name= "Age" value= "*" ></property> <property name= "car" ref= "car" ></property> </bean>
4. Construction of attribute injection
1. Adding a construction method to User3.java
Public User3 (String name, car car) { System.out.println ("construction mode"); this.name = name; This.car = car;}
2. Add configuration in Applicationcontext.xml note assignment labels are not the same
<!--construction Method injection-- <bean name= "User3" class= "Com.dic.bean.User3" > <constructor-arg name= " Name "value=" Hum "></constructor-arg> <constructor-arg name=" car "ref=" car "></constructor-arg > </bean>
How to construct Note: Specify parameter types when parameters with the same name occur
<bean name= "User3" class= "Com.dic.bean.User3" > <!--a parameter with the same name is used to specify the parameter type-- < Constructor-arg name= "name" value= "one" type= "Java.lang.Integer" ></constructor-arg> < Constructor-arg name= "Car" ref= "car" ></constructor-arg> </bean>
5. Complex Type injection
Complex type injection, such as array, list, map, properties
1 <!--complex Type injection using Set mode-2 3 <bean name= "User4" class= "Com.dic.bean.User4" > 4 <!--array--5 <!--If there is only one value in the array, you can also use name value--6 <property Nam E= "Arr" > 7 <array> 8 <value>tom</value> 9 <value>jerry</value>10 <ref bean= "User2"/>11 & Lt;/array>12 </property> <!--list-->14 <PR Operty name= "List" >15 <list>16 <value>jack</value>17 <value>rose</value>18 <ref bean= "User2"/>19 </list>20 </property> <!--map-->22 < ;p roperty name= "Map" ><map>24 <entry key= "url" value= "Jdbc:mysql:///crm" ></entry >25 <entry key= "user" value-ref= "user" ></entry>26 <ent Ry key-ref= "User3" value-ref= "user2" ></entry>27 </map>28 </prope rty>29 <!--pro-->30 <property name= "Pro" >31 <props& gt;32 <prop key= "Driverclass" >com.jdbc.mysql.driver</prop>33 & Lt;prop key= "UserName" >root</prop>34 <prop key= "password" >root</prop>35 </props>36 </property>37 </bean>
Four, code collation
Package Com.dic.bean;
public class Car {
private String name;
private String color;
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String GetColor () {
return color;
}
public void SetColor (String color) {
This.color = color;
}
}
Car.java
Package Com.dic.bean;
public class User {
private String name;
Private String age;
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String Getage () {
return age;
}
public void Setage (String age) {
This.age = age;
}
}
User.java
Package Com.dic.bean;
public class User1 {
private String name;
Private String age;
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String Getage () {
return age;
}
public void Setage (String age) {
This.age = age;
}
}
User1.java
Package Com.dic.bean;
public class User2 {
private String name;
Private String age;
Private car car;
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String Getage () {
return age;
}
public void Setage (String age) {
This.age = age;
}
Public Car Getcar () {
return car;
}
public void Setcar (car car) {
This.car = car;
}
}
User2.java
Package Com.dic.bean;
public class User3 {
private String name;
Private String age;
Private car car;
Public User3 (String name, car car) {
SYSTEM.OUT.PRINTLN ("construction mode");
THIS.name = name;
This.car = car;
}
The name of the same parameter appears
Public User3 (Integer name, car car) {
SYSTEM.OUT.PRINTLN ("parameter construction method with the same name");
THIS.name = name+ "";
This.car = car;
}
}
User3.java
Package Com.dic.bean;
Import java.util.List;
Import Java.util.Map;
Import java.util.Properties;
public class User4 {
Private object[] arr;
Private list List;
private map map;
Private Properties Pro; is actually a map
Public object[] Getarr () {
return arr;
}
public void Setarr (object[] arr) {
This.arr = arr;
}
Public List getList () {
return list;
}
public void setlist (list list) {
This.list = list;
}
Public Map Getmap () {
return map;
}
public void Setmap (map map) {
This.map = map;
}
Public Properties Getpro () {
return pro;
}
public void Setpro (Properties Pro) {
This.pro = Pro;
}
}
User4.java
Package com.dic.text;
Import Org.junit.Test;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Com.dic.bean.User;
Import Com.dic.bean.User1;
Import Com.dic.bean.User2;
Import Com.dic.bean.User3;
public class Demo1 {
@Test
public void Fun1 () {
1 Creating a Container object
ApplicationContext ac = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
2 to Container "want" user object
User U = (user) Ac.getbean ("User");
3 Printing the User object
SYSTEM.OUT.PRINTLN (U);
User1 u1 = (User1) ac.getbean ("user1");
System.out.println (U1);
User2 U2 = (User2) ac.getbean ("User2");
System.out.println (U2);
User3 U3 = (User3) ac.getbean ("User3");
System.out.println (U3);
}
}
Demo1.java
<?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.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-4.2.xsd ">
<!--bean element: Use this element to describe objects that require spring container management--
<!--
Name: Names used when calling
Class: Path
ID: Same as Name property but cannot be duplicated and cannot use special characters
Scope Property:
Singleton (default): Singleton object, created when started in spring container, only has one
Prototype: Multiple prototypes are created each time they are acquired, creating a new
-
<bean name= "user" class= "Com.dic.bean.User" ></bean>
<!--set injection--
<bean name= "user1" class= "Com.dic.bean.User1" >
<property name= "name" value= "Tom" ></property>
<property name= "Age" value= "></property>"
</bean>
<!--object Injection--
<!--1. -
<bean name= "Car" class= "Com.dic.bean.Car" >
<property name= "name" value= "healthy" ></property>
<property name= "Color" value= "Yellow" ></property>
</bean>
<!--2. -
<bean name= "User2" class= "Com.dic.bean.User2" >
<property name= "name" value= "Tom" ></property>
<property name= "Age" value= "></property>"
<property name= "Car" ref= "car" ></property>
</bean>
<!--construction Method injection--
<bean name= "User3" class= "Com.dic.bean.User3" >
<!--a parameter with the same name can be specified with type--
<constructor-arg name= "name" value= "one" type= "Java.lang.Integer" ></constructor-arg>
<constructor-arg name= "Car" ref= "car" ></constructor-arg>
</bean>
<!--complex type injection using SET mode--
<bean name= "User4" class= "Com.dic.bean.User4" >
<!--array--
<!--If there is only one value in the array, you can use name value--
<property name= "Arr" >
<array>
<value>tom</value>
<value>jerry</value>
<ref bean= "User2"/>
</array>
</property>
<!--List--
<property name= "List" >
<list>
<value>jack</value>
<value>rose</value>
<ref bean= "User2"/>
</list>
</property>
<!--Map--
<property name= "Map" >
<map>
<entry key= "url" value= "Jdbc:mysql:///crm" ></entry>
<entry key= "user" value-ref= "user" ></entry>
<entry key-ref= "User3" value-ref= "User2" ></entry>
</map>
</property>
<!--pro--
<property name= "Pro" >
<props>
<prop key= "Driverclass" >com.jdbc.mysql.Driver</prop>
<prop key= "UserName" >root</prop>
<prop key= "Password" >root</prop>
</props>
</property>
</bean>
<!--to import additional configuration files
<import resource= "Com/dic/bean/applicationcontext.xml"/>
-
</beans>
Applicationcontext.xml
Spring Tutorial (i): simple implementation (GO)