有三种办法,分别是:
- Normal way
- Shortcut
- “p” schema
假设我们现在有这么一个bean:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public
class
FileNameGenerator
{
private
String name;
private
String type;
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
String getType() {
return
type;
}
public
void
setType(String type) {
this
.type = type;
}
}
|
1. Normal way
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
<
bean
id="FileNameGenerator" class="com.mkyong.common.FileNameGenerator">
<
property
name="name">
<
value
>mkyong</
value
>
</
property
>
<
property
name="type">
<
value
>txt</
value
>
</
property
>
</
bean
>
</
beans
>
|
2. Shortcut
1
2
3
4
5
6
7
8
9
10
11
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
<
bean
id="FileNameGenerator" class="com.mkyong.common.FileNameGenerator">
<
property
name="name" value="mkyong" />
<
property
name="type" value="txt" />
</
bean
>
</
beans
>
|
3. “p” schema
1
2
3
4
5
6
7
8
9
10
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
<
bean
id="FileNameGenerator" class="com.mkyong.common.FileNameGenerator"
p:name="mkyong" p:type="txt" />
</
beans
>
|
第三种办法需要加入: xmlns:p=”http://www.springframework.org/schema/p
三种办法都很好,具体选哪个,看个人喜好。