Spring框架設(shè)值注入操作實(shí)戰(zhàn)案例分析
本文實(shí)例講述了Spring框架設(shè)值注入操作。分享給大家供大家參考,具體如下:
一 配置
<?xml version="1.0" encoding="GBK"?>
<!-- Spring配置文件的根元素,使用spring-beans-4.0.xsd語義約束 -->
<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">
<!-- 配置chinese實(shí)例,其實(shí)現(xiàn)類是Chinese類 -->
<bean id="chinese" class="org.crazyit.app.service.impl.Chinese">
<!-- 驅(qū)動(dòng)調(diào)用chinese的setAxe()方法,將容器中stoneAxe作為傳入?yún)?shù) -->
<property name="axe" ref="stoneAxe"/>
</bean>
<!-- 配置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;
// 設(shè)值注入所需的setter方法
public void setAxe(Axe axe)
{
this.axe = axe;
}
// 實(shí)現(xiàn)Person接口的useAxe方法
public void useAxe()
{
// 調(diào)用axe的chop()方法,
// 表明Person對(duì)象依賴于axe對(duì)象
System.out.println(axe.chop());
}
}
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 實(shí)例
Person p = ctx.getBean("chinese" , Person.class);
// 調(diào)用useAxe()方法
p.useAxe();
}
}
五 運(yù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緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- SpringBoot集合Mybatis過程解析
- SpringMVC接收復(fù)雜集合對(duì)象(參數(shù))代碼示例
- SpringMVC與Mybatis集合實(shí)現(xiàn)調(diào)用存儲(chǔ)過程、事務(wù)控制實(shí)例
- 詳解Java的Spring框架中bean的注入集合
- Spring實(shí)戰(zhàn)之注入嵌套Bean操作示例
- 靜態(tài)方法中調(diào)用Spring注入過程解析
- Spring框架構(gòu)造注入操作實(shí)戰(zhàn)案例
- spring依賴注入知識(shí)點(diǎn)分享
- spring依賴注入原理與用法實(shí)例分析
- Spring實(shí)戰(zhàn)之注入集合值操作示例
相關(guān)文章
SpringBoot從配置文件中獲取屬性的四種方法總結(jié)
這篇文章主要介紹了SpringBoot從配置文件中獲取屬性的四種方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Java超級(jí)實(shí)用的Freemarker工具類
這篇文章主要介紹了Java超級(jí)實(shí)用的Freemarker工具類,文章圍繞相關(guān)資料介紹以及代碼描述非常詳細(xì),需要的小伙伴可以參考一下,希望對(duì)你得學(xué)習(xí)有所幫助2022-02-02
Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
基于list stream: reduce的使用實(shí)例
這篇文章主要介紹了list stream: reduce的使用實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
SpringBoot+JPA?分頁查詢指定列并返回指定實(shí)體方式
這篇文章主要介紹了SpringBoot+JPA?分頁查詢指定列并返回指定實(shí)體方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java實(shí)現(xiàn)斗地主最簡代碼實(shí)例
在本篇文章里小編給各位分享的是關(guān)于Java實(shí)現(xiàn)斗地主最簡代碼實(shí)例,有興趣的朋友們可以參考下。2020-05-05

