Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中注释是如何使用的,在哪里使用的?_Java_Annotations - Fatal编程技术网

Java中注释是如何使用的,在哪里使用的?

Java中注释是如何使用的,在哪里使用的?,java,annotations,Java,Annotations,我们可以使用注释的主要领域是什么?该功能是否取代了基于XML的配置?Java注释有多种应用程序。首先,它们可能被编译器(或编译器扩展)使用。例如,考虑重写注释: class Foo { @Override public boolean equals(Object other) { return ...; } } 这一个实际上内置于JavaJDK中。如果某个方法被标记为错误,编译器将发出错误信号,该错误不会覆盖从基类继承的方法。此注释可能有助于避免常见错误,即您

我们可以使用注释的主要领域是什么?该功能是否取代了基于XML的配置?

Java注释有多种应用程序。首先,它们可能被编译器(或编译器扩展)使用。例如,考虑重写注释:

class Foo {

    @Override public boolean equals(Object other) {
        return ...;
    }
}
这一个实际上内置于JavaJDK中。如果某个方法被标记为错误,编译器将发出错误信号,该错误不会覆盖从基类继承的方法。此注释可能有助于避免常见错误,即您实际上打算重写一个方法,但由于方法中给出的签名与被重写的方法的签名不匹配而未能重写该方法:

class Foo {

    @Override public boolean equals(Foo other) {  // Compiler signals an error for this one
        return ...;
    }
}
从JDK7开始,任何类型都允许使用注释。此功能现在可用于编译器注释,如NotNull,如:

public void processSomething(@NotNull String text) {
    ...
}
这允许编译器警告您变量和空值的不正确/未经检查的使用


另一个更高级的注释应用程序涉及运行时的反射和注释处理。当您将注释称为“基于XML的配置的替代品”时,我想这就是您的想法。这是一种注释处理,例如,各种框架和JCP标准(持久性、依赖性注入,你可以说)使用这种处理来提供必要的元数据和配置信息。

它对于在方法、类或字段级别注释类非常有用,关于那个班级的一些与班级不太相关的东西

您可以有自己的注释,用于将某些类标记为仅用于测试。它可以仅仅用于文档目的,也可以通过在编译生产版本候选版本时过滤掉它来强制执行

您可以使用注释来存储一些元数据,比如在插件框架中,例如插件的名称


它只是另一个工具,它有很多用途。

注释是添加到Java源文件的元数据(关于数据的数据)的一种形式。框架主要使用它们来简化客户机代码的集成。我脑海中有几个真实世界的例子:

  • JUnit4—您将
    @Test
    注释添加到希望JUnit运行程序运行的每个测试方法中。另外还有一些关于设置测试的附加注释(如
    @Before
    @BeforeClass
    )。所有这些都由JUnit runner处理,JUnit runner相应地运行测试。您可以说它是XML配置的替代品,但是注释有时更强大(例如,它们可以使用反射),而且它们更接近于它们所引用的代码(
    @Test
    注释就在测试方法之前,因此该方法的用途很清楚——也可以作为文档)。另一方面,XML配置可能更复杂,并且可能包含比注释多得多的数据

  • Terracotta-使用注释和XML配置文件。例如,
    @Root
    注释告诉Terracotta运行时,带注释的字段是根,其内存应该在VM实例之间共享。XML配置文件用于配置服务器并告诉它要插入哪些类

  • Google Guice-一个例子是
    @Inject
    注释,当应用于构造函数时,它使Guice运行时根据定义的注入器查找每个参数的值。
    @Inject
    注释将很难使用XML配置文件进行复制,而且它与它引用的构造函数的接近性非常有用(想象一下,必须搜索一个巨大的XML文件才能找到您设置的所有依赖项注入)

希望我已经让您了解了注释是如何在不同的框架中使用的

它是否取代了基于XML的应用程序 配置


并非完全如此,但与代码结构(如JPA映射或Spring中的依赖项注入)密切对应的配置通常可以用注释代替,这样通常就不会那么冗长、烦人和痛苦了。几乎所有著名的框架都进行了这种转换,尽管旧的XML配置通常仍然是一种选择。

