constructor-arg
1、新建Fruit.java
package com.mrw.springtest5;
public class Fruit {
private String name;
private double money;
public Fruit(String name, double money) {
super();
this.name = name;
this.money = money;
}
public String getName() {
return name;
}
public double getMoney() {
return money;
}
public void setName(String name) {
this.name = name;
}
public void setMoney(double money) {
this.money = money;
}
}
2、将Fruit加入到beans.xml中,并为name、money进行初始化
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<bean id="fruit" name="fruit" class="com.mrw.springtest5.Fruit">
<constructor-arg index="0" value="苹果" ></constructor-arg>
<constructor-arg index="1" value="100"></constructor-arg>
</bean>
</beans>
初始化方法有几个的话,进行区分可以在constructor-arg下使用typ进行区分
3、编写Test1.java进行测试
package com.mrw.springtest5;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test1 {
@Test
public void test() {
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
Fruit fruit=(Fruit)context.getBean("fruit");
System.out.println("水果的名称:"+fruit.getName()+" 价格:"+fruit.getMoney());
}
}
4、运行结果:
c
1、与上述constructor-arg一样建立相同的Fruit.java
2、将Fruit加入到beans.xml中,并为name、money进行初始化
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c" <!-- 使用c标签必须加入-->
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<bean id="fruit" name="fruit" class="com.mrw.springtest5.Fruit" c:_1="100" c:_0="苹果"> <!-- 可改为c:money="100" c:name="苹果" -->
</bean>
</beans>
3、与上述constructor-arg一样建立相同的测试类Test1.java
4、运行结果与上述constructor-arg相同
p
1、与上述constructor-arg一样建立相同的Fruit.java
package com.mrw.springtest5;
public class Fruit {
private String name;
private double money;
public Fruit(String name, double money) {
super();
this.name = name;
this.money = money;
}
public String getName() {
return name;
}
public double getMoney() {
return money;
}
public void setName(String name) {
this.name = name;
}
public void setMoney(double money) {
this.money = money;
}
}
2、将Fruit加入到beans.xml中,并为name、money进行初始化
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" <!-- 使用p标签必须加入-->
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<bean id="fruit" name="fruit" class="com.mrw.springtest5.Fruit" p:name="苹果" p:money="100">
</bean>
</beans>
3、编写Test1.java进行测试,Test1.java上述constructor-arg相同
并没有出现预期的效果而是抛出异常:org.springframework.beans.factory.BeanCreationException:
改正方法将Fruit.java改为如下:
package com.mrw.springtest5;
public class Fruit {
private String name;
private double money;
public String getName() {
return name;
}
public double getMoney() {
return money;
}
public void setName(String name) {
this.name = name;
}
public void setMoney(double money) {
this.money = money;
}
}
现在运行结果就上述constructor-arg相同啦