java annotation 注解初探究

Annotation介绍

Annotation是java中的一个注解,可以在sourceCode、Class、RUNTIME阶段,根据Annotation来干预代码的执行,包括生成代码。我们日常中碰到的@override 以及在Spring中碰到很多注解的使用。所以对注解的理解方便我们对整体架构的了解。

Annotation的例子

下面我们来下一个在运行时才会生效的Annotation。

定义一个annotation

package com.example.springbootdemo.annotation;

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

/**
 * @author admin
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface  LogTestAnnotation {
    
}

定义对应的注解处理器

package com.example.springbootdemo.ascept;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAnnotationAscept {

    @Pointcut("@annotation(com.example.springbootdemo.annotation.LogTestAnnotation)")
    public void logPointCut() {

    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        //执行方法
        Object result = point.proceed();
        //执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;

        //保存日志
        System.out.println("cost : " + time);

        return result;
    }
}

这个注解解析器有点奇怪,其实并没有针对注解做太多的事情,而且作为aop切入点的注解

注解的使用

package com.example.springbootdemo.bean;

import com.example.springbootdemo.annotation.LogTestAnnotation;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

@Service("aBean")
public class A {

    @LogTestAnnotation
    public int add(int a, int b) {
        return a + b;
    }
}

入口测试

package com.example.springbootdemo;

import com.example.springbootdemo.bean.A;
import org.springframework.beans.BeansException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.swing.*;

@SpringBootApplication
@Component
public class SpringbootdemoApplication implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    public static void main(String[] args) {
        SpringApplication.run(SpringbootdemoApplication.class, args);

        SpringbootdemoApplication instance = new SpringbootdemoApplication();
        System.out.println(instance.getApplicationContext());

        A a = (A)applicationContext.getBean("aBean");
        System.out.println(a.add(1 , 2));
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringbootdemoApplication.applicationContext = applicationContext;
    }

    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }
}

你可能感兴趣的:(java annotation 注解初探究)