【Spring学习08】依赖配置:复合属性

本文博客地址:http://blog.csdn.net/soonfly/article/details/68928534(转载请注明出处)

接着上篇《【Spring学习07】依赖配置:内部对象 》的例子来。其实order的实例化还可以用复合属性来表示:

<bean id="order" class="twm.spring.start.Order">
    <property name="customer.name" value="陈先生" />
    <property name="customer.address" value="深圳南山区" />

    <property name="orderno" value="201799777799"></property>
    <property name="notifyservice" ref="notify2"></property>
</bean>
<bean id="notifyfactory" class="twm.spring.start.NotifyFactory" />
<bean id="notify2" factory-bean="notifyfactory" factory-method="getNotifyService" />

customer.namecustomer.address就叫复合属性。
用复合属性,必须保证在order构造函数执行后,customer不能为null,否则抛异常org.springframework.beans.NullValueInNestedPathException

因此这里要注意的,就是order的无参构造函数中是new了一个对象的(开始我也没有注意,而是报了错后才想起要加上的
Order类:

public class Order {
    /*comstomer和notifyservice都是Order的内部对象*/
    private Customer customer;
    private String orderno;
    private NotifyService notifyservice;

    public Order(){
        //new了一个对象的
        this.customer=new Customer();
    }
    public Order(Customer customer, String orderno, NotifyService notifyservice) {
        super();
        this.customer = customer;
        this.orderno = orderno;
        this.notifyservice = notifyservice;
    }

    //getter and setter methods......

    /*订单支付完成后,系统通知老板*/
    public void PaySuccess(){
        notifyservice.sendMessage("客户"+customer.getName()+"完成订单"+orderno+"付款,共人民币:97.5元");
    }
}

如果删掉this.customer=new Customer();这句,那么就会报错了:
Caused by: org.springframework.beans.NullValueInNestedPathException

本文博客地址:http://blog.csdn.net/soonfly/article/details/68928534(转载请注明出处)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值