springboot扫描⾃定义类注解、⽅法注解
青草伊伊import lombok.Data;
import t.ApplicationListener;
import t.event.ContextRefreshedEvent;
import annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import flect.InvocationTargetException;
import flect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/
**
凉拌青椒丝* @ClassName: CustomAnnotationDemo
* @Description: ⾃定义类注解、⽅法注解demo
* @Author: RuiXin Yu
* @Date: 2019/3/21 10:35
*/
@Component
public class CustomAnnotationDemo implements ApplicationListener<ContextRefreshedEvent>{
public static List<Invoker> invokerList =new ArrayList<>();
@Override
public void onApplicationEvent(ContextRefreshedEvent event){
/
/ 根容器为Spring容器
ApplicationContext().getParent()==null){
Map<String,Object> beans = ApplicationContext().getBeansWithAnnotation(OnsConsumer.class);
for(Object bean : beans.values()){
OnsConsumer ca = Class().getAnnotation(OnsConsumer.class);
小鲨鱼怎么做好吃System.out.Class().getName()+"==="+ca.value());
Method[] methods = Class().getMethods();
for(Method declaredMethod : methods){
面麻
System.out.Name());
ConsumerMapping ma = AnnotationUtils.findAnnotation(declaredMethod, ConsumerMapping.class);
if(ma != null){
买丝绣作平原君
invokerList.add(new Invoker(declaredMethod,ma.value(),bean));
System.out.Class().getName()+"==="+ca.value()+"==="+ma.value());
}
}
Method[] declaredMethods = Class().getDeclaredMethods();
for(Method declaredMethod : declaredMethods){
System.out.Name());
ConsumerMapping ma = Annotation(ConsumerMapping.class);
if(ma != null){
System.out.Class().getName()+"==="+ca.value()+"==="+ma.value());
}
}
}
}
invoke();
含泪奔跑的少年>奇妙五福星}
private void invoke(){
for(Invoker invoker : invokerList){
try{
}catch(IllegalAccessException e){
e.printStackTrace();
}catch(InvocationTargetException e){
e.printStackTrace();
}
}
晨昏定省}
@Data
class Invoker{
private Method method;
private String tag;
private Object bean;
public Invoker(Method method, String tag, Object bean){
this.tag = tag;
this.bean = bean;
}
}
}