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

Spring Series Textbooks (2) - Injecting Objects

For more, Click to learn:
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 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:

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) { = id;
    public String getName() {
        return name;
    public void setName(String 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=""
    xmlns:xsi="" xmlns:aop=""
    xmlns:tx="" xmlns:context=""
    <bean name="c" class="com.how2java.pojo.Category">
        <property name="name" value="category 1" />
    <bean name="p" class="com.how2java.pojo.Product">
        <property name="name" value="product1" />
        <property name="category" ref="c" />
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 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");

For more, Click to learn:

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