Spring IOC control inversion

3, Control reversal

By setting up a third-party container to decouple ABCD, they will not be unable to use all because of a certain collapse, and they realize independence;

Inversion of Control (IoC for short), yes object-oriented programming A design principle in that can be used to reduce differences between computer codes Coupling degree . The most common method is called Dependency Injection (DI) and the other is called Dependency Lookup. Through control inversion, when an object is created, the external entities of all objects in a control system pass the references of the objects it depends on to it. In other words, dependencies are injected into objects.

To put it simply, it is to transfer the initiative of this procedure; In the original program, the object obtained by the user is the programmer's class a=new class(); If the user's requirements change, the object will change, but if setter injection is used, this problem will not exist; Users choose what they need, and programmers avoid a lot of code modification;

4, Three ways IOC creates objects (constructor injection)

The default is to use a parameterless constructor, but when a parameterless constructor exists, it is useless

1. The first type: subscript

Properties are objects:

Constructor:

public UserServiceImpl (UserDao userdao) {
        this.userDao=userdao;
    }
<bean id="UserServiceImpl" class="com.saxon.Service.UserServiceImpl">
        <property name="userDao" ref="OralceUserImpl"/>
        <constructor-arg index="0" ref="OralceUserImpl"/>
    </bean>

Property is not an object:

Constructor:

public UserServiceImpl (int a) {
        System.out.println (a);
    }
<bean id="UserServiceImpl" class="com.saxon.Service.UserServiceImpl">
        <property name="userDao" ref="OralceUserImpl"/>
        <constructor-arg index="0" value="0"/>
</bean>

index refers to the subscript of the constructor attribute. If it is not an object, it is directly assigned with value;

2. The second: according to the type of attribute (not recommended)

public UserServiceImpl (int a,String b) {
        System.out.println (a+"&"+b);
}
<bean id="UserServiceImpl" class="com.saxon.Service.UserServiceImpl">
        <property name="userDao" ref="OralceUserImpl"/>
        <constructor-arg type="int" value="0"/>
        <constructor-arg type="java.lang.String" value="1"/>
    </bean>
<bean id="UserServiceImpl" class="com.saxon.Service.UserServiceImpl">
        <property name="userDao" ref="OralceUserImpl"/>
        <constructor-arg type="com.saxon.Dao.UserDao" ref="OralceUserImpl"/>
</bean>

When there are multiple identical data, the assignment is based on your statement order;

Reason for not recommending: when our class name is too complex, it is easy to write the class name wrong; When there are multiple overloads with the same parameter name but different class names, it is impossible to determine which one to use

3. The third type: according to the name of the attribute

public UserServiceImpl (UserDao userDao) {
        this.userDao = userDao;
    }

 <bean id="UserServiceImpl" class="com.saxon.Service.UserServiceImpl">
        <property name="userDao" ref="OralceUserImpl"/>
        <constructor-arg name="userDao" ref="OralceUserImpl"/>
</bean>

If it is an object, use ref instead of value;

All our bean s are instantiated when they are registered, so we can use them directly. Therefore, they will only instantiate one object instead of new one. The result of the following operation is true

ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContent.xml");
UserServiceImpl userServiceImpl = (UserServiceImpl)context.getBean ("UserServiceImpl");
UserServiceImpl userServiceImp2 = (UserServiceImpl)context.getBean ("UserServiceImpl");
System.out.println (userServiceImpl==userServiceImp2);

ApplicationContext is a container. You can take whatever you need;

Self study summary
Study address: Crazy God says Java

Tags: Spring

Posted by mouli on Mon, 23 May 2022 09:15:53 +0300