Spring dependency injection method (based on xml configuration)

Keywords: Java Spring xml Maven

Preface

There are three methods of Spring dependency injection:

  1. Property injection (setter method injection)
  2. Constructor injection (different ways of entering parameters)
  3. Factory method injection (static and non static methods)

Spring dependency injection principle: using Java's reflection mechanism

Note: Spring can inject external resources needed for class implementation. Instances mainly inject attributes. All instances can run tests on idea. Some maven engineering structures and configurations are attached at the end of the article

Property injection (set injection)

Property injection condition: there is a default constructor (i.e. no parameter constructor) in the class, and there is a setter method corresponding to the property.
Instance: (generate setter method in the Animal class)

public class Animal {
    private String name;
    private String food;
    public void printAnlimal(){
        System.out.println(name + "eat" + food);
    }
    /*java If there is no constructor in the class, the default nonparametric constructor will be generated automatically, if there is any parameter
    The Java class will not automatically generate the default constructor, so you need to manually create a nonparametric constructor*/
    public void setName(String name) {
        this.name = name;
    }

    public void setFood(String food) {
        this.food = food;
    }
}

Configuration of bean s in xml

<!--bean Of id It can't be the same, otherwise it's defined later bean Will overwrite the previous bean-->
<bean id="cat" class="com.ddpapa.springIoC.Animal">
        <property name="name"><value>cat</value></property>
        <property name="food"><value>Mouse</value></property>
</bean>

Constructor Inject

Attribute values are injected through the class's parameterized construction method.
Example

package com.ddpapa.springIoC;

public class Animal {
    private String name;
    private String food;
    private String animalType;
    public void printAnlimal(){
        System.out.println(name + "eat" + food);
    }
    public Animal(String name, String food) {
    //Parametric construction method
        this.name = name;
        this.food = food;
    }
}

Configuration in xml

<!--1.Parameter type matching-->
    <bean id="cat" class="com.ddpapa.springIoC.Animal">
        <constructor-arg type="java.lang.String">
            <value>cat</value>
        </constructor-arg>
        <constructor-arg type="java.lang.String">
            <value>Mouse</value>
        </constructor-arg>
    </bean>
<!--2.Index matching-->
    <bean id="cat" class="com.ddpapa.springIoC.Animal">
        <constructor-arg index="0">
            <value>cat</value>
        </constructor-arg>
        <constructor-arg index="1">
            <value>Mouse</value>
        </constructor-arg>
    </bean>
<!--3.Type index union match-->
    <bean id="cat" class="com.ddpapa.springIoC.Animal">
        <constructor-arg index="0" type="java.lang.String">
            <value>cat</value>
        </constructor-arg>
        <constructor-arg index="1" type="java.lang.String">
            <value>Mouse</value>
        </constructor-arg>
    </bean>

Plant method injection

  • Non static method

Factory class of Animal

package com.ddpapa.springIoC;

public class AnimalFactory {
    public Animal creatAnimal(){
        //Using construction method to inject attribute of animal
        Animal cat = new Animal("cat","Mouse");
        return cat;
    }
}

Configuration in xml

<! -- the factory method of animal is not static. The factory method can only be used by an example chemical plant;
In xml, you need to configure the bean s of the animalFactory first, and then specify the factory method -- >
<bean id="animalFactory" class="com.ddpapa.springIoC.AnimalFactory"/>
<bean id="cat" factory-bean="animalFactory" factory-method="creatAnimal"/>
  • Static method
    Static factory method of animal
package com.ddpapa.springIoC;

public class AnimalFactory {
    public static Animal creatAnimal(){
        //Using construction method to inject attribute of animal
        Animal cat = new Animal("cat","Mouse");
        return cat;
    }
}

Configuration in xml

<! -- factory bean s do not need to be configured to specify factory classes and factory methods -- >
<bean id="cat" class="com.ddpapa.springIoC.AnimalFactory" factory-method="creatAnimal"/>

Enclosure

  • maven engineering structure

  • The dependency files required by pom.xml to configure spring:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ddpapa</groupId>
    <artifactId>springIoC</artifactId>
    <version>1.0</version>
    <name>SpringIoCAndDI</name>
    <dependencies>

        <!-- spring rely on-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <properties>
        <file.encoding>UTF-8</file.encoding>
        <spring.version>4.2.2.RELEASE</spring.version>
    </properties>
</project>
  • Class Animal:
package com.ddpapa.springIoC;

public class Animal {
    private String name;
    private String food;
    public void printAnlimal(){
        System.out.println(name + "eat" + food);
    }
}
  • Test class:
package com.ddpapa.springIoC;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class IoCtest {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("springIoC.xml");
        Animal cat = ctx.getBean("cat",Animal.class);
        cat.printAnlimal();
    }
}
  • Spring's 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"
        xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                            http://www.springframework.org/schema/context 
                            http://www.springframework.org/schema/context/spring-context-4.0.xsd">
</beans>

Posted by opalelement on Fri, 22 Nov 2019 01:46:02 -0800