3-2 案例准备工作

定义注解

java.lang.annotation.Target

/*
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

package java.lang.annotation;

/**
 * Indicates the contexts in which an annotation type is applicable. The
 * declaration contexts and type contexts in which an annotation type may be
 * applicable are specified in JLS 9.6.4.1, and denoted in source code by enum
 * constants of {@link ElementType java.lang.annotation.ElementType}.
 *
 * <p>If an {@code @Target} meta-annotation is not present on an annotation type
 * {@code T} , then an annotation of type {@code T} may be written as a
 * modifier for any declaration except a type parameter declaration.
 *
 * <p>If an {@code @Target} meta-annotation is present, the compiler will enforce
 * the usage restrictions indicated by {@code ElementType}
 * enum constants, in line with JLS 9.7.4.
 *
 * <p>For example, this {@code @Target} meta-annotation indicates that the
 * declared type is itself a meta-annotation type.  It can only be used on
 * annotation type declarations:
 * <pre>
 *    &#064;Target(ElementType.ANNOTATION_TYPE)
 *    public &#064;interface MetaAnnotationType {
 *        ...
 *    }
 * </pre>
 *
 * <p>This {@code @Target} meta-annotation indicates that the declared type is
 * intended solely for use as a member type in complex annotation type
 * declarations.  It cannot be used to annotate anything directly:
 * <pre>
 *    &#064;Target({})
 *    public &#064;interface MemberType {
 *        ...
 *    }
 * </pre>
 *
 * <p>It is a compile-time error for a single {@code ElementType} constant to
 * appear more than once in an {@code @Target} annotation.  For example, the
 * following {@code @Target} meta-annotation is illegal:
 * <pre>
 *    &#064;Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD})
 *    public &#064;interface Bogus {
 *        ...
 *    }
 * </pre>
 *
 * @since 1.5
 * @jls 9.6.4.1 @Target
 * @jls 9.7.4 Where Annotations May Appear
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    /**
     * Returns an array of the kinds of elements an annotation type
     * can be applied to.
     * @return an array of the kinds of elements an annotation type
     * can be applied to
     */
    ElementType[] value();
}

java.lang.annotation.RetentionPolicy

/*
 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

package java.lang.annotation;

/**
 * Annotation retention policy.  The constants of this enumerated type
 * describe the various policies for retaining annotations.  They are used
 * in conjunction with the {@link Retention} meta-annotation type to specify
 * how long annotations are to be retained.
 *
 * @author  Joshua Bloch
 * @since 1.5
 */
public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

RUNTIME,注解会在CLASS的字节码文件中存在,在运行时通过反射可以拿到。SOURCE在编译的时候就会被忽略掉。

com.mmall.concurrency.annoations.ThreadSafe

package com.mmall.concurrency.annoations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 课程里用来标记线程安全的类或者写法
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface ThreadSafe {
    String value() default "";
}

com.mmall.concurrency.annoations.NotThreadSafe

package com.mmall.concurrency.annoations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 课程里用来标记【线程不安全】的类或者写法
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface NotThreadSafe {
    String value() default "";
}

com.mmall.concurrency.annoations.Recommend

package com.mmall.concurrency.annoations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 课程里用来标记【推荐】的类或者写法
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface Recommend {
    String value() default "";
}
 

com.mmall.concurrency.annoations.NotRecommend

package com.mmall.concurrency.annoations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 课程里用来标记【不推荐】的类或者写法
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface NotRecommend {
    String value() default "";
}

Microsoft Windows [版本 10.0.17134.407]
(c) 2018 Microsoft Corporation。保留所有权利。

C:UsersHONGZHENHUAimoocconcurrency>git status
On branch master
Your branch is up-to-date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed
)

        .gitignore
        mvnw
        mvnw.cmd
        pom.xml
        src/

nothing added to commit but untracked files present (use "git add" to track)

C:UsersHONGZHENHUAimoocconcurrency>git status
On branch master
Your branch is up-to-date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore
        mvnw
        mvnw.cmd
        pom.xml
        src/

