Spring實(shí)戰(zhàn)之使用c:命名空間簡(jiǎn)化配置操作示例
本文實(shí)例講述了Spring使用c命名空間簡(jiǎn)化配置操作。分享給大家供大家參考,具體如下:
一 配置
<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 配置chinese實(shí)例,其實(shí)現(xiàn)類是Chinese --> <bean id="chinese" class="org.crazyit.app.service.impl.Chinese" c:axe-ref="steelAxe" c:age="29"/> <!-- 配置chinese實(shí)例,其實(shí)現(xiàn)類是Chinese --> <bean id="chinese2" class="org.crazyit.app.service.impl.Chinese" c:_0-ref="stoneAxe" c:_1="29"/> <!-- 配置stoneAxe實(shí)例,其實(shí)現(xiàn)類是StoneAxe --> <bean id="stoneAxe" class="org.crazyit.app.service.impl.StoneAxe"/> <!-- 配置steelAxe實(shí)例,其實(shí)現(xiàn)類是SteelAxe --> <bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/> </beans>
二 接口
Axe
package org.crazyit.app.service; public interface Axe { // Axe接口里有個(gè)砍的方法 public String chop(); }
Person
package org.crazyit.app.service; public interface Person { // 定義一個(gè)使用斧子的方法 public void useAxe(); }
三 實(shí)現(xiàn)
Chinese
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class Chinese implements Person { private Axe axe; private int age; public Chinese(){ } // axe的setter方法 public void setAxe(Axe axe) { this.axe = axe; } // age的setter方法 public void setAge(int age) { this.age = age; } // 實(shí)現(xiàn)Person接口的useAxe()方法 public void useAxe() { System.out.println(axe.chop()); System.out.println("age成員變量的值:" + age); } }
StoneAxe
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class StoneAxe implements Axe { public String chop() { return "石斧砍柴好慢"; } }
SteelAxe
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class SteelAxe implements Axe { public String chop() { return "鋼斧砍柴真快"; } }
四 測(cè)試類
package lee; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.crazyit.app.service.*; public class BeanTest { public static void main(String[] args)throws Exception { // 創(chuàng)建Spring容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 獲取chinese 實(shí)例 Person p = ctx.getBean("chinese" , Person.class); // 調(diào)用useAxe()方法 p.useAxe(); Person p1 = ctx.getBean("chinese2" , Person.class); // 調(diào)用useAxe()方法 p1.useAxe(); } }
五 運(yùn)行
鋼斧砍柴真快
age成員變量的值:29
石斧砍柴好慢
age成員變量的值:29
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
spring boot環(huán)境抽象的實(shí)現(xiàn)方法
在實(shí)際開發(fā)中,開發(fā)人員在編寫springboot的時(shí)候通常要在本地環(huán)境測(cè)試然后再部署到Production環(huán)境,這兩種環(huán)境一般來(lái)講是不同的,最主要的區(qū)別就是數(shù)據(jù)源的不同。本文主要介紹了這兩種,感興趣的可以了解一下2019-04-04Spring Bean六種作用域的適用場(chǎng)景和使用方式
本文主要介紹了Spring Bean六種作用域的適用場(chǎng)景和使用方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07controller層如何同時(shí)接收兩個(gè)實(shí)體類
這篇文章主要介紹了controller層如何同時(shí)接收兩個(gè)實(shí)體類問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11Spring AOP 實(shí)現(xiàn)自定義注解的示例
這篇文章主要介紹了Spring AOP 實(shí)現(xiàn)自定義注解的示例,幫助大家更好的理解和學(xué)習(xí)使用spring框架,感興趣的朋友可以了解下2021-03-03詳解Spring Kafka中關(guān)于Kafka的配置參數(shù)
這篇文章主要介紹了詳解Spring Kafka中關(guān)于Kafka的配置參數(shù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08使用Java實(shí)現(xiàn)2048小游戲代碼實(shí)例
這篇文章主要介紹了使用Java實(shí)現(xiàn)2048小游戲代碼實(shí)例,2048 游戲是一款益智類游戲,玩家需要通過(guò)合并相同數(shù)字的方塊,不斷合成更大的數(shù)字,最終達(dá)到2048,游戲規(guī)則簡(jiǎn)單,但挑戰(zhàn)性很高,需要玩家靈活運(yùn)用策略和計(jì)算能力,本文將使用Java代碼實(shí)現(xiàn),需要的朋友可以參考下2023-10-10