JavaEE5倾向于使用注释而不是XML配置。例如,在EJB3中,EJB方法上的事务属性是使用注释指定的。他们甚至使用注释将POJO标记为EJB,并将特定方法指定为生命周期方法,而不需要实现接口。

像Hibernate这样的框架需要大量配置/映射,需要大量使用注释

看看JPA(来自JavaEE5)是注释(过度)使用的一个极好的例子。JavaEE6还将在许多新领域引入注释,例如RESTfulWebService和在好的旧ServletAPI下为每个应用程序添加新注释

以下是一些资源:

  • (检查所有三页)

注释不仅接管/可以接管配置细节,还可以使用它们来控制行为。您可以在JavaEE6的JAX-RS示例中看到这一点

  • 在用户视图中,大多数情况下,注释就像快捷方式一样工作,可以为您节省一些笔划,或者使您的程序更具可读性

  • 供应商视图,处理器的注释视图更多的是轻量级的“接口”,您的程序确实遇到了一些问题,但没有明确地“实现”特定的接口(这里又称注释)

  • e、 在jpa中,你定义了

    @Entity class Foo {...}
    
    而不是

    class Foo implements Entity {...}
    

    两者都说同样的话“Foo是一个实体类”

    注释可以用作exter的替代品
    @Override // gives error if signature is wrong while overriding.
    Public boolean equals (Object Obj) 
    
    @Deprecated // indicates the deprecated method
    Public doSomething()....
    
    @SuppressWarnings() // stops the warnings from printing while compiling.
    SuppressWarnings({"unchecked","fallthrough"})
    
    @Retention - Specifies how the marked annotation is stored—Whether in code only, compiled into the class, or available at run-time through reflection.
    
    @Documented - Marks another annotation for inclusion in the documentation.
    
    @Target - Marks another annotation to restrict what kind of java elements the annotation may be applied to
    
    @Inherited - Marks another annotation to be inherited to subclasses of annotated class (by default annotations are not inherited to subclasses).
    
    package io.hamzeen;
    
    import java.lang.annotation.Annotation;
    
    public class Driver {
    
        public static void main(String[] args) {
            Class<TestAlpha> obj = TestAlpha.class;
            if (obj.isAnnotationPresent(IssueInfo.class)) {
    
                Annotation annotation = obj.getAnnotation(IssueInfo.class);
                IssueInfo testerInfo = (IssueInfo) annotation;
    
                System.out.printf("%nType: %s", testerInfo.type());
                System.out.printf("%nReporter: %s", testerInfo.reporter());
                System.out.printf("%nCreated On: %s%n%n",
                        testerInfo.created());
            }
        }
    }
    
    package io.hamzeen;
    
    import io.hamzeen.IssueInfo;
    import io.hamzeen.IssueInfo.Type;
    
    @IssueInfo(type = Type.IMPROVEMENT, reporter = "Hamzeen. H.")
    public class TestAlpha {
    
    }
    
    package io.hamzeen;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    /**
     * @author Hamzeen. H.
     * @created 10/01/2015
     * 
     * IssueInfo annotation definition
     */
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    public @interface IssueInfo {
    
        public enum Type {
            BUG, IMPROVEMENT, FEATURE
        }
    
        Type type() default Type.BUG;
    
        String reporter() default "Vimesh";
    
        String created() default "10/01/2015";
    }
    
    a. Annotations can be used by compiler to detect errors and suppress warnings
    b. Software tools can use annotations to generate code, xml files, documentation etc., For example, Javadoc use annotations while generating java documentation for your class.
    c. Runtime processing of the application can be possible via annotations.
    d. You can use annotations to describe the constraints (Ex: @Null, @NotNull, @Max, @Min, @Email).
    e. Annotations can be used to describe type of an element. Ex: @Entity, @Repository, @Service, @Controller, @RestController, @Resource etc.,
    f. Annotation can be used to specify the behaviour. Ex: @Transactional, @Stateful
    g. Annotation are used to specify how to process an element. Ex: @Column, @Embeddable, @EmbeddedId
    h. Test frameworks like junit and testing use annotations to define test cases (@Test), define test suites (@Suite) etc.,
    i. AOP (Aspect Oriented programming) use annotations (@Before, @After, @Around etc.,)
    j. ORM tools like Hibernate, Eclipselink use annotations