nothing added to commit but untracked files present (use "git add" to track)

C:UsersHONGZHENHUAimoocconcurrency>git add .
warning: LF will be replaced by CRLF in .gitignore.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in mvnw.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in mvnw.cmd.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in pom.xml.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in src/main/java/com/mmall/concurrency/ConcurrencyApplication.java.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in src/main/java/com/mmall/concurrency/ServletInitializer.java.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in src/test/java/com/mmall/concurrency/ConcurrencyApplicationTests.java.
The file will have its original line endings in your working directory.

C:UsersHONGZHENHUAimoocconcurrency>git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   .gitignore
        new file:   mvnw
        new file:   mvnw.cmd
        new file:   pom.xml
        new file:   src/main/java/com/mmall/concurrency/ConcurrencyApplication.java
        new file:   src/main/java/com/mmall/concurrency/ServletInitializer.java
        new file:   src/main/resources/application.properties
        new file:   src/test/java/com/mmall/concurrency/ConcurrencyApplicationTests.java


C:UsersHONGZHENHUAimoocconcurrency>git commit -am 'init'
[master 42a1cd3] 'init'
 8 files changed, 604 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 mvnw
 create mode 100644 mvnw.cmd
 create mode 100644 pom.xml
 create mode 100644 src/main/java/com/mmall/concurrency/ConcurrencyApplication.java
 create mode 100644 src/main/java/com/mmall/concurrency/ServletInitializer.java
 create mode 100644 src/main/resources/application.properties
 create mode 100644 src/test/java/com/mmall/concurrency/ConcurrencyApplicationTests.java

C:UsersHONGZHENHUAimoocconcurrency>git push
Counting objects: 22, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (22/22), 7.62 KiB | 976.00 KiB/s, done.
Total 22 (delta 0), reused 0 (delta 0)
remote: Powered by Gitee.com
To gitee.com:lvyinhaolaiwu/concurrency.git
   52f2dc8..42a1cd3  master -> master

C:UsersHONGZHENHUAimoocconcurrency>git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   src/main/java/com/mmall/concurrency/TestController.java
        new file:   src/main/java/com/mmall/concurrency/annoations/NotRecommend.java
        new file:   src/main/java/com/mmall/concurrency/annoations/NotThreadSafe.java
        new file:   src/main/java/com/mmall/concurrency/annoations/Recommend.java

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   src/main/java/com/mmall/concurrency/TestController.java
        modified:   src/main/java/com/mmall/concurrency/annoations/NotRecommend.java
        modified:   src/main/java/com/mmall/concurrency/annoations/NotThreadSafe.java
        modified:   src/main/java/com/mmall/concurrency/annoations/Recommend.java

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        src/main/java/com/mmall/concurrency/annoations/ThreadSafe.java


C:UsersHONGZHENHUAimoocconcurrency>git add ThreadSafe
fatal: pathspec 'ThreadSafe' did not match any files

C:UsersHONGZHENHUAimoocconcurrency>git add src/main/java/mmall/concurrency/annoations/ThreadSafe
fatal: pathspec 'src/main/java/mmall/concurrency/annoations/ThreadSafe' did not match any files

C:UsersHONGZHENHUAimoocconcurrency>git add src/main/java/mmall/concurrency/annoations/ThreadSafe.java
fatal: pathspec 'src/main/java/mmall/concurrency/annoations/ThreadSafe.java' did not match any files

C:UsersHONGZHENHUAimoocconcurrency>git add ./src/main/java/mmall/concurrency/annoations/ThreadSafe.java
fatal: pathspec './src/main/java/mmall/concurrency/annoations/ThreadSafe.java' did not match any files

C:UsersHONGZHENHUAimoocconcurrency>git add ./src/main/java/mmall/concurrency/annoations/ThreadSafe
fatal: pathspec './src/main/java/mmall/concurrency/annoations/ThreadSafe' did not match any files

