【Spring学习07】依赖配置:内部对象
本文博客地址:http://www.52php.cn/article/p-byoifxxv-bps.html(转载请注明出处) 内部对象就是对象的里面还有对象。。。。。好象是废话。 Customer类: public class Customer {
private String name;
private String address;
private int age;
//getter and setter methods
@Override
public String toString() {
return "Person [address=" + address + ",age=" + age + ",name=" + name + "]";
}
}
Order类: public class Order {
/*comstomer和notifyservice都是Order的内部对象*/
private Customer customer;
private String orderno;
private NotifyService notifyservice;
public Order(){
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元");
}
}
然后在beans.xml定义bean对象 <bean id="order" class="twm.spring.start.Order">
<property name="customer">
<bean class="twm.spring.start.Customer">
<property name="name" value="陈先生"></property>
<property name="address" value="深圳南山区"></property>
</bean>
</property>
<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" />
如果用构造注入则是: <bean id="order" class="twm.spring.start.Order">
<constructor-arg name="customer">
<bean class="twm.spring.start.Customer">
<property name="name" value="陈先生"></property>
<property name="address" value="深圳南山区"></property>
</bean>
</constructor-arg>
<constructor-arg name="orderno" value="201799777799"></constructor-arg>
<constructor-arg name="notifyservice" ref="notify2"></constructor-arg>
</bean>
<bean id="notifyfactory" class="twm.spring.start.NotifyFactory" />
<bean id="notify2" factory-bean="notifyfactory" factory-method="getNotifyService" />
setter注入和构造注入中,在 内部bean的定义无需id或name;容器会忽略这些属性,也会忽略scope标记。内部通常是匿名的,伴随着外部类(的创建)而创建 。 本文博客地址:http://www.52php.cn/article/p-byoifxxv-bps.html(转载请注明出处) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |