Detailed explanation of four injection modes of Spring bean

  • 2021-10-27 07:44:21
  • OfStack

Directory 1. Set injection pojo layer: 1. xml file test test 2. Constructor injection pojo layer 2. xml file test test 3. Annotation injection pojo layer 3. xml file test test 4. JavaConfig injection pojo layer JavaConfig class xml file scan package test: 5. Service layer injection detailed explanation serviceserviceImplxml configuration file summary

1. Set mode injection

pojo Layer:


/**
 * @Author: crush
 * @Date: 2021-06-17 16:57
 * version 1.0
 * xml  Configure injection version   set  Mode 
 */
public class Student1 {
    public String name;
    public String school;
    public void setName(String name) {
        this.name = name;
    }
    public void setSchool(String school) {
        this.school = school;
    }
    @Override
    public String toString() {
        return "Student1{" +
                "name='" + name + '\'' +
                ", school='" + school + '\'' +
                '}';
    }
}

1. xml file


<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--set Mode injection 
        id Is injected bean The name in 
        class  Is a fully qualified class name 
        property  Is in accordance with set Mode injection 
    -->
    <bean id="student1" class="com.crush.pojo.Student1">
        <property name="name" value="wyh1"/>
        <property name="school" value="hngy1"/>
    </bean>
</beans>

test test


 @Test
    public void student1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("student1.xml");
        Student1 student1 = context.getBean("student1", Student1.class);
        System.out.println(student1);
    }

2. Constructor mode injection

pojo layer


/**
 * @Author: crush
 * @Date: 2021-06-17 17:02
 * version 1.0
 * xml  Configure   Constructor mode injection 
 */
public class Student2 {
    private String name;
    private String school;
    public Student2(String name, String school) {
        this.name = name;
        this.school = school;
    }
    @Override
    public String toString() {
        return "Student2{" +
                "name='" + name + '\'' +
                ", school='" + school + '\'' +
                '}';
    }
}

2. xml file


<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--set Mode injection 
        id Is injected bean The name in 
        class  Is a fully qualified class name 
        constructor  Is injected according to the construction mode 
        index  Is based on the number of arguments of the member variable in the constructor 
        name  Represents the member variable name 
        type  Representation type 
        value  Representation value 
        ref  Represents a reference   Additional references can be made 1 Injected into Spring Value in 
    -->
    <bean id="student2" class="com.crush.pojo.Student2">
        <constructor-arg index="0"  name="name" type="java.lang.String" value="wyh2"/>
        <constructor-arg name="school" value="hngy2"/>
    </bean>
</beans>

test test


   @Test
    public void student2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("student2.xml");
        Student2 student2 = context.getBean("student2", Student2.class);
        System.out.println(student2);
    }

3. Annotation injection

pojo Layer


/**
 * @Author: crush
 * @Date: 2021-06-17 17:08
 * version 1.0
 */
@Component
public class Student3 {
    @Value("wyh3")
    private String name;
    @Value("hngy3")
    private String school;
    @Override
    public String toString() {
        return "Student3{" +
                "name='" + name + '\'' +
                ", school='" + school + '\'' +
                '}';
    }
}

3. xml file


<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Annotation mode injection 
         Packages that need to be scanned for annotations   The annotation will take effect 
    -->
    <context:component-scan base-package="com.crush.pojo"/>
</beans>

test Test


   @Test
    public void student3(){
        ApplicationContext context = new ClassPathXmlApplicationContext("student3.xml");
        Student3 student3 = context.getBean("student3", Student3.class);
        System.out.println(student3);
    }

4. JavaConfig mode injection

pojo layer


/**
 * @Author: crush
 * @Date: 2021-06-17 17:16
 * version 1.0
 * JavaConfig  Configure 
 */
public class Student4 {
    @Value("wyh4")
    private String name;
    @Value("hngy4")
    private String school;
    @Override
    public String toString() {
        return "Student4{" +
                "name='" + name + '\'' +
                ", school='" + school + '\'' +
                '}';
    }
}

JavaConfig class


<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--set Mode injection 
        id Is injected bean The name in 
        class  Is a fully qualified class name 
        property  Is in accordance with set Mode injection 
    -->
    <bean id="student1" class="com.crush.pojo.Student1">
        <property name="name" value="wyh1"/>
        <property name="school" value="hngy1"/>
    </bean>
</beans>
0

xml file scan 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--set Mode injection 
        id Is injected bean The name in 
        class  Is a fully qualified class name 
        property  Is in accordance with set Mode injection 
    -->
    <bean id="student1" class="com.crush.pojo.Student1">
        <property name="name" value="wyh1"/>
        <property name="school" value="hngy1"/>
    </bean>
</beans>
1

Test:


<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--set Mode injection 
        id Is injected bean The name in 
        class  Is a fully qualified class name 
        property  Is in accordance with set Mode injection 
    -->
    <bean id="student1" class="com.crush.pojo.Student1">
        <property name="name" value="wyh1"/>
        <property name="school" value="hngy1"/>
    </bean>
</beans>
2

5. Detailed explanation of Service layer injection

service


/**
 * @Author: crush
 * @Date: 2021-06-17 17:27
 * version 1.0
 * xml  Configure 
 */
public interface StudentService1 {
    void test();
}

serviceImpl


<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--set Mode injection 
        id Is injected bean The name in 
        class  Is a fully qualified class name 
        property  Is in accordance with set Mode injection 
    -->
    <bean id="student1" class="com.crush.pojo.Student1">
        <property name="name" value="wyh1"/>
        <property name="school" value="hngy1"/>
    </bean>
</beans>
4

xml Configuration File


<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--set Mode injection 
        id Is injected bean The name in 
        class  Is a fully qualified class name 
        property  Is in accordance with set Mode injection 
    -->
    <bean id="student1" class="com.crush.pojo.Student1">
        <property name="name" value="wyh1"/>
        <property name="school" value="hngy1"/>
    </bean>
</beans>
5

Summarize

That's all for this article. I hope I can help you, and I hope you can pay attention to more contents of this site!


Related articles: