Spring實(shí)戰(zhàn)之抽象Bean和子Bean定義與用法示例
本文實(shí)例講述了Spring實(shí)戰(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實(shí)例 -->
<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();
}
三 實(shí)現(xiàn)類
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í)行依賴關(guān)系注入...");
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í)行依賴關(guān)系注入...");
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");
}
}
五 測試結(jié)果
Spring執(zhí)行依賴關(guān)系注入...
Spring執(zhí)行依賴關(guān)系注入...
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
DoytoQuery中關(guān)于N+1查詢問題解決方案詳解
這篇文章主要為大家介紹了DoytoQuery中關(guān)于N+1查詢問題解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
SpringBoot不讀取bootstrap.yml/properties文件問題
這篇文章主要介紹了SpringBoot不讀取bootstrap.yml/properties文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
基于Spring AOP proxyTargetClass的行為表現(xiàn)總結(jié)
這篇文章主要介紹了Spring AOP proxyTargetClass的行為表現(xiàn)總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
Mybatis mapper標(biāo)簽中配置子標(biāo)簽package的坑及解決
這篇文章主要介紹了Mybatis mapper標(biāo)簽中配置子標(biāo)簽package的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Java語言Iterator轉(zhuǎn)換成 List的方法
在 Java 中,迭代器(Iterator)是一種用于遍歷集合中元素的對象,它提供了一種簡單而一致的方式來訪問集合中的元素,而不需要暴露集合內(nèi)部的結(jié)構(gòu),這篇文章主要介紹了Java語言Iterator轉(zhuǎn)換成 List的方法,需要的朋友可以參考下2023-08-08
又又叕出BUG啦!理智分析Java NIO的ByteBuffer到底有多難用
網(wǎng)絡(luò)數(shù)據(jù)的基本單位永遠(yuǎn)是byte,Java NIO提供ByteBuffer作為字節(jié)的容器,但該類過于復(fù)雜,有點(diǎn)難用.本篇文章就帶大家簡單了解一下 ,需要的朋友可以參考下2021-06-06

