Spring autowiring mode

Time:2020-10-1

Dependency injection type

Dependency injection type Examples of configuration metadata
Setter method <proeprty name=”user” ref=”userBean”/>
constructor <constructor-arg name=”user” ref=”userBean” />
field @Autowired User user;
method @Autowired public void user(User user) { … }
Interface callback class MyBean implements BeanFactoryAware { … }

Autowiring mode

Automatic binding is mainly used in XML mode

pattern explain
no By default, autowiring is not activated and the dependency injection object needs to be specified manually.
byName The dependency lookup is performed based on the name of the injected attribute as the bean name, and the object is set to the attribute.
byType The type of the injected property is looked up as a dependency type, and the object is set to the property.
constructor Special bytype type for constructor parameters.

Setter method injection (automatic mode)

  • Byname by name
  • Bytype by type

Byname by name

configuration file Beans.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"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">


    <bean id="rumenz" class="com.rumenz.Rumenz">
        <property name="id" value="123"/>
        < property name = "name" value = "entry station" / >
    </bean>
    <bean id="holder" class="com.rumenz.Holder"  autowire="byType">
    </bean>
</beans>

call


package com.rumenz;

public class DemoApplication {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext ca=new ClassPathXmlApplicationContext("Beans.xml");
        ca.refresh();
        Holder bean1 = ca.getBean(Holder.class);
        System.out.println(bean1.getRumenz().getName());
        ca.close();
    }


}

output

Entry station

Bytype by type

configuration file Beans.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"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">


    <bean id="rumenz" class="com.rumenz.Rumenz">
        <property name="id" value="123"/>
        < property name = "name" value = "entry station" / >
    </bean>
    <bean id="holder" class="com.rumenz.Holder"  autowire="byType">
    </bean>
</beans>

call


package com.rumenz;

public class DemoApplication {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext ca=new ClassPathXmlApplicationContext("Beans.xml");
        ca.refresh();
        Holder bean1 = ca.getBean(Holder.class);
        System.out.println(bean1.getRumenz().getName());
        ca.close();
    }


}

output

Entry station

Spring autowiring mode

Original text: https://rumenz.com/rumenbiji/Spring-autowiring-model.html