The full name Of IOC refers to the Inverse Of Control. Inversion of control refers to handing over the creation of bean objects to Spring. Before Spring. All by manual code new object ();

1.IOC is inversion of control, which means that the creation of bean objects is managed by Spring. And IOC is a very important feature provided in the Spring framework. 2. The IOC container is a Spring container for managing various bean objects. 3.IOC emphasizes reversing the creation of objects into the IOC container

DI refers to Dependency Injection. Dependency injection. Simply put, you assign a value to an object that depends on another object.

DI emphasizes that IOC container dynamically injects the dependencies of objects into objects. 3.DI is the concrete implementation process of IOC.

Before using Spring, you need to do injection assignments through code.

BookService { BookDao bookDao = new BookDao(); public void setBookDao( BookDao bookDao ) { this.bookDao = bookDao; }}Copy the code

With Spring, you only need to do configuration assignments.

The following 40 experiments are used to learn about the IOC experiment 1: Create objects through the IOC container and assign values to attributes. ★ Experiment 2: Obtain instances of beans from the IOC container according to the type of beans. ★ Experiment 3: Assign values to attributes of beans through constructors. Experiment 6: Assigning values to beans through the P namespace Experiment 7: Testing using null values Experiment 8: Referencing other beans ★ 9: Referencing internal beans Experiment 10: Using set attributes of List type Experiment 11: Using set attributes of Map type Experiment 12: Experiment 13: Create a bean of collection type through the util namespace experiment 14: Assign cascading Properties to beans Experiment 15: Configure beans created using static factory methods Experiment 16: Experiment 17: Configure beans created by instance Factory Methods Experiment 18: Reuse bean configuration information by Inheritance Experiment 19: Create a template Bean by Abstract attribute Experiment 20: Inter-Bean dependencies Experiment 21: Experiment 22: Creating a bean with a lifecycle method experiment 23: Testing a bean with a post-processor experiment 24: Referencing an external properties file ★ Experiment 25: XmL-based autowiring Experiment 26: [SpEL Test I] Using literals in SpEL experiment 27: [SpEL Test II] Referencing another bean in SpEL Experiment 28: [SpEL Test III] Referencing an attribute value of another bean in SpEL Experiment 29: [IV] SpEL test in SpEL calling non-static methods experiment: 30 [V] SpEL test SpEL invokes the static methods experiment 31: [VI] SpEL test using operator in SpEL experiment 32: create a Dao through annotations, respectively, the Service, painted Controller experiment 33: Experiment 36: Use context:include-filter to specify classes to be included when scanning packets. Experiment 34: Use context:exclude-filter to specify classes to be excluded when scanning packets. Experiment 35: Use @autoWired annotation to implement auto-assembly by type. If there is more than one bean of resource type, the default is to look up the bean according to the member variable name of the @AutoWired annotation tag as its ID. If the bean is not found using the member variable name as the ID, you can explicitly specify the id of the target bean using the @qualifier annotation. The required attribute of the @autowired annotation specifies that a property is allowed not to be set. Experiment 40: Testing generic dependency injection ★

4.3,The first IOC sample program – gets objects by ID(key)

Experiment 1: Create objects from the IOC container and assign values to properties ★

Create a Java project:

Import the jar package

From the new menu option, select the following menu to create a Spring configuration file:



Configure the applicationContext.xml configuration file:

<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! -- Bean id="p1" class=" com.pojo.person "> <! -- Property tag can configure the property value of bean object. Name property can configure the property name of bean object. Value property can configure the value of current property. Set id to: 1, name to: name... <property name="id" value="1" /> <property name="name" value=" name" /> <property name="phone" value="18688886666" /> <property name="sex" value="1" /> </bean> </beans>Copy the code

Entity class:

public class Person {

