问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

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

  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;

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 ();
    }
}

运行结果

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号