C:UsersHONGZHENHUAimoocconcurrency>git add .

C:UsersHONGZHENHUAimoocconcurrency>git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes to be committed:









































































































































Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   src/main/java/com/mmall/concurrency/TestController.java
        new file:   src/main/java/com/mmall/concurrency/annoations/NotRecommend.java
        new file:   src/main/java/com/mmall/concurrency/annoations/NotThreadSafe.java
        new file:   src/main/java/com/mmall/concurrency/annoations/Recommend.java
        new file:   src/main/java/com/mmall/concurrency/annoations/ThreadSafe.java


C:UsersHONGZHENHUAimoocconcurrency>git commit -am 'add annoations'
fatal: Paths with -a does not make sense.

C:UsersHONGZHENHUAimoocconcurrency>git commit -am 'add annoations'
fatal: Paths with -a does not make sense.

C:UsersHONGZHENHUAimoocconcurrency>git commit -a
Aborting commit due to empty commit message.

C:UsersHONGZHENHUAimoocconcurrency>git push
Everything up-to-date

C:UsersHONGZHENHUAimoocconcurrency>git commit -am 'add annoations'
fatal: Paths with -a does not make sense.

C:UsersHONGZHENHUAimoocconcurrency>git commit --am 'add annoations'
error: pathspec ''add' did not match any file(s) known to git.
error: pathspec 'annoations'' did not match any file(s) known to git.

C:UsersHONGZHENHUAimoocconcurrency>git commit -a 'add annoations'
fatal: Paths with -a does not make sense.

C:UsersHONGZHENHUAimoocconcurrency>git init
Reinitialized existing Git repository in C:/Users/ZHONGZHENHUA/imooc/concurrency/.git/

C:UsersHONGZHENHUAimoocconcurrency>git add .

C:UsersHONGZHENHUAimoocconcurrency>git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   src/main/java/com/mmall/concurrency/TestController.java
        new file:   src/main/java/com/mmall/concurrency/annoations/NotRecommend.java
        new file:   src/main/java/com/mmall/concurrency/annoations/NotThreadSafe.java
        new file:   src/main/java/com/mmall/concurrency/annoations/Recommend.java
        new file:   src/main/java/com/mmall/concurrency/annoations/ThreadSafe.java


C:UsersHONGZHENHUAimoocconcurrency>git commit -am 'add annoations'
fatal: Paths with -a does not make sense.

C:UsersHONGZHENHUAimoocconcurrency>git commit -m 'add annoations'
error: pathspec 'annoations'' did not match any file(s) known to git.

C:UsersHONGZHENHUAimoocconcurrency>git init
Reinitialized existing Git repository in C:/Users/ZHONGZHENHUA/imooc/concurrency/.git/

C:UsersHONGZHENHUAimoocconcurrency>git commit -am 'add annoations'
fatal: Paths with -a does not make sense.

C:UsersHONGZHENHUAimoocconcurrency>git commit -am "add annoations"
[master 751c061] add annoations
 5 files changed, 73 insertions(+)
 create mode 100644 src/main/java/com/mmall/concurrency/TestController.java
 create mode 100644 src/main/java/com/mmall/concurrency/annoations/NotRecommend.java
 create mode 100644 src/main/java/com/mmall/concurrency/annoations/NotThreadSafe.java
 create mode 100644 src/main/java/com/mmall/concurrency/annoations/Recommend.java
 create mode 100644 src/main/java/com/mmall/concurrency/annoations/ThreadSafe.java

C:UsersHONGZHENHUAimoocconcurrency>git push
Counting objects: 14, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (14/14), 1.36 KiB | 126.00 KiB/s, done.
Total 14 (delta 4), reused 0 (delta 0)
remote: Powered by Gitee.com
To gitee.com:lvyinhaolaiwu/concurrency.git
   42a1cd3..751c061  master -> master

C:UsersHONGZHENHUAimoocconcurrency>
原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/10033908.html