Spring實戰(zhàn)之抽象Bean和子Bean定義與用法示例
本文實例講述了Spring實戰(zhàn)之抽象Bean和子Bean定義與用法。分享給大家供大家參考,具體如下:
一 配置
<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- 定義Axe實例 -->
<bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/>
<!-- 指定abstract="true"定義抽象Bean -->
<bean id="personTemplate" abstract="true">
<property name="name" value="crazyit"/>
<property name="axe" ref="steelAxe"/>
</bean>
<!-- 通過指定parent屬性指定下面Bean配置可從父Bean繼承得到配置信息 -->
<bean id="chinese" class="org.crazyit.app.service.impl.Chinese"
parent="personTemplate"/>
<bean id="american" class="org.crazyit.app.service.impl.American"
parent="personTemplate"/>
</beans>
二 接口
Axe
package org.crazyit.app.service;
public interface Axe
{
public String chop();
}
Person
package org.crazyit.app.service;
public interface Person
{
public void useAxe();
}
三 實現類
1 American
package org.crazyit.app.service.impl;
import org.crazyit.app.service.*;
public class American implements Person
{
private Axe axe;
private String name;
public void setAxe(Axe axe)
{
System.out.println("Spring執(zhí)行依賴關系注入...");
this.axe = axe;
}
public void setName(String name)
{
this.name = name;
}
public void useAxe()
{
System.out.println("我是美國人:名字為:" + name
+ "。正在用斧頭" + axe.chop());
}
}
2 Chinese
package org.crazyit.app.service.impl;
import org.crazyit.app.service.*;
public class Chinese implements Person
{
private Axe axe;
private String name;
public void setAxe(Axe axe)
{
System.out.println("Spring執(zhí)行依賴關系注入...");
this.axe = axe;
}
public void setName(String name)
{
this.name = name;
}
public void useAxe()
{
System.out.println("我是中國人:名字為:" + name
+ "。正在用斧頭" + axe.chop());
}
}
3 SteelAxe
package org.crazyit.app.service.impl;
import org.crazyit.app.service.*;
public class SteelAxe implements Axe
{
public String chop()
{
return "鋼斧砍柴真快";
}
}
4 StoneAxe
package org.crazyit.app.service.impl;
import org.crazyit.app.service.*;
public class StoneAxe implements Axe
{
public String chop()
{
return "石斧砍柴好慢";
}
}
四 測試類
package lee;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.*;
import org.crazyit.app.service.*;
public class BeanTest
{
public static void main(String[] args)
{
ApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
}
}
五 測試結果
Spring執(zhí)行依賴關系注入...
Spring執(zhí)行依賴關系注入...
更多關于java相關內容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數據結構與算法教程》、《Java操作DOM節(jié)點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
相關文章
SpringBoot不讀取bootstrap.yml/properties文件問題
這篇文章主要介紹了SpringBoot不讀取bootstrap.yml/properties文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
基于Spring AOP proxyTargetClass的行為表現總結
這篇文章主要介紹了Spring AOP proxyTargetClass的行為表現總結,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
Mybatis mapper標簽中配置子標簽package的坑及解決
這篇文章主要介紹了Mybatis mapper標簽中配置子標簽package的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
又又叕出BUG啦!理智分析Java NIO的ByteBuffer到底有多難用
網絡數據的基本單位永遠是byte,Java NIO提供ByteBuffer作為字節(jié)的容器,但該類過于復雜,有點難用.本篇文章就帶大家簡單了解一下 ,需要的朋友可以參考下2021-06-06

