What do you know about spring's dependency injection?

Keywords: xml Spring Programming less

A previous article: The difference between dependency lookup and dependency injection , this article talks about dependency lookup and dependency injection as the means to implement IOC. We use more dependency injection in our actual work. How much do you know about the specific patterns and types of dependency injection? There are basically three ways to do this online: constructor injection, setter injection, interface injection, or adding an annotation injection. This paper introduces the mode and type of down injection in detail.

There are two kinds of dependency injection modes: manual injection and automatic injection.

Manual mode is the way of configuration or programming. It arranges injection rules in advance, including xml resource configuration meta information, java annotation configuration meta information and api configuration information, and xml resource configuration meta information. It is a common configuration method. Now the popularity of springboot is much less. There is not much nonsense, and the code is more practical: first, a user class is declared as the bean to be injected

/**
*
* User class
**/
public class User{

    private Long id;

    private String name;

   
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
   
}

Then declare a UserHodler as the injected bean.

public class UserHolder {

    private User user;

    public UserHolder() {
    }

    public UserHolder(User user) {
        this.user = user;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }  
}

Next, you can manually inject the User into the userholder object through xml. The code is as follows:

<?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">

    <import resource="classpath:/"/>

    <bean class="*.UserHolder">
        <property name="user" ref="User" />
    </bean>

</beans>

Now, manually inject the User bean into the userholder, but there is a problem. If there are multiple beans to be injected, we need to write multiple < property name = "*" ref = "*" > codes, which will appear to be more codes. Automatic injection mode is a good solution to this problem. The key word of automatic injection mode is: autowire. It provides injection of beans by type, name, constructor, and default mode. The above injection code can be modified as follows:

<?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">

    <import resource="classpath:/"/>

    <bean class="*.UserHolder" autowire="byName">
    </bean>

</beans>

It seems that the code is a lot simpler, and it is also in line with the purpose of our java8 functional programming (try to do more with less code). However, there is a problem. I don't know if you have thought that if the UserHodler code is reconstructed with the development of business, the code is as follows:

public class UserHolder {

    private User myUser;

    public UserHolder() {
    }

    public UserHolder(User myUser) {
        this.myUser = myUser;
    }

    public User getMyUser() {
        return myUser;
    }

    public void setMyUser(User myUser) {
        this.myUser = myUser;
    }  
}

User - > myuser, in this way, the bean will not be injected according to the automatic injection mode, and a null pointer error will be reported. So in the process of using, we should choose a reasonable injection mode according to the actual situation.

In the spring framework, the auto injection mode is off, which they don't recommend now. We try to use it sparingly when we use it. We can only use it when we understand or write the demo.

 

Published 19 original articles, won praise 12, and received 30 thousand visits+
Private letter follow

Posted by DannyM on Sat, 07 Mar 2020 18:40:34 -0800