Spring实战之使用@Resource配置依赖操作示例
作者:cakincqm 时间:2023-08-06 10:11:13
本文实例讲述了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;
}
// 实现Person接口的useAxe()方法
public void useAxe()
{
// 调用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)
{
// 创建Spring容器
ApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
Person person = ctx.getBean("chinese" , Person.class);
person.useAxe();
}
}
五 测试结果
石斧砍柴好慢
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/chengqiuming/article/details/101170158
标签:Spring,@Resource,配置依赖
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c# BackgroundWorker使用方法
2021-05-27 00:49:12
![](https://img.aspxhome.com/file/2023/1/76411_0s.png)
C#编程实现向并口设备发送指令、获取并口设备的状态
2023-02-02 02:56:39
Android scrollToTop实现点击回到顶部(兼容PullTorefreshScrollview)
2021-07-29 09:06:37
![](https://img.aspxhome.com/file/2023/2/122102_0s.png)
浅谈java异常处理之空指针异常
2022-07-31 03:12:27
Unity游戏开发之2048游戏的实现
2023-04-19 00:11:33
![](https://img.aspxhome.com/file/2023/5/102295_0s.jpg)
JavaFX之TableView的使用详解
2022-07-16 06:32:24
![](https://img.aspxhome.com/file/2023/4/125584_0s.png)
Spring bean对象实例化实现过程图解
2023-01-02 07:32:13
![](https://img.aspxhome.com/file/2023/1/125501_0s.png)
C#利用Openxml读取Excel数据实例
2021-05-27 18:27:06
Java实现两人五子棋游戏(五) 判断是否有一方胜出
2022-03-03 18:29:44
![](https://img.aspxhome.com/file/2023/5/64955_0s.jpg)
在Java内存模型中测试并发程序代码
2023-11-24 20:37:55
![](https://img.aspxhome.com/file/2023/7/59967_0s.png)
JavaWeb ServletContext基础与应用详细讲解
2021-12-20 22:13:10
JavaWeb建立简单三层项目步骤图解
2023-03-08 16:51:02
![](https://img.aspxhome.com/file/2023/9/63799_0s.png)
Java 回调callback举例详解
2023-11-11 16:25:09
![](https://img.aspxhome.com/file/2023/2/59592_0s.png)
实例化JFileChooser对象报空指针异常问题的解决办法
2023-10-05 11:36:18
![](https://img.aspxhome.com/file/2023/7/132067_0s.png)
SpringMvc MultipartFile实现图片文件上传示例
2022-07-30 16:40:45
![](https://img.aspxhome.com/file/2023/5/113455_0s.png)
java多线程加锁以及Condition类的使用实例解析
2023-08-07 07:25:30
![](https://img.aspxhome.com/file/2023/3/57713_0s.png)
c++中的malloc底层实现代码
2023-11-02 19:49:18
![](https://img.aspxhome.com/file/2023/3/105183_0s.jpg)
详解Android中PopupWindow在7.0后适配的解决
2022-12-07 11:16:59
![](https://img.aspxhome.com/file/2023/2/90142_0s.png)
Android WorkManager使用以及源码分析
2022-02-04 01:10:26
![](https://img.aspxhome.com/file/2023/4/89554_0s.jpg)
WPF Slider滑动条的颜色修改方法
2022-06-15 20:49:38
![](https://img.aspxhome.com/file/2023/7/81147_0s.png)