1.支持扫描某个包下的所有类,进行处理。
private static final String MSG_PATH = "com.test.msg.http"; private static final String RESOURCE_PATH = "classpath*:" + StringUtils.replace(MSG_PATH, ".", "/") + "/*.class"; private Map<String, String> funMap = new HashMap<String, String>(); public void init() throws ServiceException { logger.info("注册消息初始化开始"); ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); try { Resource[] resources = resourcePatternResolver.getResources(RESOURCE_PATH); MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver); for (Resource e : resources) { MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(e); String clazzName = ClassUtils.getClass(metadataReader.getClassMetadata().getClassName()).getSimpleName();; if (StringUtils.endsWith(clazzName, "C2S_Msg")) { String funcName = StringUtils.uncapitalize(StringUtils.substringBefore(clazzName, "_")); funMap.put(clazzName, funcName); } } } catch (Exception e) { throw new ServiceException(e); } logger.info("注册消息初始化完毕"); }
public String getFuncName(Object msg) { return funMap.get(msg.getClass().getSimpleName()); }