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

本文详细介绍如何在SpringBoot项目中设置启动前执行特定方法,包括自定义注解、反射扫描包内方法及其实现流程。通过实例演示了如何利用自定义注解和反射技术实现方法的动态调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值