Spring notes 5--p-namespace and c-namespace usage

If you haven’t watched the previous article, I suggest you take a look at
Spring Note 4-Constructing methods to implement beans and discussing the life cycle of bean objects.
Let me say something before I talk.

Laziness is the first productive force of mankind, it promotes the progress and development of science and technology (doge)

Why talk about Spring's p-namespace and c-namespace

  • For simple projects or codes, it can be simplified
  • Strengthen the learning of new knowledge of Spring
  • Just study, where is so much and why!

One, p-namespace

①It is also called p-namespace ,
②What does it mean?
First of all, let’s talk about it this way. In the past, when we wrote beans, there were two ways to inject values ​​into the properties of an object. One is the constructor-arg, and the other is through (the property tag is the specified property injection value).
③And p is the first letter of property , so you should be able to guess what it is. ④It is
purely for simplification (personally think)
⑤Let’s learn from examples directly.
First, you need to use the p namespace Write some support configuration information in the corresponding xml file
Insert picture description here

xmlns:p="http://www.springframework.org/schema/p"

The project structure is set up, or the notes in the previous section (the portal is in the blog post (个_个))
Insert picture description here

package com.ysj.study;

@SuppressWarnings("ALL")
public class HelloDemo1 {
    
    

    private String name;
    private int age;

    public HelloDemo1(){
    
    
        System.out.println("HelloDemo1的 无 参构造");
    }

    public HelloDemo1(String name) {
    
    
        System.out.println("HelloDemo1的 有 参构造");
        this.name = name;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public int getAge() {
    
    
        return age;
    }

    public void setAge(int age) {
    
    
        this.age = age;
    }
}

Insert picture description here
Write the
Insert picture description here
output of the test class to see the result.
Insert picture description here
It should be noted that the name of the attribute after the p tag is not the name of the setter parameter! ! ! !
Insert picture description here

Two, c-namespace

①It is also called c-namespace ,
②What does it mean?
It is used to inject the initial property value into the object through the parameterized construction
③ and c is the first letter of constructor-arg , so you should also be able to guess what it is doing.
④ It is purely for simplification (personally think)
⑤ Let's go directly to the example to learn.
First, before using the c namespace, you need to write some support configuration information in the corresponding xml file.

xmlns:c="http://www.springframework.org/schema/c"

Insert picture description here
We make some modifications to the parameterized structure of the HelloDemo class above and modify
Insert picture description here
it to the following beans.xml code
Insert picture description here
output results.
Insert picture description here
We remove all the setter methods and find that it can still run, so the usage of c-namespace and constructor-ag r are exactly the same
need only pay attention to the following legend can
Insert picture description herec-namespace expansion:
in fact, c-namespace in addition to the injection by the value of the parameter name, you can also rely on the value injected by the parameter index. The
Insert picture description here
output results are as follows .
Insert picture description here
There are so many uses of p-namespace and c-namespace . Slowly absorb O(∩_∩)O haha~

Guess you like

Origin blog.csdn.net/YSJ367635984/article/details/113178863