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
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 (个_个))
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;
}
}
Write the
output of the test class to see the result.
It should be noted that the name of the attribute after the p tag is not the name of the setter parameter! ! ! !
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"
We make some modifications to the parameterized structure of the HelloDemo class above and modify
it to the following beans.xml code
output results.
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
c-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
output results are as follows .
There are so many uses of p-namespace and c-namespace . Slowly absorb O(∩_∩)O haha~