Search in sources :

Example 31 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder in project vertx-swagger by bobxwang.

the class SwaggerApp method Run.

public static AnnotationConfigApplicationContext Run(Class<?> clasz) {
    final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(clasz);
    BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(ApplicationContextHolder.class);
    applicationContext.registerBeanDefinition("applicationContextHolder", bdb.getBeanDefinition());
    // Just For Init
    applicationContext.getBean(ApplicationContextHolder.class);
    final Environment environment = applicationContext.getBean(Environment.class);
    final Vertx vertx = applicationContext.getBean(Vertx.class);
    final SpringVerticleFactory verticleFactory = new SpringVerticleFactory();
    vertx.registerVerticleFactory(verticleFactory);
    try {
        applicationContext.getBean(Router.class);
    } catch (BeansException be) {
        if (be instanceof NoSuchBeanDefinitionException) {
            Router rr = new RouterImpl(vertx);
            applicationContext.getBeanFactory().registerSingleton("router", rr);
        }
    }
    final Router router = applicationContext.getBean(Router.class);
    initSwagger(environment);
    configRouter(vertx, router, environment);
    Map<String, Verticle> maps = applicationContext.getBeansOfType(Verticle.class);
    DeploymentOptions options = new DeploymentOptions().setInstances(1).setWorker(true);
    for (Map.Entry<String, Verticle> temp : maps.entrySet()) {
        Verticle verticle = temp.getValue();
        String name = verticle.getClass().getSimpleName().substring(0, 1).toLowerCase() + verticle.getClass().getSimpleName().substring(1);
        vertx.deployVerticle(verticleFactory.prefix() + ":" + name, options);
        for (Method method : verticle.getClass().getDeclaredMethods()) {
            if (method.isAnnotationPresent(Override.class)) {
                continue;
            }
            if (method.getName().contains("lambda")) {
                continue;
            }
            if (io.swagger.util.ReflectionUtils.isOverriddenMethod(method, verticle.getClass())) {
                continue;
            }
            if (method.isAnnotationPresent(BBRouter.class)) {
                BBRouter bbRouter = io.swagger.util.ReflectionUtils.getAnnotation(method, BBRouter.class);
                Route route = router.route(bbRouter.httpMethod(), bbRouter.path());
                route.handler(ctx -> {
                    ReflectionUtils.makeAccessible(method);
                    ReflectionUtils.invokeMethod(method, verticle, ctx);
                });
            }
        }
    }
    HttpServer httpServer = vertx.createHttpServer(new HttpServerOptions().setSsl(false).setKeyStoreOptions(new JksOptions().setPath("server-keystore.jks").setPassword("secret")));
    httpServer.requestHandler(router::accept);
    int port;
    try {
        port = Integer.valueOf(environment.getProperty("server.port", "8080"));
    } catch (Exception e) {
        throw new RuntimeException("请配置有效端口号");
    }
    httpServer.listen(port, ar -> {
        if (ar.succeeded()) {
            logger.info("Server started on port " + ar.result().actualPort());
        } else {
            logger.error("Cannot start the server: " + ar.cause());
        }
    });
    return applicationContext;
}
Also used : BBRouter(com.bob.vertx.swagger.BBRouter) HttpServerOptions(io.vertx.core.http.HttpServerOptions) RouterImpl(io.vertx.ext.web.impl.RouterImpl) HttpServer(io.vertx.core.http.HttpServer) Route(io.vertx.ext.web.Route) BeansException(org.springframework.beans.BeansException) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Router(io.vertx.ext.web.Router) BBRouter(com.bob.vertx.swagger.BBRouter) Method(java.lang.reflect.Method) HttpMethod(io.vertx.core.http.HttpMethod) Vertx(io.vertx.core.Vertx) BeansException(org.springframework.beans.BeansException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) Verticle(io.vertx.core.Verticle) DeploymentOptions(io.vertx.core.DeploymentOptions) JksOptions(io.vertx.core.net.JksOptions) Environment(org.springframework.core.env.Environment) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Map(java.util.Map)

Example 32 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder in project dubbo by alibaba.

the class DubboConfigBindingRegistrar method registerDubboConfigBean.

private void registerDubboConfigBean(String beanName, Class<? extends AbstractConfig> configClass, BeanDefinitionRegistry registry) {
    BeanDefinitionBuilder builder = rootBeanDefinition(configClass);
    AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
    registry.registerBeanDefinition(beanName, beanDefinition);
    if (log.isInfoEnabled()) {
        log.info("The dubbo config bean definition [name : " + beanName + ", class : " + configClass.getName() + "] has been registered.");
    }
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition)

Example 33 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder in project rocketmq-externals by apache.

the class RocketMQAutoConfigurationTests method listenerContainer.

