Spring實(shí)戰(zhàn)之使用@Resource配置依賴操作示例

本文實(shí)例講述了Spring使用@Resource配置依賴操作。分享給大家供大家參考,具體如下:

成都創(chuàng)新互聯(lián)成立十多年來,這條路我們正越走越好,積累了技術(shù)與客戶資源,形成了良好的口碑。為客戶提供成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站策劃、網(wǎng)頁設(shè)計、國際域名空間、網(wǎng)絡(luò)營銷、VI設(shè)計、網(wǎng)站改版、漏洞修補(bǔ)等服務(wù)。網(wǎng)站是否美觀、功能強(qiáng)大、用戶體驗(yàn)好、性價比高、打開快等等,這些對于網(wǎng)站建設(shè)都非常重要,成都創(chuàng)新互聯(lián)通過對建站技術(shù)性的掌握、對創(chuàng)意設(shè)計的研究為客戶提供一站式互聯(lián)網(wǎng)解決方案,攜手廣大客戶,共同發(fā)展進(jìn)步。

一 配置

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-4.0.xsd">
   <!-- 自動掃描指定包及其子包下的所有Bean類 -->
   <context:component-scan
      base-package="org.crazyit.app.service"/>
</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();
}

三 Bean

Chinese

package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import javax.annotation.*;
import org.crazyit.app.service.*;
@Component
public class Chinese implements Person
{
  private Axe axe;
  // axe的setter方法
  @Resource(name="stoneAxe")
  public void setAxe(Axe axe)
  {
    this.axe = axe;
  }
  // 實(shí)現(xiàn)Person接口的useAxe()方法
  public void useAxe()
  {
    // 調(diào)用axe的chop()方法,
    // 表明Person對象依賴于axe對象
    System.out.println(axe.chop());
  }
}

SteelAxe

package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import org.crazyit.app.service.*;
@Component
public class SteelAxe implements Axe
{
  public String chop()
  {
    return "鋼斧砍柴真快";
  }
}

StoneAxe

package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import org.crazyit.app.service.*;
@Component
public class StoneAxe implements Axe
{
  public String chop()
  {
    return "石斧砍柴好慢";
  }
}

四 測試類

package lee;
 import org.springframework.context.*;
import org.springframework.context.support.*;
 import org.crazyit.app.service.*;
 public class BeanTest
{
  public static void main(String[] args)
  {
    // 創(chuàng)建Spring容器
    ApplicationContext ctx = new
      ClassPathXmlApplicationContext("beans.xml");
    Person person = ctx.getBean("chinese" , Person.class);
    person.useAxe();
  }
}

五 測試結(jié)果

石斧砍柴好慢

更多關(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è)計有所幫助。

當(dāng)前文章:Spring實(shí)戰(zhàn)之使用@Resource配置依賴操作示例
當(dāng)前地址:http://muchs.cn/article36/gdiesg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、做網(wǎng)站、用戶體驗(yàn)定制開發(fā)、網(wǎng)站設(shè)計、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)