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 ();
}
}
运行结果
热门推荐
莫愁千里路,自有到来风。34句释怀诗词,读完马上停止精神内耗
苹果电脑怎么打加减符号
血小板减少需要注意哪些事项
一文搞懂马斯克的第一性原理
重视高尿酸血症的管理
梅西大学究竟有何魅力值得学生奔赴?
海绵蛋糕和戚风蛋糕有什么区别?
山东小吃推荐:品味地道山东味
今年大专报考专业怎么选
2025年中国住房租赁市场新趋势与战略洞察
APP测试完全指南:从功能到安全的全方位测试攻略
2025深度多模态数据融合综述:学术最前沿进展大揭秘!
金黄色葡萄球菌感染的传播途径
激光祛疤一次能恢复吗?有副作用吗?
DIY做置物架家具必修课——框架!
外泌体表征测量技术最新进展
什么是方差和标准差?方差和标准差在数据分析中的意义是什么?
【AI论文】批判性微调:学会批判比学会模仿更有效
如何与HR谈话:提升职场沟通技巧的有效交流策略
去除妊娠纹的保护措施一定要知道
足三里针灸作用与好处
足三里穴位按摩:功效与注意事项
木瓜发苦的原因及处理方法
木瓜为什么会发苦(了解木瓜发苦的原因以及应对措施)
加强短视频侵权打击,保护创作者权益
独立站分批发货策略与物流选择攻略
被删后,聊天记录能保存多久?
食物真的能影响心情吗?最新研究证实的关联与建议
最新研究证实:食物真的能影响心情
单反相机RAW格式设置指南