有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

类型级别上的java@Resource

我发现@Resource也可以应用于类级别:

http://docs.oracle.com/javaee/5/tutorial/doc/bncjk.html

Class-Based Injection To use class-based injection, decorate the class with a @Resource annotation, and set the required name and type elements.

@Resource(name="myMessageQueue", type="javax.jms.ConnectionFactory") public class SomeMessageBean { ... }

你有过这样使用@Resource的经验吗


共 (1) 个答案

  1. # 1 楼答案

    来自资源javadoc

    The Resource annotation marks a resource that is needed by the application. This annotation may be applied to an application component class, or to fields or methods of the component class. When the annotation is applied to a field or method, the container will inject an instance of the requested resource into the application component when the component is initialized. If the annotation is applied to the component class, the annotation declares a resource that the application will look up at runtime

    这意味着当使用基于注释的配置和类路径扫描时,在类级别用@Resource注释的类将被视为自动检测的候选类