用于三方调用接口的统一入口,入口类GateWay扫描指定包下的service类,遍历该类中带自定义注解的方法,通过不同参数执行指定的方法。

1、自定义注解

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

    String WayName() default "空";
}

2、执行注解类

使用Reflections扫描包,所有带注解的类都写在com.holly.gateway.service包下。祥见代码

public class GateWay {

    private static final Logger logger = LoggerFactory.getLogger(GateWay.class);

    public ResponseInfo gateWay(RequestInfo requestInfo) {
        ResponseInfo responseInfo = new ResponseInfo();
        Reflections reflections = new Reflections("com.holly.gateway.service");//包名且不可忘记,不然扫描全部项目包,包括引用的jar
        //获取带Service注解的类
        Set<Class<?>> typesAnnotatedWith = reflections.getTypesAnnotatedWith(Service.class);
        for (Class clazz : typesAnnotatedWith
        ) {
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method : methods
            ) {
                //判断带自定义注解MyAnnontation的method
                if (method.isAnnotationPresent(MyAnnontation.class)) {
                    MyAnnontation annotation = method.getAnnotation(MyAnnontation.class);
                    //根据入参WayCode比较method注解上的WayCode,两者值相同才执行该method
                    if (null != annotation.WayCode() && annotation.WayCode().equals(requestInfo.getWayCode())) {
                        logger.info("WayCode = " + annotation.WayCode());
                        try {
                            //执行method
                            responseInfo = (ResponseInfo) method.invoke(clazz.newInstance(), requestInfo.getMap());
                        } catch (Exception e) {
                            logger.info("--------------执行自定义注解方法异常--------------");
                            e.printStackTrace();
                        }
                    }

                }

            }
        }
        return responseInfo;
    }

    public static void main(String[] args) {
        Map<String, Object> map = new HashMap();
        map.put("Key1", "参数1");
        map.put("Key2", "参数2");
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setWayCode("b");//自定义注解的WayCode值,调用不同的方法
        requestInfo.setWayName("调测试方法1");
        requestInfo.setMap(map);

        GateWay gate = new GateWay();
        ResponseInfo responseInfo = gate.gateWay(requestInfo);
        logger.info(responseInfo.toString());
    }
}

 

3、在方法上添加@MyAnnontation注解

在步骤2中main方法WayCode传值"b",则执行的是gateWayTwo方法

@Service
public class TestOne {

    @MyAnnontation(WayCode = "a",WayName = "测试类1")
    public ResponseInfo gateWayOne(Map<String, Object> map) {
        System.out.printf("gateWayOne printf" + map.get("Key1"));
        ResponseInfo res = new ResponseInfo();
        res.setTestId("000000");
        res.setTestName("U get Class gateWayOne");
        return res;
    }

    @MyAnnontation(WayCode = "b",WayName = "测试类2")
    public ResponseInfo gateWayTwo(Map<String, Object> map) {
        System.out.printf("gateWayOne printf" + map.get("Key2"));
        ResponseInfo res = new ResponseInfo();
        res.setTestId("000000");
        res.setTestName("U get Class gateWayTwo");
        return res;
    }

    @MyAnnontation
    public ResponseInfo gateWayThree(Map<String, Object> map) {
        System.out.printf("gateWayThree printf" + map.get("Key3"));
        ResponseInfo res = new ResponseInfo();
        res.setTestId("000000");
        res.setTestName("U get Class gateWayThree");
        return res;
    }

    public ResponseInfo gateWay4(Map<String, Object> map) {
        System.out.printf("gateWayThree printf" + map.get("Key4"));
        ResponseInfo res = new ResponseInfo();
        res.setTestId("000000");
        res.setTestName("U get Class gateWay4");
        return res;
    }
}

4、pom.xml

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.reflections/reflections -->
        <dependency>
            <groupId>org.reflections</groupId>
            <artifactId>reflections</artifactId>
            <version>0.9.11</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