Basic java framework tutorial-------spring injection object

Spring Series Textbooks (2) - Injecting Objects

For more, Click to learn: https://how2j.cn/k/spring/spring-injection/88.html
In the example above, a "category 1" string is injected into the name property of Category
In this example, a Category object is injected into the Product object
Step 1: Run first, see the effect, then learn
Step 2: Imitate and troubleshoot
Step 3:Product.java
Step 4:applicationContext.xml
Step 5:TestSpring

Step 1: Run first, see the effect, then learn

Old rule, download first Download area (click to enter) Runnable projects, configurations run, confirm availability, and then learn what steps to take to achieve this effect.

Step 2: Imitate and troubleshoot

After ensuring that the runnable project runs correctly, follow the steps of the tutorial closely and emulate the code once again.
Imitate process unavoidable code inconsistencies, resulting in unexpected run results, at this time by comparing the correct answers (runnable projects) and your own code to locate the problem.
In this way, the learning effect and the error-solving efficiency can significantly increase the learning speed and cross the various thresholds on the learning road.

It is recommended to use diffmerge software for folder comparison.Compare your own project folder with my runnable project folder.
This software is awesome, knows which two files in the folder are wrong, and clearly marks them up
A green installation and use tutorial is provided here: diffmerge download and use tutorials

Step 3: Product.java

setter getter for Category object in Product class

package com.how2java.pojo;
 
public class Product {
 
    private int id;
    private String name;
    private Category category;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Category getCategory() {
        return category;
    }
    public void setCategory(Category category) {
        this.category = category;
    }
}
Step 4: applicationContext.xml

Inject a Category object when creating a Product
Note that ref is used here to inject another object

<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
    <bean name="c" class="com.how2java.pojo.Category">
        <property name="name" value="category 1" />
    </bean>
    <bean name="p" class="com.how2java.pojo.Product">
        <property name="name" value="product1" />
        <property name="category" ref="c" />
    </bean>
 
</beans>
Step 5: TestSpring

The Product object obtained through Spring has been injected into the Category object

package com.how2java.test;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.how2java.pojo.Product;
 
public class TestSpring {
 
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
 
        Product p = (Product) context.getBean("p");
 
        System.out.println(p.getName());
        System.out.println(p.getCategory().getName());
    }
}

For more, Click to learn: https://how2j.cn/k/spring/spring-injection/88.html

65 original articles published, 4 praised, 3424 visited
Private letter follow

Tags: Spring xml Java encoding

Posted on Sat, 14 Mar 2020 22:58:57 -0400 by kecebong_soft