Java注解使用MVC控制
1、注解文件
a)SocketCommand.java
/** * */ package com.example.netty.lesson11.serviceSeparate.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 请求命令 * @author yangwj * */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface SocketCommand { /** * 请求的命令号 * @return */ short cmd(); }
b) SocketModule.java
/** * */ package com.example.netty.lesson11.serviceSeparate.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 请求模块 * @author yangwj * */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface SocketModule { /** * 请求的模块号 * @return */ short module(); }
2、service服务
a)UserService.java
package com.example.netty.lesson11.serviceSeparate.service; import com.example.netty.lesson11.serviceSeparate.annotation.SocketCommand; import com.example.netty.lesson11.serviceSeparate.annotation.SocketModule; /** * @author yangwj * @date 2020/4/6 16:12 */ @SocketModule(module = 1) public interface UserService { /** * 登陆 */ @SocketCommand(cmd = 1) public void login(); @SocketCommand(cmd = 2) public void getInfo(); }
b)UserServiceImpl.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.example.netty.lesson11.serviceSeparate.service.serviceImpl; import com.example.netty.lesson11.serviceSeparate.service.UserService; import org.springframework.stereotype.Component; /** * @author yangwj * @date 2020/4/6 16:15 */ @Component public class UserServiceImpl implements UserService { @Override public void login() { System.out.println("login"); } @Override public void getInfo() { System.out.println("getInfo"); } }
3、执行器Invoker.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.example.netty.lesson11.serviceSeparate; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * 执行器 * @author yangwj * @date 2020/4/6 16:36 */ public class Invoker { /** * 目标对象 */ private Object target; /** * 方法 */ private Method method; public static Invoker valueOf(Object target,Method method){ Invoker invoker = new Invoker(); invoker.setTarget(target); invoker.setMethod(method); return invoker; } public Object invoke(Object[] args){ try { method.invoke(target,args); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return null; } public Object getTarget() { return target; } public void setTarget(Object target) { this.target = target; } public Method getMethod() { return method; } public void setMethod(Method method) { this.method = method; } }
4、执行器管理器InvokerHolder.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.example.netty.lesson11.serviceSeparate; import java.util.HashMap; import java.util.Map; /** * 执行器管理者 * @author yangwj * @date 2020/4/6 16:41 */ public class InvokerHolder { public static Map<Short, Map<Short,Invoker>> invokers = new HashMap<>(); /** * 添加一个执行器 * @param module * @param cmd * @param invoker */ public static void addInvoker(short module,short cmd ,Invoker invoker){ Map<Short,Invoker> map = invokers.get(module); if(map == null){ map = new HashMap<>(); invokers.put(module,map); } map.put(cmd,invoker); } public static Invoker getInvoker(short module,short cmd){ Map<Short,Invoker> map = invokers.get(module); if(map != null){ return map.get(cmd); } return null; } }
5、注解扫描器
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.example.netty.lesson11.serviceSeparate; import com.example.netty.lesson11.serviceSeparate.annotation.SocketCommand; import com.example.netty.lesson11.serviceSeparate.annotation.SocketModule; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.stereotype.Component; import java.lang.reflect.Method; /** * 扫描器 * @author yangwj * @date 2020/4/6 16:16 */ @Component public class Scanner implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { Class<? extends Object> clazz = bean.getClass(); Class<?>[] interfaces = clazz.getInterfaces(); if(interfaces != null && interfaces.length >0 ){ //扫描所有的接口 for(Class<?> interFace : interfaces){ SocketModule socketModule = interFace.getAnnotation(SocketModule.class); if(socketModule == null){ continue; } Method[] methods = interFace.getMethods(); if(methods!=null && methods.length>0){ for (Method method:methods) { SocketCommand socketCommand = method.getAnnotation(SocketCommand.class); if(socketCommand == null){ continue; } short module = socketModule.module(); short cmd = socketCommand.cmd(); Invoker invoker = Invoker.valueOf(bean,method); if(InvokerHolder.getInvoker(module,cmd) == null){ InvokerHolder.addInvoker(module,cmd,invoker); }else { System.out.println("重复注册执行器module:"+ module +"---cmd:"+cmd); } } } } } return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } }
6、主程序执行
package com.example.netty; import com.example.netty.lesson11.serviceSeparate.Invoker; import com.example.netty.lesson11.serviceSeparate.InvokerHolder; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class NettyApplication { public static void main(String[] args) { SpringApplication.run(NettyApplication.class, args); Invoker invoker = InvokerHolder.getInvoker((short)2, (short)1); invoker.invoke(null); } }
完毕!
GitHub地址:https://github.com/812406210/Netty-study.git
本文来自博客园,作者:小白啊小白,Fighting,转载请注明原文链接:https://www.cnblogs.com/ywjfx/p/12740163.html