Search in sources :

Example 1 with Controller

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));
        }
    }
}
Also used : RouteBuilder(com.blade.mvc.route.RouteBuilder) Service(com.blade.ioc.annotation.Service) RestController(com.blade.mvc.annotation.RestController) Controller(com.blade.mvc.annotation.Controller) RestController(com.blade.mvc.annotation.RestController) ClassReader(com.blade.kit.resource.ClassReader) Component(com.blade.ioc.annotation.Component) WebContextListener(com.blade.context.WebContextListener) ClassInfo(com.blade.kit.resource.ClassInfo)

Example 2 with Controller

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);
                }
            }
        }
    }
}
Also used : RestController(com.blade.mvc.annotation.RestController) HttpMethod(com.blade.mvc.http.HttpMethod) Method(java.lang.reflect.Method) RestController(com.blade.mvc.annotation.RestController) Controller(com.blade.mvc.annotation.Controller) Route(com.blade.mvc.annotation.Route) HttpMethod(com.blade.mvc.http.HttpMethod)

Aggregations

Controller (com.blade.mvc.annotation.Controller)2 RestController (com.blade.mvc.annotation.RestController)2 WebContextListener (com.blade.context.WebContextListener)1 Component (com.blade.ioc.annotation.Component)1 Service (com.blade.ioc.annotation.Service)1 ClassInfo (com.blade.kit.resource.ClassInfo)1 ClassReader (com.blade.kit.resource.ClassReader)1 Route (com.blade.mvc.annotation.Route)1 HttpMethod (com.blade.mvc.http.HttpMethod)1 RouteBuilder (com.blade.mvc.route.RouteBuilder)1 Method (java.lang.reflect.Method)1