public class SteelAxe implements Axe
{
//count 是个状态值,每次执行chop 方法该值增加1
private int count = 0;
public SteelAxe(){
System.out.println("Spring实例化依赖bean: SteelAxe 实例.. .");
}
//测试用方法
public String chop(){
return "钢斧砍柴真快" + ++count;
}
}
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" class="lee.SteelAxe"/> <!… 通过abstract 属性定义该bean 是抽象bean--> <bean id="chineseTemplate" class="lee.Chinese" abstract="true"> <!一定义依赖注入的属性一〉 <property name="axe"> <ref local="steelAxe"/> </property> </bean> </beans>
public class BeanTest
{
public static void main(String[] args)throws Exception{
ApplicationContext ctx = new FileSysternXmlApplicationContext("bean.xml");
}
}
//主程序部分仅仅实例化了ApplicationContext,在实例化ApplicationContext时,默认实例化singleton bean。
<?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" class="lee.SteelAxe"/> <!一通过abstract 属性定义该bean 是抽象bean,抽象bean 没有指定class 属性一〉 <bean id="chineseTemplate" abstract="true"> <!… 定义依赖注入的属性一〉 <property name="axe"> <ref local="steelAxe"/> </property> </bean〉 </beans>
<?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" class="lee.SteelAxe"/> <!一通过abstract 属性定义该bean 是抽象bean--> <bean id="chineseTemplate" class="lee.Chinese" abstract="true"> <!-- 定义依赖注入的属性一〉 <property name="axe"> <ref local="steelAxe"/> </property> </bean> <!一通过parent 属性定义子bean ? <bean id="chinese" parent="chineseTemplate"/> </beans>
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();
}
}
//Axe 的实现类StoneAxe如下:
public class StoneAxe implements Axe
//默认构造器
public StoneAxe(){
System.out.println("Spring实例化依赖bean: StoneAxe实例.. .");
}
//实现Axe 接口的chop 方法
public String chop(){
return "石斧砍柴好慢";
}
}
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" class="lee.SteelAxe"/> <bean id="stoneAxe" class="lee.StoneAxe"/> <!一通过abstract 属性定义该bean 是抽象bean--> <bean id="chineseTemplate" class="lee.Chinese" 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>
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();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有