步骤:
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
@autowire 从Spirng容器获取bean
@resource 从Spirng容器获取bean
@Configuration
@FrameworkEndpoint 和@Controller类似