Bean代碼如下
public class Cat implements HelloWorld{
private String name;
private int age;
public Cat(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Cat(){
super();
}
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;
}
@Override
public void say() {
// TODO Auto-generated method stub
System.out.println("name"+this.name+"----"+"age"+this.age);
}
}
1、構造器方式:對於基本數據類型,xml配置中使用type標籤聲明數據類型,可以很好的避免歧義
<bean id="cat" class="bea.Cat">
<constructor-arg>
<value type="java.lang.String">tom</value>
</constructor-arg>
<constructor-arg>
<value type="java.lang.Integer">90</value>
</constructor-arg>
</bean>
2、正常方式
<bean id="cat" class="bea.Cat">
<property name="name">
<value>tom</value>
</property>
<property name="age">
<value>90</value>
</property>
</bean>
3、快捷方式,
<bean id="cat" class="bea.Cat">
<property name="name" value="tom"></property>
<property name="age" value="90"></property>
</bean>
4、p模式,聲明 xmlns:p=”http://www.springframework.org/schema/p" 在Spring XML bean配置文件
<bean id="cat" class="bea.Cat" p:name="tom" p:age="90">
</bean>
四種方式,看自己的喜好吧