未知数据

简,是一种境界.

首页 新随笔 联系 聚合 管理
  8 Posts :: 0 Stories :: 2 Comments :: 0 Trackbacks

使用模板装配

如果需要装配大量的同一类型的Bean,而这些Bean都有一些相同的属性,可以使用模板来装配Bean.

package example.chapter3;
import java.io.Serializable;
public class Employee implements Serializable {
private String name;
private String title;
private String company;
private String department;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public String getCompany() { return company; }
public void setCompany(String company) { this.company = company; }
public String getDepartment() { return department; }
public void setDepartment(String department) { this.department = department; }
}

xml配置文件

<bean id="abstractEmployee" class="java.io.Serializable" abstract="true">
<property name="company" value="Inc." />
<property name="department" value="Center" />
</bean>
<bean id="engineer1" class="example.chapter3.Employee" parent="abstractEmployee">
<property name="name" value="Bill" />
<property name="title" value="Software Engineer" />
</bean>
<bean id="engineer2" class="example.chapter3.Employee" parent="abstractEmployee">
<property name="name" value="Steven" />
<property name="title" value="Hardware Engineer" />
</bean>
<bean id="manager" class="example.chapter3.Employee" parent="abstractEmployee">
<property name="name" value="Michael" />
<property name="title" value="Manager" />
<property name="department" value="HR" />
</bean>
1.指定abstract="true"表示这个Bean只作为模板使用,Spring容器不会对标记为abstract的Bean创建实例,也不会检查其属性是否合法.因此可以随意注入任何属性
2.下面创建的engineer1,engineer2,engineer3指定parent="abstractEmployee,将自动继承abstractEmployee的company和department属性.同时子类也可以覆盖模板中的属性,比如engineer3

posted on 2008-02-29 16:02 未知数据 阅读(286) 评论(0)  编辑  收藏 所属分类: Spring

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问