spring framework learning

What is Spring framework

Spring is a J2EE system framework based on IOC and AOP

IOC Inversion Of Control is the foundation of Spring. Inversion Of Control is simply to say that the creation of objects is called by the previous programmer's own new construction method, which turns into the creation of objects by Spring;

DI dependency injection, which is simply to say that the properties of the obtained object have been injected with relevant values and can be used directly.

Principle analysis:

Compare by getting objects

Traditional way:
Actively create an object through the new keyword.
IOC mode:
The life cycle of an object is managed by Spring, and an object is obtained directly from Spring IOC is reverse control

The abbreviation of (Inversion Of Control) is like that the control is originally in its own hands and handed over to Spring.

Make a metaphor:
Traditional way: it's equivalent to going to the food market and buying a new chicken. It's only raw chicken. You have to pluck the hair, remove the internal organs, and then

Pepper, soy sauce, baking, after a variety of processes, can be eaten.
IOC: it's equivalent to ordering a chicken in a spring restaurant. When it's handed over to you, it's already full of flavors. You just need to

Just eat.

                  

 

 

Next, run TestSpring to show how to get an object with Spring, print its name and use IOC to inject a Category object into the Product object

(the process of creating the project and importing the package has been omitted. About the jar package needed in the spring project, Please click here)

Prepare pojo Category and Product to demonstrate IOC and DI:

package com.spring.pojo;
 
public class 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;
    }
    private int id;
    private String name;
}

 

package com.spring.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;
    }
}

 

Create applicationContext.xml:

Create a new applicationContext.xml file in the src directory;
applicationContext.xml is the core configuration file of Spring. You can get the Product object through the keyword p

(IOC procedure). When the object is acquired, the string "product 1" is injected into the name attribute (DI procedure).

Inject a Category object when creating a Product. Note that ref is used here to inject another object (DI procedure)

<?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.spring.pojo.Category">
        <property name="name" value="category 1" />
    </bean>
   <bean name="p" class="com.how2java.pojo.Product">
       <property name="name" value="product1" />
Use ref to inject category object -- >
       <property name="category" ref="c" />
    </bean>
</beans>

 

TestSpring:
Test the code, and demonstrate how to get the product object through spring, as well as the name and category properties injected into the object. As shown in the figure, you can print
Get the name attribute of the Product object and the name attribute of the Category object obtained through Spring.
          
package com.spring.test;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.spring.pojo.Product;
 
public class TestSpring {
 
    public static void main(String[] args) {
        //Read profile
        ApplicationContext context = new ClassPathXmlApplicationContext(
              new String[] { "applicationContext.xml" });
        //By keyword“ c",from spring Get in Category object
        Product p = (Product) context.getBean("p");
     
//p.getName()Obtain p Properties in object name Value,p.getCategory().getName()Obtain category Of name value System.out.println(p.getName());
     
System.out.println(p.getCategory().getName());
}
}

Note: read configuration file:

//Mode 1: (only one configuration file can be read)
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//Mode 2: (one or more configuration files can be read)
ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });

 

Original address: https://how2j.cn/k/spring/spring-injection/88.html#nowhere

Tags: Java Spring xml Attribute encoding

Posted on Tue, 05 Nov 2019 01:40:46 -0500 by wmguk