use of com.blade.mvc.annotation.Controller 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.Controller in project blade by biezhi.
the class RouteBuilder method addRouter.
/**
* Parse all routing in a controller
*
* @param router resolve the routing class
*/
public void addRouter(final Class<?> router) {
Method[] methods = router.getMethods();
if (null == methods || methods.length == 0) {
return;
}
String nameSpace = null, suffix = null;
if (null != router.getAnnotation(Controller.class)) {
nameSpace = router.getAnnotation(Controller.class).value();
suffix = router.getAnnotation(Controller.class).suffix();
}
if (null != router.getAnnotation(RestController.class)) {
nameSpace = router.getAnnotation(RestController.class).value();
suffix = router.getAnnotation(RestController.class).suffix();
}
if (null == nameSpace) {
LOGGER.warn("Route [{}] not controller annotation", router.getName());
return;
}
for (Method method : methods) {
Route mapping = method.getAnnotation(Route.class);
//route method
if (null != mapping) {
// build multiple route
HttpMethod methodType = mapping.method();
String[] paths = mapping.values();
if (mapping.value().length > 1 || !mapping.value()[0].equals("/")) {
paths = mapping.value();
}
if (paths.length > 0) {
for (String path : paths) {
String pathV = getRoutePath(path, nameSpace, suffix);
this.buildRoute(router, method, pathV, methodType);
}
}
}
}
}
Aggregations