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 ();
}
}
运行结果
热门推荐
信用卡逾期显示呆账原因分析
莲雾怎么吃要削皮嘛
降低体脂率的运动(体脂怎么减最快)
服装店如何选址?新手开店选址必需考虑的10个细节
如何自测肺功能是否正常?
如何剖析企业利润的波动情况?这种波动情况有哪些应对策略?
什么是多空指标?它在市场分析中如何发挥作用?
独立女孩子的特征
如果P=NP被证明了会发生什么
香煎黄花鱼
美国主要支付方式大揭秘:PayPal、信用卡、现金哪家强?
红鸾星动:古代命理学中的姻缘征兆
行走“几字弯”:地下水超采区水位回升,这里是如何做到的?
一千字搞懂上拉电阻和下拉电阻的实战应用
胆囊息肉长到1cm要多久?这些因素影响其生长速度
不按交通信号指示通行的处罚标准及处理流程
流感为什么会引发肺炎?早期使用这个药,可降低肺炎发生率40%!
越来越多瑞士人坦诚并无宗教信仰
起诉书怎样写事实理由
零售银行业的未来:如何提升客户体验
胃溃疡通过什么检查出来
家常美食制作——蒜香椒盐虾(做法简单易学,香辣可口)
复合材料的应用介绍
咖啡豆检测项目有哪些?咖啡豆的质量标准介绍
河北皮影戏:赶潮出圈
选购壁挂炉全攻略:品牌、性能、预算与售后服务全方位考量
50㎡小公寓也能装出6大功能区,用实例教你“榨干”小家每寸空间
近20年豆瓣评分最高的36部国产悬疑剧大盘点
老人的产权房如何继承?小产权房风险全解析
住房公积金跨省贷款购房的条件和注意事项