	private int id;
	private String name;
	private String phone;
	private String sex;
Copy the code

The test class:

Class SpringTest {@test public void test1() throws Exception {// A Spring IOC container object is required first. An interface that is said in the spring the spring IOC window object / / ClassPathXmlApplicationContext class representation from the Classpath Classpath to load the XML configuration files to create SpringIOC container object //FileSystemXmlApplicationContext applicationContext = new FileSystemXmlApplicationContext("config/applicationContext.xml"); ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); / / to get an id from Spring IOC container for p1 object Person Person = (Person) applicationContext. GetBean (" p1 "); System.out.println( person ); }}Copy the code

Problem: 1, FileSystemXmlApplicationContext how to do? A: The same relative path as using JavaSE 2. When was the Bean created? 3. If getBean is called multiple times, how many times will it be created? A: The default value is one

Experiment 2: Get an instance of a bean from an IOC container based on its type.

<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! -- Bean id="p1" class=" com.pojo.person "> <! -- Property tag can configure the property value of bean object. Name property can configure the property name of bean object. Value property can configure the value of current property. Set id to: 1, name to: name... <property name="id" value="1" /> <property name="name" value=" name" /> <property name="phone" value="18688886666" /> <property name="sex" value="1" /> </bean> <bean id="p2" class="com.pojo.Person"> <property name="id" value="2" /> <property name="name" value=" what is love?" /> <property name="phone" value="18688886666" /> <property name="sex" value="1" /> </bean> </beans>Copy the code

The test class:

@test public void test2() throws Exception {// First a Spring IOC container object is required. An interface that is said in the spring the spring IOC window object / / ClassPathXmlApplicationContext class representation from the Classpath Classpath to load the XML configuration files to create SpringIOC container object ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); <br/> * <br/> * <br/> * <br/> * <br/> * <br/> */ Person person = (Person) applicationContext.getBean(Person.class); System.out.println( person ); }Copy the code



If you find more than one, you can report an error

Experiment 3: Configure the applicationContext.xml configuration file to assign values to bean properties using the constructor:

<bean id="p3" class="com.pojo.Person"> <! <constructor-arg name="id" value="3" /> <constructor-arg name="name" value=" this is the value assigned by the constructor "/> <constructor-arg name="phone" value="18688886666"/> <constructor-arg name="sex" value="0"/> </bean>Copy the code

Test code:

	@Test
	public void test3() throws Exception {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); 		
		Person person = (Person) applicationContext.getBean("p3");
		System.out.println( person );
	}
Copy the code

The applicationContext.xml configuration file uses the index attribute to specify the position of the parameter.

<bean id="p4" class="com.pojo.Person"> <! /> <constructor-arg index="0" value="4" /> <constructor-arg index="1" Value =" God, please give me a girl!" /> <constructor-arg index="2" value="18616816888" /> <constructor-arg index="3" value="1" /> </bean>Copy the code

The test class:

	@Test
	public void test4() throws Exception {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person = (Person) applicationContext.getBean("p4");
		System.out.println( person );
	}
Copy the code

4.7 IOC example procedure – Injection according to parameter type Experiment 5: Injection according to parameter type

Reload the Person constructor

public Person() { super(); System.out.println(" Person with no arguments "); } public Person(Integer id, String name, String phone, Integer sex) { super(); this.id = id; this.name = name; this.phone = phone; this.sex = sex; System.out.println(" argument constructor Person"); } public Person(Integer id, String name, Integer sex, String phone) { super(); this.id = id; this.name = name; this.phone = phone; this.sex = sex; System.out.println(" argument constructor Person"); }Copy the code

Applicationcontext.xml configuration file:

<bean id="p5" class="com.pojo.Person"> <constructor-arg index="0" type="java.lang.Integer" value="5" /> <constructor-arg <constructor-arg index="2" type="java.lang.String" value="1"/> <constructor-arg index="2" type="java.lang.String" value="1"/> <constructor-arg index="3" type="java.lang.Integer" value="0" /> </bean>Copy the code

The test class:

 @Test
	public void test5() throws Exception {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person = (Person) applicationContext.getBean("p5");
		System.out.println( person );
	}
Copy the code

4.8. P namespace of IOC

Experiment 6: Assign values to beans through the P namespace

Add the p namespace



Configure the applicationContext.xml configuration file:

<! -- p namespace, Is another way to assign attributes --> <bean id="p6" class="com.pojo.Person" p: ID ="6" p:name=" assign via the p namespace "p :phone=" This is the phone of the P namespace "p :sex="1"/>Copy the code

The test class:

	@Test
	public void test6() throws Exception {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person = (Person) applicationContext.getBean("p6");
		System.out.println( person );
	}
Copy the code

The applicationContext. XML configuration file is configured using null values.

<bean id="p7" class="com.pojo.Person"> <property name="id" value="7" /> <property name="name" value=" this is null value test "/> <! -- If I just write a single value="null" it will pass a string, Length 4--> <property name="phone" >< NULL ></ NULL ></ property> <property name="sex" value="1" /> </bean>Copy the code

The test class:

	@Test
	public void test7() throws Exception {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person = (Person) applicationContext.getBean("p7");
		System.out.println( person.getPhone().length() );
	}
Copy the code

A null pointer exception is reported only when null is given

4.10. Assignment experiment of IOC’s child object 8: Reference other bean★ Create entity class

public class Car {

	private String carNo;
	private String name;

public class Person {

	private Integer id;
	private String name;
	private Car car;
Copy the code

The applicationcontext.xml configuration:

<bean id="car01" class="com.pojo.Car"> <property name="carNo" value=" kingb123412 "/> </bean> <bean id="p8" class="com.pojo.Person"> <property name="id" value="8" /> <property name="name" value=" today! / > <! <property name="car" ref="car01" /> </bean>Copy the code

Test code:

@Test
public void test1() throws Exception {
	
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	System.out.println( applicationContext.getBean("p8") );
	
}
Copy the code

4.11. Use experiment of IOC’s internal Bean 9: Reference internal Bean

<bean id="p9" class="com.pojo.Person"> <property name="id" value="9" /> <property name="name" value=" /> <property name="car"> <! <property name="carNo" value=" kingb123411 "/> <property name="name" /> </bean> </property> </bean>Copy the code

Test code:

@Test
public void test2() throws Exception {
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	System.out.println( applicationContext.getBean("p9") );
	System.out.println( applicationContext.getBean("car01") );
	System.out.println( applicationContext.getBean("car02") );
}
Copy the code

Common error: Internal beans cannot be used externally

4.12. Assignment of List Attributes of IOC Experiment 10: Use List child elements to assign values to attributes of List type

public class Person {

private Integer id;
private String name;
private Car car;
private List<String> list;
Copy the code

Configuration information:

<bean id="p10" class="com.pojo.Person"> <property name=" ID "value="10" /> <property name="name" value=" memory gate" /> <property  name="list"> <list> <value>string1</value> <value>string2</value> <value>string3</value> </list> </property> </bean>Copy the code

Test code:

@Test
public void test3() throws Exception {
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	System.out.println( applicationContext.getBean("p10") );
}
Copy the code

4.13. Assignment of Map Attributes of IOC Experiment 11: Assign values to attributes of Map type using Map sub-elements

Person object modification

public class Person {
	private Integer id;
	private String name;
	private Car car;
	private List<String> list;
	private Map<String, Object> map;
Copy the code

Configuration information

<bean id="p11" class="com.pojo.Person"> <property name=" ID "value="11" /> < Property name="name" value= "/> <property name="map"> <map> <entry key=" KEY1 "value ="value1" /> < Entry key=" KEY2 "value ="value2" /> <entry key="key2" value="value2" /> <entry key="key2" value="value2" /> <entry key="key3" value="value3" /> </map> </property> </bean>Copy the code

Test code:

@Test
public void test4() throws Exception {
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	System.out.println( applicationContext.getBean("p11") );
}
Copy the code

4.14. Assignment of Properties Properties of IOC Experiment 12: Assign Properties of type Properties using prop child elements

public class Person {
	private Integer id;
	private String name;
	private Car car;
	private List<String> list;
	private Map<String, Object> map;
	private Properties properties;
Copy the code

Configuration information:

<bean id="p12" class="com.pojo.Person"> <property name=" ID "value="11" /> < Property name="name" value=" Good day, "/> <property name="properties"> <prop key="user">root</prop> <prop key="url">jdbc:mysql://localhost:3306/test</prop> </props> </property> </bean>Copy the code

Test code:

@Test
public void test5() throws Exception {
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	System.out.println( applicationContext.getBean("p12") );
}
Copy the code

The util namespace of IOC allows you to define experiment 13: creating a bean of collection type from the util namespace

Add the Utils namespace:

Configuration information:

<! <util:list id="list01"> <value>string1</value> <value>string2</value> <value>string3</value> </util:list> <bean id="p13" class="com.pojo.Person"> <property name="list" ref="list01" /> </bean>Copy the code

Test code:

@Test
public void test6() throws Exception {
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	System.out.println( applicationContext.getBean("list01") );
	System.out.println( applicationContext.getBean("p13") );
}
Copy the code

4.16. IOC Cascade Attribute Assignment Experiment 14: Assign value to bean cascade attribute

Configuration information:

<bean id="p14" class="com.pojo.Person"> <property name="car" ref="car01" /> <! <property name="car. Name "value=" car. Name" /> </bean>Copy the code

Test code:

@Test
public void test7() throws Exception {
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	System.out.println( applicationContext.getBean("p14") );
}
Copy the code

Common mistake: Cascading properties must first inject objects. Reinject the properties of the object

4.17 IOC static Factory Method for Bean Creation Experiment 15: Configure beans created by static factory method

Factory Code:

Public class PersonFactory {public static Person createPerson() {return new Person(null, "Person created by static factory method ", null); }}Copy the code

Configuration information:

<! <bean id="p15" factory-method="createPerson" <bean id="p15" factory-method="createPerson" class="com.factory.PersonFactory"></bean>Copy the code

Test code:

@Test
public void test7() throws Exception {
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	System.out.println( applicationContext.getBean("p15") );
}
Copy the code

4.18 IOC factory Instance Method to Create beans Experiment 16: Configure beans created by instance factory method

Create a factory

Public class PersonFactory {public Person createPerson2() {return new Person(null, "Person object created by factory instance method ", null); }}Copy the code

Configuration information:

<! - define the person factory - > < bean id = "personFactory" class = "com. Factory. PersonFactory" / > <! <bean id="p16" factory-bean="personFactory" factory-method=" person2 "/> <bean id="p16" factory-bean="personFactory" factory-method="createPerson2" />Copy the code

Test code:

@Test
public void test8() throws Exception {
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	System.out.println( applicationContext.getBean("p16") );
}
Copy the code

4.19. Experiment 17: Configuring the FactoryBean interface to create Bean objects

Public class PersonFactoryBean implements FactoryBean<Person>{/** ** public Person getObject() Throws Exception {return new Person(null, "This is an object created by the FactoryBean interface ", null); } /** * return the type of the object */ @override public Class<? > getObjectType() { return Person.class; } @override public Boolean isSingleton() {return true; }}Copy the code

Configuration information:

<bean id="p17" class="com.factory.PersonFactoryBean" />
Copy the code

Test code:

@Test
public void test9() throws Exception {
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	System.out.println( applicationContext.getBean("p17") );
}
Copy the code

4.20. IOC’s Inheritance Bean Configuration Experiment 18: Reuse Bean configuration information through inheritance

Configuration information:

<! <bean id="parent" class="com.pojo.Person" <property name="id" Value ="100" /> <property name="name" value=" car01" /> </bean> <! Class ="com.pojo.Person" parent="parent"> <property name="id" value="18" /> </bean>Copy the code

Test code:

@Test
public void test10() throws Exception {
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	System.out.println( applicationContext.getBean("p18") );
}
Copy the code

4.21 IOC abstract Bean Experiment 19: Create a template Bean from the abstract property

<! <bean id="parent" class="com.pojo.Person" <property name="id" Value ="100" /> <property name="name" value=" car01" /> </bean>Copy the code

The test class:

@Test
public void test11() throws Exception {
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	System.out.println( applicationContext.getBean("parent") );
}
Copy the code

4.22 IOC component creation Sequence Experiment 20: Bean dependence depends-on attribute

Public class A {public A() {system.out.println (" I am A"); }} public class B {public B() {system.out.println (" I am B"); }} public class C {public C() {system.out.println (" I am C"); }}Copy the code

Configuration:

<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! - depends on setting depend on - - > < bean id = "a" class = "com. Pojo. A" depends - on = "b, c" > < / bean > < bean id = "b" class = "com. Pojo. B" > < / bean > <bean id="c" class="com.pojo.C"></bean> </beans>Copy the code

Test code:

@Test
public void test12() throws Exception {
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
}
Copy the code

4.23. IOC’s Bean singleton and multi-instance (key) Experiment 21: Test the scope of Bean, respectively create single-instance and multi-instance Bean.

<! The scope property sets the bean object's scoped singleton default value. Singleton 1 is created when the Spring IOC container is created. The prototype bean object is not created with the container object. A Person object is created each time the getBean method is called. Request returns the same object each time it is called, as long as it is the same request.  Object bean = request.getAttribute(id); if (bean == null) { bean = new Object(); request.setAttribute( id,bean ); } return bean; Session As long as the current session Object is the same, no matter how many times getBean is called, the same bean Object getBean() is returned. if (bean == null) { bean = new Object(); session.setAttribute( id,bean ); } return bean; --> <bean id="p21" class="com.pojo.Person" scope="prototype"></bean>Copy the code

4.24. Automatic injection based on XML configuration files starts by creating the Person and Car classes

public class Car {

	private String name;

public class Person {

	private Car car;

	public Person(Car car) {
		this.car = car;
	}
Copy the code

Configuration:

<! -- Autowire is automatically injected value Default and no both mean no value is injected. If not found, null value will be injected. ByType means that the springIOC container will search for the object by the type of the attribute object, and then inject 1. Constructor () select * from type where null is found (); Class ="com.pojo.Person" autowire="constructor"> <property name="id" value="24"></property> </bean>Copy the code