认识java自定义注解简单例子

public class FTPClientConfig {

    private String host;

    private int port;

    private String username;

    private String password;

    private String passiveMode;

    private String encoding;

    private int clientTimeout;

    private int bufferSize;

    private int transferFileType;

    private boolean renameUploaded;

    private int retryTime;
    @MyAnnotation(value = "aaaaa",pointcut="222")
    public String getHost() {
        return host;
    }

    public void setHost(@MyAnnotation("bbbb") String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassiveMode() {
        return passiveMode;
    }

    public void setPassiveMode(String passiveMode) {
        this.passiveMode = passiveMode;
    }

    public String getEncoding() {
        return encoding;
    }

    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    public int getClientTimeout() {
        return clientTimeout;
    }

    public void setClientTimeout(int clientTimeout) {
        this.clientTimeout = clientTimeout;
    }

    public int getBufferSize() {
        return bufferSize;
    }

    public void setBufferSize(int bufferSize) {
        this.bufferSize = bufferSize;
    }

    public int getTransferFileType() {
        return transferFileType;
    }

    public void setTransferFileType(int transferFileType) {
        this.transferFileType = transferFileType;
    }

    public boolean isRenameUploaded() {
        return renameUploaded;
    }

    public void setRenameUploaded(boolean renameUploaded) {
        this.renameUploaded = renameUploaded;
    }

    public int getRetryTime() {
        return retryTime;
    }

    public void setRetryTime(int retryTime) {
        this.retryTime = retryTime;
    }
}
  • 创建自定义注解类

 

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Locale;

@Target(value={ElementType.METHOD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
    String value() default "";

    String pointcut() default "";

    String returning() default "";

    String argNames() default "";
}

class AnnotationTest {

    public static void main(String[] args) throws Exception {
        // 新建Person
        FTPClientConfig person = new FTPClientConfig();
        // 获取Person的Class实例
        Class<FTPClientConfig> c = FTPClientConfig.class;
        Method method = c.getMethod("getHost", new Class[]{});
        // 判断 somebody() 方法是否包含MyAnnotation注解
        if(method.isAnnotationPresent(MyAnnotation.class)){
            System.out.println("------");
            // 获取该方法的MyAnnotation注解实例
            MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
            // 获取 myAnnotation的值,并打印出来

            System.out.println(myAnnotation.value()+myAnnotation.pointcut());
        }else {
            System.out.println(method);
        }
        Locale locale = new Locale("de-","DE-");

        NumberFormat numberFormat = NumberFormat.getInstance(locale);
        System.out.println(numberFormat.format(123444.233));

        System.out.println(MessageFormat.format("我是1 {0} {2}","1",3,3));
        System.out.println(System.getSecurityManager());
    }
}

Guess you like

Origin blog.csdn.net/ding43930053/article/details/118961071