首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

spring 抽象bean及沿袭(模板)

2013-08-01 
spring 抽象bean及继承(模板)}}public class Chinese implements Person//面向Axe 接口编程,而不是具体的

spring 抽象bean及继承(模板)
}

}

public class Chinese implements Person

//面向Axe 接口编程,而不是具体的实现类
private Axe axe;
//默认的构造器
public Chinese(){
System.out.println("Spring实例化主调bean: Chinese 实例... ");
}
//设值注入所需的setter 方法
public void setAxe( Axe axe){
System.out.pr工ntln (" Spring 执行依赖关系注入...");
this.axe = axe;
}
//实现Person 接口的useAxe 方法
public void useAxe(){
System.out.println(axe.chop());
}

}

<?xml version="1.0" encoding="gb2312"?>
<!一指定Spring 配置文件的dtd>
<lDOCTYPE beans PUBL工C "-//SPRING//DTD BEAN//EN"
''http://www.springframework.org/dtd/spring-beans.dtd''>
<!一Spring 配置文件的根元素一〉
<beans>
<bean id="steelAxe" abstract="true">
<!一定义依赖注入的属性一〉
<property name="axe">
<ref local="steelAxe"/>
</property>
</bean>
</beans>
从配置文件中可以看出,抽象bean 的定义与普通bean 的定义几乎没有区别,仅仅增加abstract 属性为true ,
但主程序执行结果却有显著的差别。下面的主程序采用AppliactionContext 作为Spring 容器,
. AppliationContext 默认预初始化所有的singleton bean。其主程序部分如下:

public class BeanTest
{

public static void main(String[] args)throws Exception{
ApplicationContext ctx = new FileSysternXmlApplicationContext("bean.xml");
}

}
//主程序部分仅仅实例化了ApplicationContext,在实例化ApplicationContext时,默认实例化singleton bean。
程序执行结果如下:

Spring 实例化依赖bean: SteelAxe 实例.. .

容器并没有实例化chineseTemplate bean ,而忽略了所有声明为abstract 的beano 如果取消abstract 属性定义,
则程序执行结果如下:

Spring 实例化依赖bean: SteelAxe 实~J...
Spring 实例化主调bean: Chinese 实例.. .
Spring 执行依赖关系注入...

可以看出,抽象bean 是一个bean 模板,容器会忽略抽象bean 定义,因而不会实例化抽象bean。
但抽象bean 无须实例化,因此可以没有class 属性。如下的配置文件也有效:
<?xml version="1.0" e口coding="gb2312"?>
<!一指定Spring 配置文件的dtd>
<!DOCTYPE beans PUBLIC "-/!SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd''>
<! -- Spring 配置文件的根元素-->
<beans>
<bean id="steelAxe" abstract="true">
<!… 定义依赖注入的属性一〉
<property name="axe">
<ref local="steelAxe"/>
</property>
</bean〉
</beans>

注意:抽象bean 不能实例化,既不能通过getBean 获得抽象bean,也不能让其他bean
<!一指定Spring 配置文件的dtd>
<lDOCTYPE beans PUBL工C "-//SPRING//DTD BEAN//EN"
''http://www.springframework.org/dtd/spring-beans.dtd''>
<!-- Spring 配置文件的根元素一〉
<beans>
<bean id="steelAxe" abstract="true">
<!-- 定义依赖注入的属性一〉
<property name="axe">
<ref local="steelAxe"/>
</property>
</bean>
<!一通过parent 属性定义子bean ?
<bean id="chinese" parent="chineseTemplate"/>
</beans>
子bean 与普通bean 的定义并没有太大区别,仅仅增加了parent 属性。子bean 可以没有class 属性,
若父bean 定义中有class 属性,则子bean 定义中可省略其class 属性,但子bean 将采用与父bean 相同的实现类。
测试程序修改如下:
public class BeanTest
{

public static void main(String[] args)throws Exception{
ApplicationContext ctx = new FileSysternXmlApplicationContext("bean.xml");
Person p = (Person)ctx.getBean("chinese");
p.useAxe();
}

}
程序执行结果如下:
Spring 实例化依赖bean: Stee1Axe 实例.. .
Spring实例化主调bean: Chinese 实例.. .
spring 执行依赖关系注入...
钢斧砍柴真快

另外,子bean 从父bean 定义继承了实现类并依赖bean 。但子bean 也可覆盖父bean的定义,看如下的配置文件:

//Axe 的实现类StoneAxe如下:
public class StoneAxe implements Axe

//默认构造器
public StoneAxe(){
System.out.println("Spring实例化依赖bean: StoneAxe实例.. .");
}
//实现Axe 接口的chop 方法
public String chop(){
return "石斧砍柴好慢";
}

}

Chinese子类如下:
public class Shanghai extends Chinese {

?? public void show() {
??????????????? System.out.println("子Bean ,中国的上海");
??? }

}


<?xm1 version="1.0" encoding="gb2312"?>
<! 指定Spring 配置文件的dtd>
<lDOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
''http://www.springframework.org/dtd/spring-beans.dtd''>
<! -- Spring 配置文件的根元素一〉
<beans>
<bean id="steelAxe" abstract="true">
<property name="axe">
<ref local="steelAxe"/>
</property>
</bean>
<!一通过parent 属性定义子bean-->
<bean id="shanghai" parent="chineseTemplate">
<!一覆盖父bean 的依赖定义…〉
<property name="axe">
<ref local="stoneAxe"/>
</property>
</bean>
</beans>

此时,子bean 的依赖不再是父bean 定义的依赖了。注意,这个时候的父类lee.Chinese?不能是抽象类,(说明下:有abstract="true")不一定说明这个类一定是个抽象类,不是抽象类同样可以在Spring里定义为抽象Bean,如果你的Class是抽象类,那这个时候就不能用父Bean的Class,一定要在子Bean中定义Class来初始化这个子Bean)
测试程序修改如下:
public class BeanTest
{

public static void main(String[] args)throws Exception{
ApplicationContext ctx = new FileSysternXmlApplicationContext("bean.xml");
Person p = (Person)ctx.getBean("shanghai");
p.useAxe();
}

}

按上面的测试程序执行结果如下:
Spring 实例化依赖bean: SteelAxe 实例.. .
spring 实例化依赖bean: StoneAxe 实例.. .
Spring 实例化主调bean: Chinese 实例.. .
Spring 执行依赖关系注入...
石斧砍柴好慢

注意:上例中的子bean 定义都没有class 属性,因为父bean 定义中已有class 属性,子bean 的class 属性可从父bean 定义中继承,但需要注意的是从父Bean继承Class时,父Bean一定不能是抽象类,因为抽象类不能创建实例;如果父bean 定义中也没有指定class 属性,则子bean 定义中必须指定class 属性,否则会出错;如果父bean 定义指定了class 属性,子bean 定义也指定了class 属性,则子bean 将定义的class 属性覆盖父bean 定义的class属性。

?

?

-------------------------------------------------

---------------------------------------------------

Spring 中bean的继承和Java中的继承截然不同,前者是实例与实例之间的参数的延续,后者是一般到特殊的细化,前者是对象和对象之间的关系,后者是类和类之间的关系。

? a.Spring中的子bean和父bean可以是不同的类型,但是Java中的继承,子类是一种特殊的父类;

? b.Spring中的bean的继承是实例之间的关系,主要表现在参数的延续,而Java中的继承是类与类之间的关系,主要体现在方法和属性的延续。

? c.Spring中子bean不可以作为父bean使用,不具备多态性,Java中的子类实例完全可以当作父类实例使用。