Spring injection various

Preparation

Create a class to register in container.

Triangle.java



package com.springLearning;                                                             
                                                                                        
public class Triangle {                                                                 
                                                                          
    public void draw(){                                                                 
        System.out.println("Triangle is here!");                  
    }                                                                                   
                                                                                        
}                                                                                       

Create an Xml file for Application Context (Spring container).

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

    <bean id="Triangle" class="com.springLearning.Triangle">
        
    </bean>

</beans>

Create a container property in main class and connect it with Xml file. In addition, you will receive a Triangle Class bean from the container.

DrawingApplication.java


public static void main(String[] args) {
		//instantiate Spring container and load the xml config
		//Spring container path for Marven:src\main\resources
		ClassPathXmlApplicationContext mContext=
                   new ClassPathXmlApplicationContext("ApplicationContext.xml");

		//get object from Spring container(Application Context)
		//also Triangle mTriangle=(Triangle)mContext.getBean("Triangle");
		Triangle mTriangle=mContext.getBean("Triangle",Triangle.class);
		mTriangle.draw();
	}

As a result, Triangle is here is printed.

injection with setter

For example, if you want to inject a variable called String type from container into Triangle Class First, make a setter.

Triangle.java


public class Triangle {                                                           
    private String type;                                                          
                                                     
    public void setType(String type) {                                          
        this.type = type;                                                         
    }                                                                           
                                       
    public void draw(){                                                           
        System.out.println(type+":Triangle is here!");            
    }                                                                             
                                                                                  
}                                                                                 
                                                                                  

Register the value you want to inject in the container

ApplicationContext.xml


 <bean id="Triangle" class="com.springLearning.Triangle">
        <property name="type" value="triangle"/>
 </bean>

If you execute Main, "Triangle is here!" Will be printed out.

inject with constructor **Only 1 parameter

First, create a constructor in the Triangle class. It's also easy to accept parameters for String type only.

Triangle.java


private String type;     

public Triangle(String type) {            
    this.type = type;                     
}

public void draw(){                                                           
        System.out.println(type+":Triangle is here!");            
    }                                                        

Register in container. When going through the constructor, use constructor arg instead of property. If there is only one parameter, it will be recognized if you write only Value.

ApplicationContext.xml


 <bean id="Triangle" class="com.springLearning.Triangle">
        <constructor-arg value="triangle"/>
 </bean>

If you execute Main, "Triangle is here!" Will be printed out.

**multiple parameter

If the constructor has more than one parameter, Spring just throws the values in order, it doesn't recognize it, so if you don't give it something to tell, you have two options. One is Type = "xxx" (adds data type information) and the other is Name = "xxx" (adds a variable name). I chose Name because I want to write a case where there are two int variables. First is the constructor.

Triangle.java


private String type;    
private int height;     
private int base;       

 public Triangle(String type, int height, int base) {    
     this.type = type;                                   
     this.height = height;                               
     this.base=base;                                     
 } 

public void draw(){                                                     
    System.out.println(type+height+"x"+base+":Triangle is here!");      
}                                                                       

                                                                                       

Registration to container.

ApplicationContext.xml


<bean id="Triangle" class="com.springLearning.Triangle">
       <constructor-arg name="type" value="triangle"/>
        <constructor-arg name="height" value="20"/>
        <constructor-arg name="base" value="100"/>
    </bean>


As a result, "Triangle 20x100: Triangle is here" is printed out.

object injection Inject beans (property) into other classes with setter.

First, create a class that you plan to inject and register it in the container. I want to set the value in container, so I also make a setter. I also made a getter for later printing out in another class.

Point.java


package com.springLearning;

public class Point {
    private int x;
    private int y;
    private int z;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getZ() {
        return z;
    }

    public void setZ(int z) {
        this.z = z;
    }
}

Register + value in container. I want to set 3 sets of values, so give different Ids and refer to the same class.

ApplicationContext.xml


 <bean id="Point0" class="com.springLearning.Point">
        <property name="x" value="0"/>
        <property name="y" value="0"/>
        <property name="z" value="0"/>
 </bean>

 <bean id="Point2" class="com.springLearning.Point">
        <property name="x" value="0"/>
        <property name="y" value="14"/>
        <property name="z" value="18"/>
</bean>

<bean id="Point3" class="com.springLearning.Point">
        <property name="x" value="0"/>
        <property name="y" value="24"/>
        <property name="z" value="28"/>
</bean>

Next, create a variable in Triangle Class that accepts three bean Obj. We also create a setter to accept beans from the Container. The code to finally print out the bean value was made with draw ().

Triangle.java


public class Triangle {
    private Point pointA;
    private Point pointB;
    private Point pointC;

    public void setPointA(Point pointA) {
        this.pointA = pointA;
    }

    public void setPointB(Point pointB) {
        this.pointB = pointB;
    }

    public void setPointC(Point pointC) {
        this.pointC = pointC;
    }

    public void draw(){
        System.out.printf("PointA:%d,%d,%d%n",pointA.getX(),pointA.getY(),pointA.getZ());
        System.out.printf("PointB:%d,%d,%d%n",pointB.getX(),pointB.getY(),pointB.getZ());
        System.out.printf("PointA:%d,%d,%d%n",pointC.getX(),pointC.getY(),pointC.getZ());

    }

}                                               
                                                                                       

Register in container. name is the variable name and ref is bean Id. Then bean obj was injected into a variable of Triangle Class.

ApplicationContext.xml


<bean id="Triangle" class="com.springLearning.Triangle">
        <property name="PointA" ref="Point1"/>
        <property name="PointB" ref="Point2"/>
        <property name="PointC" ref="Point3"/>
    </bean>

After executing the Main Class, it looks like this. image.png

*Extra edition If this bean uses only a specific class, you can write the setting directly in the property tag as an inner bean. For example, if point2 of bean uses only Triangle, the content is directly inserted in the property tag of PointB. You don't have to add new bean Id etc. Of course, in that case, it is not possible to request this bean from another class.

ApplicationContext.xml


<bean id="Triangle" class="com.springLearning.Triangle">
        <property name="PointA" ref="Point0"/>
        <property name="PointB" >
            <bean  class="com.springLearning.Point">
                <property name="x" value="0"/>
                <property name="y" value="14"/>
                <property name="z" value="18"/>
            </bean>
        </property>
        <property name="PointC" ref="Point3"/>
</bean>

The above is a memorandum.

Recommended Posts

Spring injection various
[Java] [Spring] Spring Boot Dependency injection mysterious hamarineta
spring × docker
About Spring Dependency Injection using Java, Kotlin
About Spring ③
FizzBuzz various
Spring Java
SQL injection
Various correspondence table of Spring Framework and Spring Boot