Spring框架@Resource注解详解与实战
创作时间:
作者:
@小白创作中心
Spring框架@Resource注解详解与实战
引用
CSDN
1.
https://m.blog.csdn.net/wochunyang/article/details/137639720
@Resource注解是Spring框架中用于自动装配bean的重要注解,遵循JSR-250标准。它支持字段、方法和方法参数级别的依赖注入,是开发Spring应用时不可或缺的工具。本文将从源码层面深入解析@Resource注解的各个属性,并通过实际代码示例展示其具体应用场景。
@Resource注解介绍
@Resource注解是一个JSR-250标准注解,用于自动装配(autowiring)Spring容器中的bean。它可以用于字段、方法和方法参数上,以声明依赖注入。
@Resource注解源码
@Target({TYPE, FIELD, METHOD})
@Retention(RUNTIME)
@Repeatable(Resources.class)
public @interface Resource {
String name() default "";
String lookup() default "";
Class<?> type() default java.lang.Object.class;
enum AuthenticationType {
CONTAINER,
APPLICATION
}
AuthenticationType authenticationType() default AuthenticationType.CONTAINER;
boolean shareable() default true;
String mappedName() default "";
String description() default "";
}
@Resource注解属性介绍
- name:资源的JNDI名称,装配指定名称的Bean。
- type:装配指定类型的Bean。
- lookup:引用指向的资源名称,可以使用JNDI名称指向任何兼容的资源。
- AuthenticationType:指定身份验证类型。
- shareable:指定当前Bean是否可以在多个组件之间共享。
- mappedName:指定资源的映射名称。
- description:指定资源的描述。
@Resource注解使用场景
- 数据库连接池注入:在Java应用中,数据库连接池是一个常见的资源。使用@Resource注解可以将数据库连接池注入到需要使用数据库连接的类中。
- JNDI资源注入:Java Naming and Directory Interface(JNDI)是一个应用程序设计的API,为开发人员提供了查找和访问各种命名和目录服务的通用、统一的接口,如DNS、LDAP、NIS、CORBA对象服务等。使用@Resource注解可以将JNDI资源注入到JavaBean中。
- 事务管理器注入:在Java应用中,事务管理器是一个重要的资源。使用@Resource注解可以将事务管理器注入到需要进行事务管理的类中。
- 其他资源注入:除了上述资源外,@Resource注解还可以用于将其他类型的资源注入到JavaBean中,如文件资源、网络资源等。
@Resource注解测试示例代码
ResourceDemoService接口
package com.yang.SpringTest.annotation.resourceLean;
public interface ResourceDemoService {
void demo();
}
ResourceDemoServiceAImpl实现类
package com.yang.SpringTest.annotation.resourceLean;
import org.springframework.stereotype.Service;
@Service("resourceDemoServiceA")
public class ResourceDemoServiceAImpl implements ResourceDemoService {
@Override
public void demo () {
System.out.println ("===== ResourceDemoServiceAImpl.demo...");
}
}
ResourceDemoServiceBImpl实现类
package com.yang.SpringTest.annotation.resourceLean;
import org.springframework.stereotype.Service;
@Service("resourceDemoServiceB")
public class ResourceDemoServiceBImpl implements ResourceDemoService {
@Override
public void demo () {
System.out.println ("===== ResourceDemoServiceBImpl.demo...");
}
}
ResourceDemoController控制器
package com.yang.SpringTest.annotation.resourceLean;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
@Controller
public class ResourceDemoController {
@Resource(name = "resourceDemoServiceB")
private ResourceDemoService resourceDemoService;
public void demo () {
resourceDemoService.demo ();
}
}
ResourceDemoConfig配置类
package com.yang.SpringTest.annotation.resourceLean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(value = {"com.yang.SpringTest.annotation.resourceLean"})
public class ResourceDemoConfig {
}
ResourceDemoTest测试类
package com.yang.SpringTest.annotation.resourceLean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.Arrays;
public class ResourceDemoTest {
public static void main (String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (ResourceDemoConfig.class);
String[] definitionNames = context.getBeanDefinitionNames ();
Arrays.stream (definitionNames).forEach ((definitionName) -> System.out.println (definitionName));
System.out.println ("--------------------");
ResourceDemoController resourceDemoController = context.getBean (ResourceDemoController.class);
resourceDemoController.demo ();
context.close ();
}
}
运行结果
热门推荐
房产过户有哪些方式可选
家庭智能门锁选购避坑指南:五大黄金法则守住安全与便利
抑郁症、焦虑症
男篮亚预赛总结:两任主帅率21人出战 真正挑战还没来
言简意赅的文章才是好文章
临床研究有哪些类型?什么是IIT研究呢?
影响电焊弧稳定性的因素分析
上海女孩之死背后,“主动安乐死”的争议在哪?
教师情感支持:学生心灵的温暖灯塔与成长导航
研究评估大豆进口对我国粮食、水资源、耕地等的综合影响
电脑内存不足时如何查看?有哪些优化建议?
权属是什么意思
共享计划完整版:如何利用共享经济最大化收益?
开始使用树莓派:给树莓派安装操作系统
糖尿病人可以吃苹果吗
股票投资的首要步骤是什么?这个首要步骤在实际操作中会面临哪些挑战?
钓鱼技巧揭秘:详解鲫鱼习性、钓具选择、饵料运用与投饵技巧
【定位精度的秘密武器】:3大策略优化RTK数据链路
搞懂RTK定位,看这一篇就够了
Excel材料成本记录完整指南:从基础到自动化
心电图(ECG 或 EKG)
减少大豆进口依赖“新招”:中国开发新型高蛋白玉米
历史学者王汎森著作《古史辨运动的兴起》简体版首次问世
左右脸不对称的原因是什么?五招帮你矫正脸型
治疗慢性咽炎的中成药是什么
前瞻性研究和回顾性研究之间的异同
电池校准指南:如何确保设备电池健康和准确显示剩余电量
不确定的英语
南安:一场蓄谋已久的历史与文化盛宴
二手房交易需支付哪些费用?