当前位置: 首页 > news >正文

【spring】@Resource注解学习

@Resource介绍

在Spring框架中,@Resource 注解是一个JSR-250标准注解,用于自动装配(autowiring)Spring容器中的bean。@Resource 注解可以用于字段、方法和方法参数上,以声明依赖注入。

@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注解使用场景

  1. 数据库连接池注入:在 Java 应用中,数据库连接池是一个常见的资源。使用 @Resource 注解可以将数据库连接池注入到需要使用数据库连接的类中。

  2. JNDI 资源注入:Java Naming and Directory Interface(JNDI)是一个应用程序设计的API,为开发人员提供了查找和访问各种命名和目录服务的通用、统一的接口,如DNS、LDAP、NIS、CORBA 对象服务等。使用 @Resource 注解可以将 JNDI 资源注入到 JavaBean 中。

  3. 事务管理器注入:在 Java 应用中,事务管理器是一个重要的资源。使用 @Resource 注解可以将事务管理器注入到需要进行事务管理的类中。

  4. 其他资源注入:除了上述资源外,@Resource 注解还可以用于将其他类型的资源注入到 JavaBean 中,如文件资源、网络资源等。

@Resource测试示例代码

示例代码 一

ResourceDemoService类
package com.yang.SpringTest.annotation.resourceLean;/*** <p>ResourceDemoService类</p>** @author By: chengxuyuanshitang* Package com.yang.SpringTest.annotation.resourceLean* Ceate Time 2024-04-12 16:23*/
public interface ResourceDemoService {void demo();
}
ResourceDemoServiceAImpl类
package com.yang.SpringTest.annotation.resourceLean;import org.springframework.stereotype.Service;/*** <p>ResourceDemoServiceAImpl类</p>** @author By: chengxuyuanshitang* Package com.yang.SpringTest.annotation.resourceLean* Ceate Time 2024-04-12 16:26*/
@Service("resourceDemoServiceA")
public class ResourceDemoServiceAImpl implements ResourceDemoService {@Overridepublic void demo () {System.out.println ("===== ResourceDemoServiceAImpl.demo...");}
}
ResourceDemoServiceBImpl类
package com.yang.SpringTest.annotation.resourceLean;import org.springframework.stereotype.Service;/*** <p>ResourceDemoServiceBImpl类</p>** @author By: chengxuyuanshitang* Package com.yang.SpringTest.annotation.resourceLean* Ceate Time 2024-04-12 16:26*/
@Service("resourceDemoServiceB")
public class ResourceDemoServiceBImpl implements ResourceDemoService {@Overridepublic void demo () {System.out.println ("===== ResourceDemoServiceBImpl.demo...");}
}
ResourceDemoController类
package com.yang.SpringTest.annotation.resourceLean;import org.springframework.stereotype.Controller;import javax.annotation.Resource;/*** <p>ResourceDemoController类</p>** @author By: chengxuyuanshitang* Package com.yang.SpringTest.annotation.resourceLean* Ceate Time 2024-04-12 16:27*/
@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;/*** <p>ResourceDemoConfig配置类</p>** @author By: chengxuyuanshitang* Package com.yang.SpringTest.annotation.resourceLean* Ceate Time 2024-04-12 16:31*/
@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;/*** <p>ResourceDemoTest测试类</p>** @author By: chengxuyuanshitang* Package com.yang.SpringTest.annotation.resourceLean* Ceate Time 2024-04-12 16:32*/
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 ();}}
运行结果




http://www.lryc.cn/news/339873.html

相关文章:

  • 【leetcode面试经典150题】43. 字母异位词分组(C++)
  • 计算机网络 Cisco路由器基本配置
  • Windows Edge 兼容性问题修复:提升用户体验的关键步骤
  • Vue 3 性能飞跃:解析其性能提升的关键方面
  • MySQL 存储过程中,参数的传递主要通过以下两种方式:IN、OUT 和 INOUT
  • 修改当前Git仓库的地址、用户名、密码
  • 尚鼎环境科技诚邀您参观2024第13届生物发酵展
  • UE5 C++ 创建3DWidgete 血条 再造成伤害
  • Android 14 vold 分析(1)启动
  • 【云计算】混合云组成、应用场景、风险挑战
  • spring bean的继承和依赖
  • Swift中的字符串
  • MySQL基础-----约束详解
  • 【Unity】游戏场景添加后处理特效PostProcessing
  • STM32芯片软复位导致SRAM2的值被擦除话题
  • 【C++航海王:追寻罗杰的编程之路】异常——错误处理方式之一
  • 5.2 mybatis之autoMappingBehavior作用
  • 【算法一则】做算法学数据结构 - 简化路径 - 【栈】
  • OpenHarmony实战开发-如何使用Web预渲染实现功能介绍。
  • 三七互娱,oppo,快手25届暑期实习内推
  • InnoDB架构:内存篇
  • 8个Python高效数据分析的技巧
  • 暴力破解密码自动阻断
  • 【华为】Telnet实验配置
  • SAM功能改进VRP-SAM论文解读VRP-SAM: SAM with Visual Reference Prompt
  • MySQL truncate table 与 delete 清空表的区别和坑
  • Spring GA、PRE、SNAPSHOT 版本含义及区别
  • 一文看懂标准版和Pro版的区别
  • 腾讯云服务器价格表(腾讯云服务器报价表)
  • 试试把GPT和Suno结合起来用(附免费GPT)