springboot:自定义注解执行指定的类方法

1、搭建springboot项目,并且初始化,

2、设置springboot项目启动之前执行方法

@Configuration
public class ActionManager {

	/**
	 * 	扫描包
	 */
	@PostConstruct
	private void scan() {

    }
}

3、编写自定义函数

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.*;
@Target({ElementType.FIELD, ElementType.METHOD}) //声明自定义的注解使用在方法上
@Retention(RetentionPolicy.RUNTIME)//注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在
@Documented
public @interface BaseAction {
	String name() default "";
	int actionCode() default 0;
}

4、编写要启动的方法以及类

@Configuration
public class ActionManager {

	/**
	 * 	扫描包
	 */
	@PostConstruct
	private void scan() {
        Message message=new Message(doActionUrl);
		Map<String ,Object> map = new HashMap<String ,Object>();
        byte byteLongTtpe=(byte)0;
		map.put("LOGIN_TYPE", byteLongTtpe);
		message.setDataMap(map);
		ActionManager actionManager=new ActionManager();
		message=actionManager.doAction(doActionUrl, message);
    }
    public Message doAction(String doActionUrl,Message message) {
         Reflections reflections = new Reflections("com.xxxx.xx");//            包名且不可忘记,不然扫描全部项目包,包括引用的jar,填写所在的包名称!!!!
	     //获取带Service注解的类
		 Set<Class<?>> typesAnnotatedWith = reflections.getTypesAnnotatedWith(Service.class);
	       for (Class clazz : typesAnnotatedWith) {
	            Method[] methods = clazz.getDeclaredMethods();
	            for (Method method : methods) {
	                //判断带自定义注解BaseAction的method
	                if (method.isAnnotationPresent(BaseAction.class)) {
	                	BaseAction baseAction = method.getAnnotation(BaseAction.class);
	                    //根据入参name比较method注解上的name,两者值相同才执行该method
	                    if (null != baseAction.name() && baseAction.name().equals(doActionUrl)) {
	                        try {
	                            //执行method
	                        	message = (Message) method.invoke(clazz.newInstance(), message);
	                        } catch (Exception e) {
	                            e.printStackTrace();
	                        }
	                    }
	 
	                }
	          }
	     }
		return message;
    }
}

5、编写实体类

import java.util.Map;
public class Message {
	
	private Map<String,Object> dataMap ;
	private byte[] data ;
	private String actionUrl;
	public Message(String actionUrl) {
		this.actionUrl = actionUrl ;
	}
	
	
	public Message() {
		super();
	}


	public Message(Map<String, Object> dataMap, byte[] data, String actionUrl) {
		super();
		this.dataMap = dataMap;
		this.data = data;
		this.actionUrl = actionUrl;
	}
    public byte[] getData() {
		return data;
	}


	public void setData(byte[] data) {
		this.data = data;
	}


	public String getActionUrl() {
		return actionUrl;
	}


	public void setActionUrl(String actionUrl) {
		this.actionUrl = actionUrl;
	}


	public Message(byte[]data) {
		this.data = data ;
		calcMap();
	}

	public Message(int actionId,Map<String,Object> dataMap) {
		this.dataMap = dataMap ;
		calcByte();
	}
}

 6.编写要执行的方法以及类

import java.util.Map;

import org.springframework.stereotype.Service;

@Service
public class LoginAction {
	
	@BaseAction(name="LoginAction",actionCode=0x01)
	public Message doAction(Message msg) {
		Map<String ,Object> map = msg.toMap();
		System.out.println("I Am LoginAction");
		return msg;
	}
}
@Service
public class LogoutAction {
	@BaseAction(name="LogoutAction",actionCode=0x02)
	public Message doAction(Message msg) {
		Map<String ,Object> map = msg.toMap();
		System.out.println("I Am LogoutAction");
		return msg;
	}
}

7、pom.xml所带的jar

        <dependency>
            <groupId>org.reflections</groupId>
            <artifactId>reflections</artifactId>
            <version>0.9.11</version>
        </dependency>

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,没有明确说明Spring Boot自定义注解执行顺序。但是,我们可以通过以下步骤来实现自定义注解执行顺序: 1. 定义多个自定义注解,并为每个注解指定一个执行顺序的。 2. 创建一个注解处理器,使用@Order注解指定该处理器的执行顺序。 3. 在注解处理器中,使用@Priority注解指定处理器的执行顺序。 4. 在处理器中,使用@Around注解指定处理器的执行方法,并在方法中使用ProceedingJoinPoint参数来控制注解执行顺序。 下面是一个示例代码,演示了如何实现自定义注解执行顺序: ```java @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface FirstAnnotation { int order() default 1; } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface SecondAnnotation { int order() default 2; } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface ThirdAnnotation { int order() default 3; } @Aspect @Component public class AnnotationAspect { @Around("@annotation(com.example.demo.FirstAnnotation) || @annotation(com.example.demo.SecondAnnotation) || @annotation(com.example.demo.ThirdAnnotation)") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); Annotation[] annotations = method.getAnnotations(); Arrays.sort(annotations, new Comparator<Annotation>() { @Override public int compare(Annotation o1, Annotation o2) { int order1 = getOrder(o1); int order2 = getOrder(o2); return order1 - order2; } private int getOrder(Annotation annotation) { if (annotation instanceof FirstAnnotation) { return ((FirstAnnotation) annotation).order(); } else if (annotation instanceof SecondAnnotation) { return ((SecondAnnotation) annotation).order(); } else if (annotation instanceof ThirdAnnotation) { return ((ThirdAnnotation) annotation).order(); } return 0; } }); for (Annotation annotation : annotations) { System.out.println(annotation.annotationType().getSimpleName() + " is executed."); } return joinPoint.proceed(); } } ``` 在上面的代码中,我们定义了三个自定义注解:FirstAnnotation、SecondAnnotation和ThirdAnnotation,并为每个注解指定了一个执行顺序的。然后,我们创建了一个注解处理器AnnotationAspect,并使用@Order注解指定了该处理器的执行顺序。在处理器中,我们使用@Around注解指定了处理器的执行方法,并在方法中使用ProceedingJoinPoint参数来控制注解执行顺序。最后,我们使用Arrays.sort方法注解进行排序,并按照指定执行顺序依次执行注解

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值