com.blade.ioc.annotation.Bean Java Examples

The following examples show how to use com.blade.ioc.annotation.Bean. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SimpleIoc.java    From blade with Apache License 2.0 5 votes vote down vote up
/**
 * Register @Bean marked objects
 */
@Override
public <T> T addBean(Class<T> type) {
    Bean    beanAnnotation = type.getAnnotation(Bean.class);
    boolean isSingleton    = null == beanAnnotation || beanAnnotation.singleton();
    if (isSingleton) {
        Object bean = put(type, true);
        return type.cast(bean);
    }
    return null;
}
 
Example #2
Source File: ClassDefineTest.java    From blade with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassDefine() {
    ClassDefine       classDefine = ClassDefine.create(BladeClassDefineType.class);
    int               modifires   = classDefine.getModifiers();
    Field[]           fields      = classDefine.getDeclaredFields();
    Bean              bean        = classDefine.getAnnotation(Bean.class);
    Annotation[]      annotations = classDefine.getAnnotations();
    List<ClassDefine> interfaces  = classDefine.getInterfaces();
    String            name        = classDefine.getName();
    String            simpleName  = classDefine.getSimpleName();
    ClassDefine       superKlass  = classDefine.getSuperKlass();
    Class<?>          type        = classDefine.getType();

    assertEquals(Modifier.PUBLIC, modifires);
    assertEquals(2, fields.length);
    assertNotNull(bean);
    assertEquals(1, annotations.length);
    assertEquals(0, interfaces.size());
    assertEquals("com.blade.types.BladeClassDefineType", name);
    assertEquals("BladeClassDefineType", simpleName);
    assertEquals(Object.class, superKlass.getType());
    assertEquals(BladeClassDefineType.class, type);
    assertEquals(false, classDefine.isAbstract());
    assertEquals(false, classDefine.isInterface());
    assertEquals(false, classDefine.isStatic());
    assertEquals(true, classDefine.isPublic());
    assertEquals(false, classDefine.isPrivate());
    assertEquals(false, classDefine.isProtected());

    assertEquals(1, ClassDefine.create(BladeWebHookType.class).getInterfaces().size());
    assertEquals(1, ClassDefine.create(BladeWebHookType.class).getInterfaces().size());

}
 
Example #3
Source File: NettyServer.java    From blade with Apache License 2.0 4 votes vote down vote up
private void parseAndCreate(Class<?> clazz) {
    if (null != clazz.getAnnotation(Bean.class) || null != clazz.getAnnotation(Value.class)) {
        blade.register(clazz);
    }
    if (null != clazz.getAnnotation(Path.class)) {
        if (null == blade.getBean(clazz)) {
            blade.register(clazz);
        }
        Object controller = blade.getBean(clazz);
        routeBuilder.addRouter(clazz, controller);
    }
    if (null != clazz.getAnnotation(Configuration.class) && clazz.getMethods().length > 0) {
        Object config = ReflectKit.newInstance(clazz);
        Arrays.stream(clazz.getMethods())
                .filter(m -> m.getAnnotation(Bean.class) != null)
                .forEach(n -> {
                    try {
                        blade.register(n.invoke(config));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
    }
    if (ReflectKit.hasInterface(clazz, WebHook.class) && null != clazz.getAnnotation(Bean.class)) {
        URLPattern URLPattern = clazz.getAnnotation(URLPattern.class);
        if (null == URLPattern) {
            routeBuilder.addWebHook(clazz, "/.*");
        } else {
            Stream.of(URLPattern.values())
                    .forEach(pattern -> routeBuilder.addWebHook(clazz, pattern));
        }
    }

    if (ReflectKit.hasInterface(clazz, BladeLoader.class) && null != clazz.getAnnotation(Bean.class)) {
        this.loaders.add((BladeLoader) blade.getBean(clazz));
    }
    if (ReflectKit.hasInterface(clazz, BeanProcessor.class) && null != clazz.getAnnotation(Bean.class)) {
        this.processors.add((BeanProcessor) blade.getBean(clazz));
    }
    if (isExceptionHandler(clazz)) {
        ExceptionHandler exceptionHandler = (ExceptionHandler) blade.getBean(clazz);
        blade.exceptionHandler(exceptionHandler);
    }
    WebSocket webSocket;
    if (null != (webSocket = clazz.getAnnotation(WebSocket.class))) {
        if (null == blade.getBean(clazz)) {
            blade.register(clazz);
        }
        if (ReflectKit.hasInterface(clazz, WebSocketHandler.class)) {
            blade.webSocket(webSocket.value(), (WebSocketHandler) blade.getBean(clazz));
        } else {
            WebSocketHandlerWrapper wrapper = blade.getBean(WebSocketHandlerWrapper.class);
            if (wrapper == null) {
                wrapper = WebSocketHandlerWrapper.init(blade);
                blade.register(wrapper);
            }
            blade.webSocket(webSocket.value(), wrapper);
            wrapper.wrapHandler(webSocket.value(), clazz);
        }

    }
}
 
Example #4
Source File: NettyServer.java    From blade with Apache License 2.0 4 votes vote down vote up
private boolean isExceptionHandler(Class<?> clazz) {
    return (null != clazz.getAnnotation(Bean.class) && (
            ReflectKit.hasInterface(clazz, ExceptionHandler.class) || clazz.getSuperclass().equals(DefaultExceptionHandler.class)));
}