Search in sources :

Example 1 with BeanDefine

use of com.blade.ioc.bean.BeanDefine in project blade by biezhi.

the class BeanDefineTest method testBeanDefine2.

@Test
public void testBeanDefine2() {
    BeanDefine beanDefine = new BeanDefine(new BladeBeanDefineType(), BladeBeanDefineType.class);
    assertEquals(BladeBeanDefineType.class, beanDefine.getType());
    beanDefine = new BeanDefine(new BladeBeanDefineType(), BladeBeanDefineType.class, true);
    assertEquals(BladeBeanDefineType.class, beanDefine.getType());
    assertEquals(true, beanDefine.isSingleton());
}
Also used : BladeBeanDefineType(com.blade.types.BladeBeanDefineType) BeanDefine(com.blade.ioc.bean.BeanDefine) Test(org.junit.Test)

Example 2 with BeanDefine

use of com.blade.ioc.bean.BeanDefine in project blade by biezhi.

the class SimpleIoc method createBean.

@Override
public Object createBean(Class<?> type) {
    BeanDefine beanDefine = createBeanDefine(type, true);
    IocKit.initInjection(this, Objects.requireNonNull(beanDefine));
    IocKit.injectionValue(WebContext.blade().environment(), beanDefine);
    return beanDefine.getBean();
}
Also used : BeanDefine(com.blade.ioc.bean.BeanDefine)

Example 3 with BeanDefine

use of com.blade.ioc.bean.BeanDefine in project blade by biezhi.

the class SimpleIoc method put.

/**
 * Register @Bean marked objects
 */
private Object put(String name, Class<?> beanClass, boolean isSingleton) {
    BeanDefine beanDefine = this.createBeanDefine(beanClass, isSingleton);
    if (pool.put(name, beanDefine) != null) {
        log.warn("Duplicated Bean: {}", name);
    }
    // add interface态put to pool
    Class<?>[] interfaces = beanClass.getInterfaces();
    if (interfaces.length > 0) {
        for (Class<?> interfaceClazz : interfaces) {
            if (null != this.getBean(interfaceClazz)) {
                break;
            }
            this.put(interfaceClazz.getName(), beanDefine);
        }
    }
    return Objects.requireNonNull(beanDefine).getBean();
}
Also used : BeanDefine(com.blade.ioc.bean.BeanDefine)

Example 4 with BeanDefine

use of com.blade.ioc.bean.BeanDefine in project blade by biezhi.

the class NettyServer method initIoc.

private void initIoc() {
    RouteMatcher routeMatcher = blade.routeMatcher();
    routeMatcher.initMiddleware(blade.middleware());
    routeBuilder = new RouteBuilder(routeMatcher);
    blade.scanPackages().stream().flatMap(DynamicContext::recursionFindClasses).map(ClassInfo::getClazz).filter(ReflectKit::isNormalClass).forEach(this::parseAndCreate);
    routeMatcher.register();
    this.loaders.stream().sorted(new OrderComparator<>()).forEach(b -> b.preLoad(blade));
    this.processors.stream().sorted(new OrderComparator<>()).forEach(b -> b.preHandle(blade));
    Ioc ioc = blade.ioc();
    if (BladeKit.isNotEmpty(ioc.getBeans())) {
        log.info("{}Register bean: {}", getStartedSymbol(), ioc.getBeans());
    }
    List<BeanDefine> beanDefines = ioc.getBeanDefines();
    if (BladeKit.isNotEmpty(beanDefines)) {
        beanDefines.forEach(b -> {
            IocKit.initInjection(ioc, b);
            IocKit.injectionValue(environment, b);
            List<TaskStruct> cronExpressions = BladeKit.getTasks(b.getType(), environment);
            if (null != cronExpressions) {
                taskStruts.addAll(cronExpressions);
            }
        });
    }
    this.loaders.stream().sorted(new OrderComparator<>()).forEach(b -> b.load(blade));
    this.processors.stream().sorted(new OrderComparator<>()).forEach(b -> b.processor(blade));
}
Also used : RouteBuilder(com.blade.mvc.route.RouteBuilder) RouteMatcher(com.blade.mvc.route.RouteMatcher) TaskStruct(com.blade.task.TaskStruct) OrderComparator(com.blade.ioc.bean.OrderComparator) Ioc(com.blade.ioc.Ioc) BeanDefine(com.blade.ioc.bean.BeanDefine) ClassInfo(com.blade.ioc.bean.ClassInfo)

Example 5 with BeanDefine

use of com.blade.ioc.bean.BeanDefine in project blade by biezhi.

the class IocKit method injection.

public static void injection(Ioc ioc, BeanDefine beanDefine) {
    ClassDefine classDefine = ClassDefine.create(beanDefine.getType());
    List<FieldInjector> fieldInjectors = getInjectFields(ioc, classDefine);
    Object bean = beanDefine.getBean();
    fieldInjectors.forEach(fieldInjector -> {
        Object fieldInstance = ReflectKit.newInstance(fieldInjector.getType());
        if (fieldInjector.hasInjectFields()) {
            injection(ioc, new BeanDefine(fieldInstance));
        }
        fieldInjector.injection(bean, fieldInstance);
    });
}
Also used : ClassDefine(com.blade.ioc.bean.ClassDefine) FieldInjector(com.blade.ioc.bean.FieldInjector) BeanDefine(com.blade.ioc.bean.BeanDefine)

Aggregations

BeanDefine (com.blade.ioc.bean.BeanDefine)8 BladeBeanDefineType (com.blade.types.BladeBeanDefineType)2 Test (org.junit.Test)2 Ioc (com.blade.ioc.Ioc)1 ClassDefine (com.blade.ioc.bean.ClassDefine)1 ClassInfo (com.blade.ioc.bean.ClassInfo)1 FieldInjector (com.blade.ioc.bean.FieldInjector)1 OrderComparator (com.blade.ioc.bean.OrderComparator)1 RouteBuilder (com.blade.mvc.route.RouteBuilder)1 RouteMatcher (com.blade.mvc.route.RouteMatcher)1 TaskStruct (com.blade.task.TaskStruct)1