spring之业务分离思想笔记

当我们在处理业务的时候,可以借助spring来实现业务分离

首先我们可以定义2个注解类

 

 
  1. /**

  2. *

  3. */

  4. package com.cn.common.core.annotion;

  5.  
  6. import java.lang.annotation.ElementType;

  7. import java.lang.annotation.Retention;

  8. import java.lang.annotation.RetentionPolicy;

  9. import java.lang.annotation.Target;

  10.  
  11. /**

  12. * 请求命令

  13. *

  14. */

  15. @Target(ElementType.METHOD)

  16. @Retention(RetentionPolicy.RUNTIME)

  17. public @interface SocketCommand {

  18.  
  19. /**

  20. * 请求的命令号

  21. * @return

  22. */

  23. short cmd();

  24.  
  25. }


 

 

 
  1. /**

  2. *

  3. */

  4. package com.cn.common.core.annotion;

  5.  
  6. import java.lang.annotation.ElementType;

  7. import java.lang.annotation.Retention;

  8. import java.lang.annotation.RetentionPolicy;

  9. import java.lang.annotation.Target;

  10.  
  11. /**

  12. * 请求模块

  13. *

  14. */

  15. @Target(ElementType.TYPE)

  16. @Retention(RetentionPolicy.RUNTIME)

  17. public @interface SocketModule {

  18.  
  19. /**

  20. * 请求的模块号

  21. * @return

  22. */

  23. short module();

  24. }

 

 

2我们可以在接口中使用该注解

 

 

 
  1. package com.cn.client.module.chat.handler;

  2. import com.cn.common.core.annotion.SocketCommand;

  3. import com.cn.common.core.annotion.SocketModule;

  4. import com.cn.common.module.ModuleId;

  5. import com.cn.common.module.chat.ChatCmd;

  6. import com.cn.common.module.chat.response.ChatResponse;

  7. /**

  8. * 聊天

  9. * @author -琴兽-

  10. *

  11. */

  12. @SocketModule(module = ModuleId.CHAT)

  13. public interface ChatHandler {

  14.  
  15. /**

  16. * 发送广播消息回包

  17. * @param resultCode

  18. * @param data {@link null}

  19. * @return

  20. */

  21. @SocketCommand(cmd = ChatCmd.PUBLIC_CHAT)

  22. public void publicChat(int resultCode, byte[] data);

  23.  
  24. /**

  25. * 发送私人消息回包

  26. * @param resultCode

  27. * @param data {@link null}

  28. * @return

  29. */

  30. @SocketCommand(cmd = ChatCmd.PRIVATE_CHAT)

  31. public void privateChat(int resultCode, byte[] data);

  32.  
  33. /**

  34. * 收到推送聊天信息

  35. * @param resultCode

  36. * @param data {@link ChatResponse}

  37. * @return

  38. */

  39. @SocketCommand(cmd = ChatCmd.PUSHCHAT)

  40. public void receieveMessage(int resultCode, byte[] data);

  41. }