@Test
public void listenerContainer() {
    load(false, "spring.rocketmq.nameServer=127.0.0.1:9876");
    BeanDefinitionBuilder beanBuilder = BeanDefinitionBuilder.rootBeanDefinition(MyListener.class);
    this.context.registerBeanDefinition("myListener", beanBuilder.getBeanDefinition());
    this.context.refresh();
    assertThat(this.context.getBeansOfType(DefaultRocketMQListenerContainer.class)).isNotEmpty();
    assertThat(this.context.containsBean(DefaultRocketMQListenerContainer.class.getName() + "_1")).isTrue();
    DefaultRocketMQListenerContainer listenerContainer = this.context.getBean(DefaultRocketMQListenerContainer.class.getName() + "_1", DefaultRocketMQListenerContainer.class);
    ObjectMapper objectMapper = this.context.getBean("rocketMQMessageObjectMapper", ObjectMapper.class);
    assertThat(listenerContainer.getObjectMapper()).isEqualTo(objectMapper);
    assertThat(listenerContainer.getConsumeMode()).isEqualTo(ConsumeMode.CONCURRENTLY);
    assertThat(listenerContainer.getSelectorType()).isEqualTo(SelectorType.TAG);
    assertThat(listenerContainer.getSelectorExpress()).isEqualTo("*");
    assertThat(listenerContainer.getConsumerGroup()).isEqualTo(TEST_CONSUMER_GROUP);
    assertThat(listenerContainer.getTopic()).isEqualTo(TEST_TOPIC);
    assertThat(listenerContainer.getNameServer()).isEqualTo("127.0.0.1:9876");
    assertThat(listenerContainer.getMessageModel()).isEqualTo(MessageModel.CLUSTERING);
    assertThat(listenerContainer.getConsumeThreadMax()).isEqualTo(1);
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) DefaultRocketMQListenerContainer(org.apache.rocketmq.spring.starter.core.DefaultRocketMQListenerContainer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 34 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder in project pentaho-platform by pentaho.

the class BeanListParser method parseInternal.

@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(BeanListBuilder.class.getName());
    builder.addPropertyValue("type", element.getAttribute("class"));
    Map<String, String> propMap = new HashMap<String, String>();
    Element objectproperties = DomUtils.getChildElementByTagName(element, Const.ATTRIBUTES);
    if (objectproperties != null) {
        List props = DomUtils.getChildElementsByTagName(objectproperties, Const.ATTR);
        if (props != null) {
            for (Object o : props) {
                Element prop = (Element) o;
                String key = prop.getAttribute(Const.KEY);
                String value = prop.getAttribute(Const.VALUE);
                propMap.put(key, value);
            }
        }
    }
    builder.addPropertyValue(Const.ATTRIBUTES, propMap);
    AbstractBeanDefinition definition = builder.getRawBeanDefinition();
    definition.setSource(parserContext.extractSource(element));
    return definition;
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) List(java.util.List)

Example 35 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder in project spring-security-oauth by spring-projects.

the class ConfigUtils method createSecurityMetadataSource.

public static BeanDefinition createSecurityMetadataSource(Element element, ParserContext pc) {
    List<Element> filterPatterns = DomUtils.getChildElementsByTagName(element, "url");
    if (filterPatterns.isEmpty()) {
        return null;
    }
    String patternType = element.getAttribute("path-type");
    if (!StringUtils.hasText(patternType)) {
        patternType = "ant";
    }
    MatcherType matcherType = MatcherType.valueOf(patternType);
    ManagedMap<BeanDefinition, BeanDefinition> invocationDefinitionMap = new ManagedMap<BeanDefinition, BeanDefinition>();
    for (Element filterPattern : filterPatterns) {
        String path = filterPattern.getAttribute("pattern");
        if (!StringUtils.hasText(path)) {
            pc.getReaderContext().error("pattern attribute cannot be empty or null", filterPattern);
        }
        String method = filterPattern.getAttribute("httpMethod");
        if (!StringUtils.hasText(method)) {
            method = null;
        }
        String access = filterPattern.getAttribute("resources");
        if (StringUtils.hasText(access)) {
            BeanDefinition matcher;
            if (createMatcherMethod4x != null) {
                matcher = (BeanDefinition) ReflectionUtils.invokeMethod(createMatcherMethod4x, matcherType, pc, path, method);
            } else {
                matcher = (BeanDefinition) ReflectionUtils.invokeMethod(createMatcherMethod3x, matcherType, path, method);
            }
            if (access.equals("none")) {
                invocationDefinitionMap.put(matcher, BeanDefinitionBuilder.rootBeanDefinition(Collections.class).setFactoryMethod("emptyList").getBeanDefinition());
            } else {
                BeanDefinitionBuilder attributeBuilder = BeanDefinitionBuilder.rootBeanDefinition(SecurityConfig.class);
                attributeBuilder.addConstructorArgValue(access);
                attributeBuilder.setFactoryMethod("createListFromCommaDelimitedString");
                if (invocationDefinitionMap.containsKey(matcher)) {
                    pc.getReaderContext().warning("Duplicate URL defined: " + path + ". The original attribute values will be overwritten", pc.extractSource(filterPattern));
                }
                invocationDefinitionMap.put(matcher, attributeBuilder.getBeanDefinition());
            }
        }
    }
    BeanDefinitionBuilder fidsBuilder = BeanDefinitionBuilder.rootBeanDefinition(DefaultFilterInvocationSecurityMetadataSource.class);
    fidsBuilder.addConstructorArgValue(invocationDefinitionMap);
    fidsBuilder.getRawBeanDefinition().setSource(pc.extractSource(element));
    return fidsBuilder.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanMetadataElement(org.springframework.beans.BeanMetadataElement) Element(org.w3c.dom.Element) MatcherType(org.springframework.security.config.http.MatcherType) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Collections(java.util.Collections) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Aggregations

BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)539 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)115 Element (org.w3c.dom.Element)99 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)60 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)60 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)59 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)53 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)47 ManagedList (org.springframework.beans.factory.support.ManagedList)33 ManagedMap (org.springframework.beans.factory.support.ManagedMap)29 HashMap (java.util.HashMap)19 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)19 Test (org.junit.jupiter.api.Test)16 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)16 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)12 Node (org.w3c.dom.Node)12 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)10 TypedStringValue (org.springframework.beans.factory.config.TypedStringValue)10 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)10