Spring constructor-arg、c、p

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相同啦


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值