注解 自定义注解 获取某个包下有注解的类 判断方法上是否存在特定注解

步骤:
1.添加自定义注解类
2.类或方法或成员变量添加注解 @Pay(channelId = 1)
3.获取有注解的类或方法或成员 以及注解值 1

1.自定义注解类

package com.ruoyi.web.controller.pay;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)   //指定注解作用在什么上面。 type类  METHOD方法  field成员变量   PARAMETER参数  CONSTRUCTOR构造方法
@Retention(RetentionPolicy.SOURCE)   //指定注解声明周期
public @interface Pay {
    int channelId();
     boolean isStart() default false;
}

2.方法上或者类上添加注解

@Pay(channelId = 1)
public class ICBCBankImpl implements Strategy {
}

3.获取有指定注解的类 和有注解的值 (用于if…else…银行支付实现类 传channelId)

public class StrategyFactory {   
    static {
        //1.扫描impl包下的所有类
        Reflections reflections =new Reflections("com.ruoyi.web.controller.pay.impl");
        //2.impl包下类上有pay注解的类
       Set<Class<?>> classSet = reflections.getTypeAnnotatedWith(Pay.class);  //获取有指定注解的类
       //3.循环遍历set 
        for(Class clazz: classSet){  
            //获取指定注解
            Pay pay = (Pay) clazz.getAnnotation(Pay.class); 
            //获注解值  pay.name()  pay.type()
        }
    }

4.获取有注解的方法、方法上的指定注解、注解的值 (用与事务)

public static void main(String[] args) throws ClassNotFoundException {
    Class<?> clazz = Class.forName("com.springAop.method.User");
    //获取当前类所有方法
    Method[] declaredMethods = clazz.getDeclaredMethods();
    for (Method method:declaredMethods){
        //获取方法上的指定注解
        DIYTransaction dIYTransactionAnnotation = method.getDeclaredAnnotation(DIYTransaction.class);
        if (dIYTransactionAnnotation== null){
            continue;
        }
        //获取注解值age和name
        int age = dIYTransactionAnnotation.age();
        String name = dIYTransactionAnnotation.name();
  
        
       /*
       Sring切面
       public Object doBefore(ProceedingJoinPoint joinPoint) throws Throwable {
	        MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
	         //拿到切的方法
	        Method method = methodSignature.getMethod();                                  
	         //获取方法上的指定注解
	        MonitorRequest monitorRequestAnnotation = method.getAnnotation(MonitorRequest.class);      
	        //注解值isOnlyTokenC
	        boolean onlyTokenC = monitorRequestAnnotation .isOnlyTokenC();  
        } 
        */

    }
}

判断属性或者方法或者类上是否有指定注解Resource
field.isAnnotationPresent(Resource.class)

5,Eureka

public static void main(String[] args) throws Exception {
    Map<String,String> resultMap= new HashMap<>();
    Set<Class<?>> classes = ClassUtil.getClasses("com.test.web.dao");  //1.获取所有的类
    if (!classes.isEmpty()){
        for (Class clazz:classes){
            Annotation clazzDeclaredAnnotation = clazz.getDeclaredAnnotation(RpcClazz.class);
            if (clazzDeclaredAnnotation != null){   //2.类上有指定注解
                Method[] declaredMethods = clazz.getDeclaredMethods();
                for (Method method:declaredMethods){
                    //获取当前类有注解的方法
                    RpcMethod methodDeclaredAnnotation = method.getDeclaredAnnotation(RpcMethod.class);
                    if (methodDeclaredAnnotation != null){   //3.方法上有指定注解
                        resultMap.put(clazz.getSimpleName() + method.getName(),method.getName());
                    }
                }
            }
        }
    }
}

6,注解汇总

//以下五个的结果都是为spring容器为这个类创建Bean.

@Bean       注解告诉Spring这个方法将会返回一个对象,这个对象要注册为Spring应用上下文中的bean。 通常方法体中包含了最终产生bean实例的逻辑  
@Component  注解表明一个类会作为组件类,并告知Spring要为这个类创建bean  

@Controller   告知Spring要为这个类创建bean
@Service      告知Spring要为这个类创建bean
@Repository   告知Spring要为这个类创建bean
@mapper       告知Spring要为这个类创建bean

@autowireSpirng容器获取bean
@resourceSpirng容器获取bean

@Configuration

@FrameworkEndpoint@Controller类似



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飘然生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值