如何申请建设网站首页,检验是否安装wordpress,制作二维码的软件app,官方建网站有哪些步骤Resource介绍
在Spring框架中#xff0c;Resource 注解是一个JSR-250标准注解#xff0c;用于自动装配#xff08;autowiring#xff09;Spring容器中的bean。Resource 注解可以用于字段、方法和方法参数上#xff0c;以声明依赖注入。
Resource源码
Target({TYPE, FIE…Resource介绍
在Spring框架中Resource 注解是一个JSR-250标准注解用于自动装配autowiringSpring容器中的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注解使用场景 数据库连接池注入在 Java 应用中数据库连接池是一个常见的资源。使用 Resource 注解可以将数据库连接池注入到需要使用数据库连接的类中。 JNDI 资源注入Java Naming and Directory InterfaceJNDI是一个应用程序设计的API为开发人员提供了查找和访问各种命名和目录服务的通用、统一的接口如DNS、LDAP、NIS、CORBA 对象服务等。使用 Resource 注解可以将 JNDI 资源注入到 JavaBean 中。 事务管理器注入在 Java 应用中事务管理器是一个重要的资源。使用 Resource 注解可以将事务管理器注入到需要进行事务管理的类中。 其他资源注入除了上述资源外Resource 注解还可以用于将其他类型的资源注入到 JavaBean 中如文件资源、网络资源等。
Resource测试示例代码
示例代码 一 ResourceDemoService类
package com.yang.SpringTest.annotation.resourceLean;/*** pResourceDemoService类/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;/*** pResourceDemoServiceAImpl类/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;/*** pResourceDemoServiceBImpl类/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;/*** pResourceDemoController类/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;/*** pResourceDemoConfig配置类/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;/*** pResourceDemoTest测试类/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 ();}}
运行结果