tags: spring IOC Dependency injection
What is IOC? IOC is inversion of control. For example, you originally created a new turkey yourself, but now let the control power to spring, which uses dependency injection technology to do it for you.
First prepare a turkey
public class Turkey {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Then prepare a kitchen, the property contains turkey
public class Cookhouse {
private int id;
private String name;
private Turkey turkey;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Turkey getTurkey() {
return turkey;
}
public void setTurkey(Turkey turkey) {
this.turkey = turkey;
}
@Override
public String toString() {
return "Cookhouse{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Give the bean a name, class is the dmo class, property name is the property name, value and ref are the property values,If the attribute is an object, use "ref="
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="t" class="Turkey">
<property name="name" value="usaTurkey"/>
</bean>
<bean name="c" class="Cookhouse">
<property name="name" value="cnCookhouse"/>
<property name="turkey" ref="t"/>
</bean>
</beans>
JAR package and other knowledge are detailed in a project of a great god on the Internet:Self-study website

Let spring help me cook turkey in the kitchen. Spring created a Chinese kitchen based on the core configuration file applicationContext.xml, and then made an American turkey.
public class TestSpring {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
Cookhouse c = (Cookhouse)applicationContext.getBean("c");
System.out.println(c.getName()+"Done only"+c.getTurkey().getName());
}
}
operation result:

Some friends said: turkey cooking is too slow, waiting for hungry
spring said: Then I will use annotation injection to make turkey for you
First optimize the injection method of the object, add applicationContext.xml<context:annotation-config/>, Which tells Spring to use annotations for injection,<property name="turkey" ref="t"/>It can be commented out, we only need to add @Autowired to the attribute Turkey of the Cookhouse class to inject the turkey
@Autowired
private Turkey turkey;
Then optimize the Bean injection method. Bean injection in applicationContext.xml still looks cumbersome. Let’s simplify it and add only:
<context:component-scan base-package="com.fantasy.dmo"/>
Its function is to tell Spring that all beans are placed in this package
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--beans are placed under this package-->
<context:component-scan base-package="com.fantasy.dmo"/>
</beans>
Because Bean has moved to dmo, annotate @Component to Cookhouse and Turkey in dmo, and then name their properties.
@Component("c")
public class Cookhouse {
private int id;
private String name="cnCookhouse";
@Autowired
private Turkey turkey;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Turkey getTurkey() {
return turkey;
}
public void setTurkey(Turkey turkey) {
this.turkey = turkey;
}
@Override
public String toString() {
return "com.fantasy.dmo.Cookhouse{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
@Component("t")
public class Turkey {
private int id;
private String name="usaTurkey";
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
The result of running the program is the same, and a lot of configuration is omitted. The fragrant turkey can be made faster~

BeanpostProcessor handles the Bean object, which means that it has been instantly completed. After this method, we can only know that we can customize Bean's data, such as reset value. Based on this i...
1. Add dependencies 2. UserDao interface 3. UserDao implementation class, divided into 3 java files 4. UserController, you need to inject the place where UserDao 5. Configure file ApplicationContext.x...
IoC (Inverse of Control) is the core of the Spring container, and functions such as AOP and declaring things blossom on this basis. Let's use examples to understand what IoC is. 1. What is IoC The fol...
interface-->proxy object-->springbean First is determined to make an interface to an object, JDK dynamic proxies very appropriate, but the proxy object to spring management how to do it? We will...
Preface: When we first started learning Spring, we came into contact with IoC. As the first core concept of Spring, we must have an in-depth understanding of it before interpreting its source code. Re...
IoC concept Inversion of Control (IoC) is a type of object-oriented programmingDesign Principles, Can be used to reduce the degree of coupling between computer codes. One of the most common methods is...
This article continues to follow the idea of tiny-spring to experience the classic design in the IOC in the Spring framework. The fifth step is to parse the Bean configured in Xml and inject it into...
Non-web application appreciation through Spring Boot Spring IoC First, project background I have built a system and runs this system on the Ali Cloud server. Because I only have a cloud server to run,...
IOC (Inversion of Control) is controlled, which is the core of the Spring framework. Di (Dependency Injection) Dependent is the core of IOC. IoCWhat is controlled in turn? The formation of the c...
Spring integrates JDBC Add dependence Write configuration file DB.Properties Bean.xml configuration modification Configuring data sources Template configuration Test integrated results Case practical ...