有三种办法,分别是:

  • Normal way
  • Shortcut
  • “p” schema

假设我们现在有这么一个bean:

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

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
	<bean >
		<property name="name">
			<value>mkyong</value>
		</property>
		<property name="type">
			<value>txt</value>
		</property>
	</bean>
</beans>

  

2. Shortcut

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
	<bean >
		<property name="name" value="mkyong" />
		<property name="type" value="txt" />
	</bean>
 
</beans>

  

3. “p” schema

<beans xmlns="http://www.springframework.org/schema/beans"
	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
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
	<bean  
             p:name="mkyong" p:type="txt" />
 
</beans>

  第三种办法需要加入: xmlns:p=”http://www.springframework.org/schema/p

三种办法都很好,具体选哪个,看个人喜好。

相关文章:

  • 2021-10-19
  • 2021-11-10
  • 2022-12-23
  • 2021-07-12
  • 2021-12-05
  • 2021-09-11
  • 2022-12-23
猜你喜欢
  • 2021-09-17
  • 2021-07-08
  • 2021-05-24
  • 2021-09-19
  • 2022-12-23
  • 2021-04-17
  • 2022-01-01
相关资源
相似解决方案