Spring實戰(zhàn)之使用c:命名空間簡化配置操作示例
本文實例講述了Spring使用c命名空間簡化配置操作。分享給大家供大家參考,具體如下:
一 配置
<?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實例,其實現(xiàn)類是Chinese -->
<bean id="chinese" class="org.crazyit.app.service.impl.Chinese"
c:axe-ref="steelAxe" c:age="29"/>
<!-- 配置chinese實例,其實現(xiàn)類是Chinese -->
<bean id="chinese2" class="org.crazyit.app.service.impl.Chinese"
c:_0-ref="stoneAxe" c:_1="29"/>
<!-- 配置stoneAxe實例,其實現(xiàn)類是StoneAxe -->
<bean id="stoneAxe" class="org.crazyit.app.service.impl.StoneAxe"/>
<!-- 配置steelAxe實例,其實現(xiàn)類是SteelAxe -->
<bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/>
</beans>
二 接口
Axe
package org.crazyit.app.service;
public interface Axe
{
// Axe接口里有個砍的方法
public String chop();
}
Person
package org.crazyit.app.service;
public interface Person
{
// 定義一個使用斧子的方法
public void useAxe();
}
三 實現(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;
}
// 實現(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 "鋼斧砍柴真快";
}
}
四 測試類
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 實例
Person p = ctx.getBean("chinese" , Person.class);
// 調(diào)用useAxe()方法
p.useAxe();
Person p1 = ctx.getBean("chinese2" , Person.class);
// 調(diào)用useAxe()方法
p1.useAxe();
}
}
五 運行
鋼斧砍柴真快
age成員變量的值:29
石斧砍柴好慢
age成員變量的值:29
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
mybatisplus報Invalid bound statement (not found)錯誤的解決方法
搭建項目時使用了mybatisplus,項目能夠正常啟動,但在調(diào)用mapper方法查詢數(shù)據(jù)庫時報Invalid bound statement (not found)錯誤。本文給大家分享解決方案,感興趣的朋友跟隨小編一起看看吧2020-08-08
Spring Security基于數(shù)據(jù)庫的ABAC屬性權(quán)限模型實戰(zhàn)開發(fā)教程
這篇文章主要介紹了Spring Security基于數(shù)據(jù)庫的ABAC屬性權(quán)限模型實戰(zhàn)開發(fā)教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2025-04-04
Java多線程 ReentrantReadWriteLock原理及實例詳解
這篇文章主要介紹了Java多線程 ReentrantReadWriteLock原理及實例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09
SpringBoot+MyBatis實現(xiàn)登錄案例
前端時間在網(wǎng)上看到有朋友在學(xué)習(xí)springboot項目的搭建過程,今天就抽空給大家分享一個案例幫助大家學(xué)習(xí)SpringBoot+MyBatis實現(xiàn)登錄功能,具體實現(xiàn)代碼跟隨小編一起看看吧2021-06-06

