Spring實戰(zhàn)之使用@Resource配置依賴操作示例
本文實例講述了Spring使用@Resource配置依賴操作。分享給大家供大家參考,具體如下:
一 配置
<?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;
}
// 實現(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();
}
}
五 測試結果
石斧砍柴好慢
更多關于java相關內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數(shù)據(jù)結構與算法教程》、《Java操作DOM節(jié)點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
- Spring實戰(zhàn)之ResourceLoaderAware加載資源用法示例
- Spring實戰(zhàn)之ResourceLoader接口資源加載用法示例
- Spring實戰(zhàn)之ServletContextResource訪問資源文件示例
- Spring實戰(zhàn)之FileSystemResource加載資源文件示例
- Spring實戰(zhàn)之使用ClassPathResource加載xml資源示例
- Spring 中 @Service 和 @Resource 注解的區(qū)別
- Spring框架中 @Autowired 和 @Resource 注解的區(qū)別
- 詳解Spring注解--@Autowired、@Resource和@Service
- Springboot項目打war包docker包找不到resource下靜態(tài)資源的解決方案
- 詳解SpringBoot開發(fā)使用@ImportResource注解影響攔截器
- Spring實戰(zhàn)之使用Resource作為屬性操作示例
相關文章
IDEA整合SSM框架實現(xiàn)網(wǎng)頁上顯示數(shù)據(jù)
最近做了個小項目,該項目包在intellij idea中實現(xiàn)了ssm框架的整合以及實現(xiàn)訪問,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05
springboot基于過濾器實現(xiàn)接口請求耗時統(tǒng)計操作
這篇文章主要介紹了springboot基于過濾器實現(xiàn)接口請求耗時統(tǒng)計操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
SpringBoot?Validation快速實現(xiàn)數(shù)據(jù)校驗的示例代碼
在實際開發(fā)中,肯定會經(jīng)常遇到對參數(shù)字段進行校驗的場景,通常我們只能寫大量的if else來完成校驗工作,而如果使用SpringBoot Validation則可以輕松的通過注解來完成,接下來小編給大家介紹下利用SpringBoot?Validation快速實現(xiàn)數(shù)據(jù)校驗的示例代碼,需要的朋友參考下吧2022-06-06
Java 迪杰斯特拉算法實現(xiàn)查找最短距離的實現(xiàn)
這篇文章主要介紹了Java 迪杰斯特拉算法實現(xiàn)查找最短距離的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09
SpringBoot3集成SLF4J+logback進行日志記錄的實現(xiàn)
本文主要介紹了SpringBoot3集成SLF4J+logback進行日志記錄的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
Java Iterator迭代器_動力節(jié)點Java學院整理
迭代器是一種模式,它可以使得對于序列類型的數(shù)據(jù)結構的遍歷行為與被遍歷的對象分離,接下來通過本文給大家分享Java Iterator迭代器_動力節(jié)點Java學院整理,需要的朋友參考下吧2017-05-05

