use of com.blade.mvc.annotation.RestController in project blade by biezhi.
the class IocApplication method initBeans.
public void initBeans() throws Exception {
Set<String> pkgs = blade.bConfig().getPackages();
if (null != pkgs) {
Ioc ioc = blade.ioc();
RouteBuilder routeBuilder = blade.routeBuilder();
List<BeanProcessor> processors = CollectionKit.newArrayList();
List<ClassInfo> ctxClasses = CollectionKit.newArrayList(8);
List<ClassInfo> processoers = CollectionKit.newArrayList(8);
pkgs.forEach(p -> {
ClassReader classReader = DynamicContext.getClassReader(p);
Set<ClassInfo> classInfos = classReader.getClass(p, true);
if (null != classInfos) {
classInfos.forEach(c -> {
Class<?> clazz = c.getClazz();
if (!clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers())) {
Service service = clazz.getAnnotation(Service.class);
Controller controller = clazz.getAnnotation(Controller.class);
RestController restController = clazz.getAnnotation(RestController.class);
Component component = clazz.getAnnotation(Component.class);
if (null != service || null != component) {
ioc.addBean(clazz);
} else if (null != controller || null != restController) {
ioc.addBean(clazz);
routeBuilder.addRouter(clazz);
} else if (clazz.getSuperclass().getName().equals("com.blade.aop.AbstractMethodInterceptor")) {
aopInterceptors.add(ReflectKit.newInstance(clazz));
} else {
Class<?>[] interfaces = clazz.getInterfaces();
for (Class<?> in : interfaces) {
if (in.equals(Interceptor.class)) {
ioc.addBean(clazz);
routeBuilder.addInterceptor(clazz);
} else if (in.equals(WebContextListener.class)) {
ctxClasses.add(c);
} else if (in.equals(BeanProcessor.class)) {
processoers.add(c);
}
}
}
}
});
}
});
ctxClasses.sort(orderComparator);
processoers.sort(orderComparator);
ctxClasses.forEach(c -> {
Object bean = ioc.addBean(c.getClazz());
ctxs.add((WebContextListener) bean);
});
processoers.forEach(c -> {
Object bean = ioc.addBean(c.getClazz());
processors.add((BeanProcessor) bean);
});
processors.forEach(b -> b.register(ioc));
if (null != ioc.getBeans() && !ioc.getBeans().isEmpty()) {
LOGGER.info("Add Object: {}", ioc.getBeans());
}
List<BeanDefine> beanDefines = ioc.getBeanDefines();
if (null != beanDefines) {
beanDefines.forEach(b -> IocKit.injection(ioc, b));
}
}
}
use of com.blade.mvc.annotation.RestController in project blade by biezhi.
the class RouteViewResolve method handle.
public void handle(Request request, Response response, Route route) throws Exception {
try {
Method actionMethod = route.getAction();
Object target = route.getTarget();
int len = actionMethod.getParameterTypes().length;
Object returnParam;
if (len > 0) {
Object[] args = MethodArgument.getArgs(request, response, actionMethod);
returnParam = ReflectKit.invokeMehod(target, actionMethod, args);
} else {
returnParam = ReflectKit.invokeMehod(target, actionMethod);
}
if (null != returnParam) {
Class<?> returnType = returnParam.getClass();
RestController restController = target.getClass().getAnnotation(RestController.class);
JSON json = actionMethod.getAnnotation(JSON.class);
if (null != restController || null != json) {
response.json(viewSettings.toJSONString(returnParam));
} else {
if (returnType == String.class) {
response.render(returnParam.toString());
} else if (returnType == ModelAndView.class) {
ModelAndView modelAndView = (ModelAndView) returnParam;
response.render(modelAndView);
}
}
}
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new BladeException(e.getCause());
} catch (Exception e) {
throw e;
}
}
Aggregations