explain:
(1) Spring IoC container completes [object instantiation] and [dependencies between objects] through configuration, which can solve the shortcomings of traditional coding methods;
(2) In the previous blogs, we introduced [object instantiation]; that is, instantiate a single Bean in the IoC container; then, in the previous blogs, we introduced When [object instantiation], the attributes of these classes are basic value types. Therefore, for example, when instantiating an Apple object, the attributes of these basic value types are not regarded as objects, that is, it is not considered that there is a dependency between objects when instantiating an Apple object;
(3) Next, we will introduce how to set the dependency between objects in the Spring IoC container. This process is called dependency injection. Here, for example, we regard the basic value types in the class as objects;
(4) [object dependency injection] there are two methods: injecting objects based on setter methods and injecting objects based on construction methods; this blog introduces injecting objects based on setter methods;
catalogue
1: Object dependency injection: introducing
1: Object dependency injection: introducing
It can be seen that in order to realize the operation of "children eat apples", the Child class object must depend on the Apple class object; However, based on the concept of IoC container, instead of creating an Apple object in the Child object, the IoC container creates the Child object and Apple object, and then dynamically performs dependency injection; Dependency injection, to put it bluntly, is to associate two objects;
PS: in order to better understand the meaning here, you can refer to it quickly[ Spring IoC container and Bean management 5: using XML to implement spring IoC pre II: Spring IoC initial experience II: IoC container completes [dependency between objects]; ]Description in;)
2: Object dependency injection: here is the first method of object dependency injection [injecting objects based on setter methods]
(1) [object dependency injection] there are two methods: injecting objects based on setter methods and injecting objects based on construction methods;
(2) Injecting objects based on setter method: This is a common method in daily development;
(3) Inject objects based on setter method: there are two usage scenarios;
1. Inject objects based on setter method: the first usage scenario: static value injection: directly assign values to attributes such as String and float of Apple objects;
The most important thing in this scenario is to understand: when assigning values to some basic data type attributes (such as String, float, int), it is also regarded as object dependency injection, because these basic data types have corresponding wrapper classes
(1) A standard example
Create a maven project: s03:
Create two classes for presentation: Apple class and Child class:
Apple class:
package com.imooc.spring.ioc.entity; public class Apple { private String title; //Label and variety private String color; //colour private String origin; //Place of Origin public Apple() { } public Apple(String title, String color, String origin) { this.title = title; this.color = color; this.origin = origin; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getOrigin() { return origin; } public void setOrigin(String origin) { this.origin = origin; } }
explain:
(1) Apple class has nothing to say, just a simple javaBean;
(2) Emphasize that the attributes of the Apple class are private, and then you can assign values to these attributes through the setter method;
Child class:
package com.imooc.spring.ioc.entity; public class Child { private String name; //Child name private Apple apple; //Apples eaten by children public Child() { } public Child(String name, Apple apple) { this.name = name; this.apple = apple; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Apple getApple() { return apple; } public void setApple(Apple apple) { this.apple = apple; } public void eat() { System.out.println(name + "Yes" + apple.getOrigin() + "Planted" + apple.getTitle()); } }
explain:
(1) The Child class has nothing to say, just a simple javaBean;
(2) Emphasize that the properties of the Child class are private, and then you can assign values to these properties through the setter method;
Then, in the pom file, introduce the dependency of Spring Framework;
Then, create a Spring configuration file in the resources directory: ApplicationContext xml:
applicationContext.xml:
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="sweetApple" class="com.imooc.spring.ioc.entity.Apple"> <property name="title" value="Red Fuji"/> <property name="color" value="gules"/> <property name="origin" value="Europe"/> </bean> </beans>
Description:
(1) When using methods such as [< property name = "title" value = "Fuji" / >], the instantiation of the object is completed by calling the corresponding setter method;
Create a program entry class, get the object from the IoC container, and test:
SpringApplication class:
package com.imooc.spring.ioc; import com.imooc.spring.ioc.entity.Apple; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringApplication { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); Apple sweetApple = context.getBean("sweetApple", Apple.class); System.out.println(sweetApple.getTitle()); } }
explain:
(1) To verify that it does assign values to attributes by calling the setter method, output something in the setter method of Apple class:
(2) In the parameterless construction of Apple class, a little content is output to explain that when [using setter to implement object dependency injection] to create an object, it is [first create an object using parameterless construction, and then assign values to object properties using setter method]
(3) Running result: when instantiating an Apple object, the IoC container finds that after [< property name = "title" value = "Fuji" / >], it assigns a value to the property of the current object through the corresponding setter method;
(2) An error situation: for example, add a price attribute to the Apple class, but do not add the setter method of the attribute:
If yes, in ApplicationContext In XML, when assigning a value to the price attribute of an Apple object through [< property name = "price" value = "20.7" / >]:
However, regardless of this error, if it is enforced?
Excerpt of error reporting information:
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'price' of bean class [com.imooc.spring.ioc.entity.Apple]: Bean property 'price' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
The solution is to add the setter method of the price attribute to the Apple class;
(3) Then, it should be noted that IoC container will automatically convert [string that can be forcibly converted to number] to [corresponding number type]
However, in daily development, not all attributes in a class are static values. (naturally, it cannot be denied that static values have corresponding wrapper classes here, which can also be regarded as objects)
(Continued)
2. Object injection based on setter method: the second usage scenario: object injection in a general sense: assigning a value to the Apple attribute of the Child object;
(1) A standard example
In ApplicationContext Configure a Child object in XML:
applicationContext.xml:
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="sweetApple" class="com.imooc.spring.ioc.entity.Apple"> <property name="title" value="Red Fuji"/> <property name="color" value="gules"/> <property name="origin" value="Europe"/> <property name="price" value="20.4"/> </bean> <bean id="lily" class="com.imooc.spring.ioc.entity.Child"> <property name="name" value="Lily"/> <property name="apple" ref="sweetApple"/> </bean> </beans>
Description:
(1) Excuse me, two corresponding instructions
In the program entry class SpringApplication class, get the object from the IoC container and test:
package com.imooc.spring.ioc; import com.imooc.spring.ioc.entity.Apple; import com.imooc.spring.ioc.entity.Child; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringApplication { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); Child lilyChild = context.getBean("lily", Child.class); } }
explain:
(1) In order to better see the object creation process, print something in Child's parameterless structure; print something in Child's setName() method and setApple() method;
(2) Operation results