3我们可以利用BeanPostProcessor来实现注解扫描

 

 

 
  1. package com.cn.client.scanner;

  2.  
  3. import java.lang.reflect.Method;

  4.  
  5. import org.springframework.beans.BeansException;

  6. import org.springframework.beans.factory.config.BeanPostProcessor;

  7. import org.springframework.stereotype.Component;

  8.  
  9. import com.cn.common.core.annotion.SocketCommand;

  10. import com.cn.common.core.annotion.SocketModule;

  11. /**

  12. * handler扫描器

  13. *

  14. */

  15. @Component

  16. public class HandlerScaner implements BeanPostProcessor {

  17.  
  18. @Override

  19. public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

  20. return bean;

  21. }

  22.  
  23. @Override

  24. public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

  25.  
  26. Class<? extends Object> clazz = bean.getClass();

  27.  
  28. Class<?>[] interfaces = clazz.getInterfaces();

  29.  
  30. if(interfaces != null && interfaces.length > 0){

  31. //扫描类的所有接口父类

  32. for (Class<?> interFace : interfaces) {

  33. //判断是否为handler接口类

  34. SocketModule socketModule = interFace.getAnnotation(SocketModule.class);

  35. if (socketModule == null) {

  36. continue;

  37. }

  38.  
  39. //找出命令方法

  40. Method[] methods = interFace.getMethods();

  41. if(methods != null && methods.length > 0){

  42. for(Method method : methods){

  43. SocketCommand socketCommand = method.getAnnotation(SocketCommand.class);

  44. if(socketCommand == null){

  45. continue;

  46. }

  47.  
  48. final short module = socketModule.module();

  49. final short cmd = socketCommand.cmd();

  50.  
  51. if(InvokerHoler.getInvoker(module, cmd) == null){

  52. InvokerHoler.addInvoker(module, cmd, Invoker.valueOf(method, bean));

  53. }else{

  54. System.out.println("重复命令:"+"module:"+module +" "+"cmd:" + cmd);

  55. }

  56. }

  57. }

  58.  
  59. }

  60. }

  61. return bean;

  62. }

  63.  
  64. }

 

 
  1. package com.cn.client.scanner;

  2.  
  3. import java.lang.reflect.InvocationTargetException;

  4. import java.lang.reflect.Method;

  5. /**

  6. * 命令执行器

  7. *

  8. */

  9. public class Invoker {

  10.  
  11. /**

  12. * 方法

  13. */

  14. private Method method;

  15.  
  16. /**

  17. * 目标对象

  18. */

  19. private Object target;

  20.  
  21. public static Invoker valueOf(Method method, Object target){

  22. Invoker invoker = new Invoker();

  23. invoker.setMethod(method);

  24. invoker.setTarget(target);

  25. return invoker;

  26. }

  27.  
  28. /**

  29. * 执行

  30. * @param paramValues

  31. * @return

  32. * @throws InvocationTargetException

  33. * @throws IllegalArgumentException

  34. * @throws IllegalAccessException

  35. */

  36. public Object invoke(Object... paramValues){

  37. try {

  38. return method.invoke(target, paramValues);

  39. } catch (IllegalAccessException e) {

  40. e.printStackTrace();

  41. } catch (IllegalArgumentException e) {

  42. e.printStackTrace();

  43. } catch (InvocationTargetException e) {

  44. e.printStackTrace();

  45. }

  46. return null;

  47. }

  48.  
  49. public Method getMethod() {

  50. return method;

  51. }

  52.  
  53. public void setMethod(Method method) {

  54. this.method = method;

  55. }

  56.  
  57. public Object getTarget() {

  58. return target;

  59. }

  60.  
  61. public void setTarget(Object target) {

  62. this.target = target;

  63. }

  64. }

 

 
  1. package com.cn.client.scanner;

  2.  
  3. import java.util.HashMap;

  4. import java.util.Map;

  5. /**

  6. * 命令执行器管理者

  7. *

  8. */

  9. public class InvokerHoler {

  10.  
  11. /**命令调用器*/

  12. private static Map<Short, Map<Short, Invoker>> invokers = new HashMap<>();

  13.  
  14. /**

  15. * 添加命令调用

  16. * @param module

  17. * @param cmd

  18. * @param invoker

  19. */

  20. public static void addInvoker(short module, short cmd, Invoker invoker){

  21. Map<Short, Invoker> map = invokers.get(module);

  22. if(map == null){

  23. map = new HashMap<>();

  24. invokers.put(module, map);

  25. }

  26. map.put(cmd, invoker);

  27. }

  28.  
  29.  
  30. /**

  31. * 获取命令调用

  32. * @param module

  33. * @param cmd

  34. * @param invoker

  35. */

  36. public static Invoker getInvoker(short module, short cmd){

  37. Map<Short, Invoker> map = invokers.get(module);

  38. if(map != null){

  39. return map.get(cmd);

  40. }

  41. return null;

  42. }

  43.  
  44. }


接下来可以试试了

 

 

 
  1. package com.cn.client;

  2.  
  3.  
  4. import org.springframework.context.ApplicationContext;

  5. import org.springframework.context.support.ClassPathXmlApplicationContext;

  6.  
  7.  
  8. import com.cn.client.swing.Swingclient;

  9.  
  10.  
  11. /**

  12. * 启动函数

  13. *

  14. */

  15. public class ClientMain {

  16.  
  17.  
  18. @SuppressWarnings("resource")

  19. public static void main(String[] args) {

  20. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

  21.  
  22. Swingclient swing = applicationContext.getBean(Swingclient.class);

  23. swing.setVisible(true);

  24. }

  25.  
  26.  
  27. }

ok ,这种思想很好的,特此记录下

https://blog.csdn.net/jiangzhexi/article/details/61661675

https://blog.csdn.net/woshixuye/article/details/54705884

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值