Learn the SPringle framework. IOC is one of the core contents. Today, let's summarize and talk about the understanding of IOC in Spring
1. What is IOC?
Inversion of control is not a technology, but an idea. The learning of ideas is slightly difficult to understand compared with code. IOC is to give your designed object to the container control, rather than the traditional direct control inside your object.
- Who controls who? What is controlled?: The traditional javase is created in the form of a new object. The program actively creates dependent objects, while the IOC has a special container to create these objects, that is, the right container to create objects; Who controls who? It is the IOC container that controls the object and mainly controls the acquisition of external resources
- Why and what aspects are reversed: in traditional applications, we actively control new objects and directly obtain dependent objects, which is a forward rotation; In reverse, the container helps create and inject dependent objects, and the object passively receives dependent objects.
1.1 implementation of traditional business processing code
Dao layer
// Write an interface UserDao public interface UserDao { public void getUser(); } ==================================== /** Implement the above interface */ public class UserDaoImpl implements UserDao { public void getUser(){ System.out.println("UserDao~~~~"); } }
Service layer
// Write a Service interface UserService public interface UserService { public void getUser(); } =============================================== /** Implement the above interface */ public class UserServiceImpl implements UserService { // Importing dao objects private UserDao userDao; public void getUser(){ userDao.getUser(); } }
Test class
public class MyTest { public static void main(String[] args) { UserService userService = new UserServiceImpl(); userService.getUser(); } }
Through this test method, you can pass the test. However, if we add another class in dao, which needs to be a function, what we need to do is
- First, establish a class and implement the interface, and write the method
public class UserMysqlDaoImpl implement UserDao{ public void getMysql(){ system.out.println("MySQL The implementation class executes"); } }
- Call dao layer in business layer
private UserDao = new UserMysqlDaoImpl(); /** This operation needs to be changed after new every time there is a new demand. If there is a large increase, it is very inconvenient */
1.2 using IOC to control inversion (XML)
For the above traditional methods, we use set injection
Modify the code in the service layer
public class UserServiceImpl implements UserService { UserDao userDao; // Add set method injection public void setUserDao(UserDao userDao){ this.userDao = userDao; } public void getUser(){ userDao.getUser(); } }
Test class
UserService userService = new UserServiceImpl(); ((UserServiceImpl)userService).setUserDao(new UserDaoImpl()); userService.getUser();
2. Dependency injection
Present in the most primitive XML way
2.1 injection by constructor
id: bean The unique identifier of, which is equivalent to the object name we learned class = new Object of name: Attribute name value: Attribute value <bean id="user" class="com.kuang.pojo.User"> <constructor-arg name="name" value="AD calcium"></constructor-arg> </bean>
2.2 Set mode injection
Dependency: the creation of bean objects depends on the container
- Complex type
public class Address{ String address; public String getAddress(){ return address; } public void setAddress(String address){ this.address = address; } }
- Real test object
public class Student{ private String name; private Address address; private String books; private List<String> hobby; private Map<String, String> card; private Set<String> games; private String wife; private Properties Info; }
- Perfect injection information
<bean id="address" class="com.kuang.pojo.Address"> <property name="address" value="Xi'an"></property> </bean> <bean id="student" class="com.kuang.pojo.Student"> <!--First, normal value injection--> <property name="name" value="AD calcium"></property> <!--Second, bean Injection, ref--> <property name="address" ref="address"></property> <!--Array injection, ref--> <property name="books"> <array> <value>The Dream of Red Mansion</value> <value>Journey to the West</value> <value>Water Margin</value> <value>Romance of the Three Kingdoms</value> </array> </property> <!--List--> <property name="hobby"> <list> <value>listen to the music</value> <value>having dinner</value> <value>watch movie</value> </list> </property> <!--Map--> <property name="card"> <map> <entry key="ID" value="111111111111" /> <entry key="Student card" value="222222222222"></entry> </map> </property> <!--set--> <property name="games"> <set> <value>LOL</value> <value>COC</value> <value>BOB</value> </set> </property> <!--null--> <property name="wife"> <null></null> </property> <!--properties--> <property name="info"> <props> <prop key="Student number">202202313</prop> <prop key="Gender">male</prop> <prop key="full name">Li Si</prop> </props> </property> </bean>
3. Automatic assembly using annotations
At jdk1 After 5, Spring supports annotations
Notes for use:
Import constraints. congtext constraint
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- Turn on automatic assembly --> <context:annotation-config/> </beans>
automatic assembly
@Autowired: auto assembly type. full name
You can use it directly on the attribute! It can also be used on the set method
@Difference between Autowired and @ Resource
They are used for automatic assembly and can be placed in the attribute field
@Autowired is implemented by byType
@Resource is implemented by byname by default. If the name cannot be found, it can be implemented by byType. If not, an error will be reported
Derived notes:
@Component has several derived annotations. During development, it will be layered according to the three-tier architecture!
dao layer - @ Repository
Service layer - @ service
Controller layer - @ controller
The four annotation functions are the same. They all represent registering a class in Spring and assembling beans
Summary:
We must enable annotation support when using automatic assembly
<context:annotation-config/>
The learning of IOC idea requires the practical operation of code. The idea of control inversion is fully cultivated through various cases. The small editor is also a review of learning. IOC believes that several operations in the project in the future will be like a duck to water for this kind of programming.