Search in sources :

Example 1 with Service

use of com.blade.ioc.annotation.Service 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)

Aggregations

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 Controller (com.blade.mvc.annotation.Controller)1 RestController (com.blade.mvc.annotation.RestController)1 RouteBuilder (com.blade.mvc.route.RouteBuilder)1