org.springframework.boot.autoconfigure.AutoConfigureAfter源码实例Demo

类org.springframework.boot.autoconfigure.AutoConfigureAfter源码实例Demo

下面列出了org.springframework.boot.autoconfigure.AutoConfigureAfter 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

private void createRestModuleAutoConfigurationSource(String packageName, EipModel model) throws MojoFailureException {
    final JavaClass javaClass = new JavaClass(getProjectClassLoader());
    final int pos = model.getJavaType().lastIndexOf(".");
    final String name = model.getJavaType().substring(pos + 1) + "AutoConfiguration";
    final String configType = model.getJavaType().substring(pos + 1) + "Properties";

    javaClass.setPackage(packageName);
    javaClass.setName(name);

    String doc = "Generated by camel-package-maven-plugin - do not edit this file!";
    javaClass.getJavaDoc().setFullText(doc);

    javaClass.addAnnotation(Generated.class).setStringValue("value", SpringBootAutoConfigurationMojo.class.getName());
    javaClass.addAnnotation(Configuration.class).setLiteralValue("proxyBeanMethods", "false");
    javaClass.addAnnotation(ConditionalOnBean.class).setStringValue("type", "org.apache.camel.spring.boot.CamelAutoConfiguration");
    javaClass.addAnnotation(ConditionalOnProperty.class).setStringValue("name", "camel.rest.enabled").setLiteralValue("matchIfMissing", "true");
    javaClass.addAnnotation(AutoConfigureAfter.class).setStringValue("name", "org.apache.camel.spring.boot.CamelAutoConfiguration");
    javaClass.addAnnotation(EnableConfigurationProperties.class).setLiteralValue("value", configType + ".class");

    javaClass.addImport("java.util.Map");
    javaClass.addImport("java.util.HashMap");
    javaClass.addImport("org.apache.camel.util.CollectionHelper");
    javaClass.addImport("org.apache.camel.support.IntrospectionSupport");
    javaClass.addImport("org.apache.camel.spring.boot.util.CamelPropertiesHelper");
    javaClass.addImport("org.apache.camel.CamelContext");
    javaClass.addImport("org.apache.camel.component.rest.RestComponent");
    javaClass.addImport("org.apache.camel.spi.RestConfiguration");

    javaClass.addField().setName("camelContext").setType(loadClass("org.apache.camel.CamelContext")).setPrivate().addAnnotation(Autowired.class);
    javaClass.addField().setName("config").setType(loadClass(packageName + "." + configType)).setPrivate().addAnnotation(Autowired.class);

    Method method;

    // Configuration
    method = javaClass.addMethod();
    method.setName("configure" + model.getShortJavaType());
    method.setPublic();
    method.addThrows(Exception.class);
    method.setReturnType(loadClass("org.apache.camel.spi.RestConfiguration"));
    method.addAnnotation(Lazy.class);
    method.addAnnotation(Bean.class).setLiteralValue("name", "RestComponent.DEFAULT_REST_CONFIGURATION_ID");
    method.addAnnotation(ConditionalOnClass.class).setLiteralValue("value", "CamelContext.class");
    method.addAnnotation(ConditionalOnMissingBean.class);
    method.setBody("" + "Map<String, Object> properties = new HashMap<>();\n" + "IntrospectionSupport.getProperties(config, properties, null, false);\n"
                   + "// These options is configured specially further below, so remove them first\n" + "properties.remove(\"enableCors\");\n"
                   + "properties.remove(\"apiProperty\");\n" + "properties.remove(\"componentProperty\");\n" + "properties.remove(\"consumerProperty\");\n"
                   + "properties.remove(\"dataFormatProperty\");\n" + "properties.remove(\"endpointProperty\");\n" + "properties.remove(\"corsHeaders\");\n" + "\n"
                   + "RestConfiguration definition = new RestConfiguration();\n" + "CamelPropertiesHelper.setCamelProperties(camelContext, definition, properties, true);\n"
                   + "\n" + "// Workaround for spring-boot properties name as It would appear\n" + "// as enable-c-o-r-s if left uppercase in Configuration\n"
                   + "definition.setEnableCORS(config.getEnableCors());\n" + "\n" + "if (config.getApiProperty() != null) {\n"
                   + "    definition.setApiProperties(new HashMap<>(CollectionHelper.flattenKeysInMap(config.getApiProperty(), \".\")));\n" + "}\n"
                   + "if (config.getComponentProperty() != null) {\n"
                   + "    definition.setComponentProperties(new HashMap<>(CollectionHelper.flattenKeysInMap(config.getComponentProperty(), \".\")));\n" + "}\n"
                   + "if (config.getConsumerProperty() != null) {\n"
                   + "    definition.setConsumerProperties(new HashMap<>(CollectionHelper.flattenKeysInMap(config.getConsumerProperty(), \".\")));\n" + "}\n"
                   + "if (config.getDataFormatProperty() != null) {\n"
                   + "    definition.setDataFormatProperties(new HashMap<>(CollectionHelper.flattenKeysInMap(config.getDataFormatProperty(), \".\")));\n" + "}\n"
                   + "if (config.getEndpointProperty() != null) {\n"
                   + "    definition.setEndpointProperties(new HashMap<>(CollectionHelper.flattenKeysInMap(config.getEndpointProperty(), \".\")));\n" + "}\n"
                   + "if (config.getCorsHeaders() != null) {\n" + "    Map<String, Object> map = CollectionHelper.flattenKeysInMap(config.getCorsHeaders(), \".\");\n"
                   + "    Map<String, String> target = new HashMap<>();\n" + "    map.forEach((k, v) -> target.put(k, v.toString()));\n"
                   + "    definition.setCorsHeaders(target);\n" + "}\n" + "return definition;");

    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    writeSourceIfChanged(javaClass, fileName, true);
    writeComponentSpringFactorySource(packageName, name);
}
 
private void createComponentAutoConfigurationSource(String packageName, ComponentModel model, List<String> componentAliases, String overrideComponentName)
    throws MojoFailureException {

    final String name = model.getJavaType().substring(model.getJavaType().lastIndexOf(".") + 1).replace("Component", "ComponentAutoConfiguration");
    final String configurationName = name.replace("ComponentAutoConfiguration", "ComponentConfiguration");
    final String componentName = (overrideComponentName != null ? overrideComponentName : model.getScheme()).toLowerCase(Locale.US);

    Class configClass = generateDummyClass(packageName + "." + configurationName);

    final JavaClass javaClass = new JavaClass(getProjectClassLoader());

    javaClass.setPackage(packageName);
    javaClass.setName(name);
    javaClass.getJavaDoc().setFullText("Generated by camel-package-maven-plugin - do not edit this file!");
    javaClass.addAnnotation(Generated.class).setStringValue("value", SpringBootAutoConfigurationMojo.class.getName());
    javaClass.addAnnotation(Configuration.class).setLiteralValue("proxyBeanMethods", "false");
    javaClass.addAnnotation(Conditional.class).setLiteralValue("{ConditionalOnCamelContextAndAutoConfigurationBeans.class,\n        " + name + ".GroupConditions.class}");
    javaClass.addAnnotation(AutoConfigureAfter.class).setLiteralValue("CamelAutoConfiguration.class");
    javaClass.addAnnotation(EnableConfigurationProperties.class).setLiteralValue("{ComponentConfigurationProperties.class,\n        " + configurationName + ".class}");

    javaClass.addImport(HashMap.class);
    javaClass.addImport(List.class);
    javaClass.addImport(Map.class);
    javaClass.addImport(ApplicationContext.class);
    javaClass.addImport("org.slf4j.Logger");
    javaClass.addImport("org.slf4j.LoggerFactory");
    javaClass.addImport("org.apache.camel.CamelContext");
    javaClass.addImport("org.apache.camel.spi.ComponentCustomizer");
    javaClass.addImport("org.apache.camel.spring.boot.CamelAutoConfiguration");
    javaClass.addImport("org.apache.camel.spring.boot.ComponentConfigurationProperties");
    javaClass.addImport("org.apache.camel.spring.boot.util.CamelPropertiesHelper");
    javaClass.addImport("org.apache.camel.spring.boot.util.ConditionalOnCamelContextAndAutoConfigurationBeans");
    javaClass.addImport("org.apache.camel.spring.boot.util.GroupCondition");
    javaClass.addImport("org.apache.camel.spring.boot.util.HierarchicalPropertiesEvaluator");
    javaClass.addImport("org.apache.camel.support.IntrospectionSupport");
    javaClass.addImport("org.apache.camel.util.ObjectHelper");
    javaClass.addImport("org.apache.camel.spi.HasId");
    javaClass.addImport(model.getJavaType());

    javaClass.addField().setPrivate().setStatic(true).setFinal(true).setName("LOGGER").setType(loadClass("org.slf4j.Logger"))
        .setLiteralInitializer("LoggerFactory\n            .getLogger(" + name + ".class)");
    javaClass.addField().setPrivate().setName("applicationContext").setType(ApplicationContext.class).addAnnotation(Autowired.class);
    javaClass.addField().setPrivate().setName("camelContext").setType(loadClass("org.apache.camel.CamelContext")).addAnnotation(Autowired.class);
    javaClass.addField().setPrivate().setName("configuration").setType(configClass).addAnnotation(Autowired.class);
    javaClass.addField().setPrivate().setName("customizers").setType(loadType("java.util.List<org.apache.camel.spi.ComponentCustomizer<" + model.getJavaType() + ">>"))
        .addAnnotation(Autowired.class).setLiteralValue("required", "false");

    javaClass.addNestedType().setName("GroupConditions").setStatic(true).setPackagePrivate().extendSuperType("GroupCondition").addMethod().setName("GroupConditions")
        .setConstructor(true).setPublic().setBody("super(\"camel.component\", \"camel.component." + componentName + "\");");

    // add method for auto configure
    String body = createComponentBody(model.getShortJavaType(), componentName);
    String methodName = "configure" + model.getShortJavaType();

    Method method = javaClass.addMethod().setName(methodName).setPublic().setBody(body).setReturnType(loadType(model.getJavaType())).addThrows(Exception.class);

    // Determine all the aliases
    String[] springBeanAliases = componentAliases.stream().map(alias -> alias + "-component").toArray(size -> new String[size]);

    method.addAnnotation(Lazy.class);
    method.addAnnotation(Bean.class).setStringArrayValue("name", springBeanAliases);
    method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue(model.getShortJavaType() + ".class");

    sortImports(javaClass);

    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    writeSourceIfChanged(javaClass, fileName, false);
}
 
private void createDataFormatAutoConfigurationSource(String packageName, DataFormatModel model, List<String> dataFormatAliases, String overrideDataFormatName)
    throws MojoFailureException {

    final String name = model.getJavaType().substring(model.getJavaType().lastIndexOf(".") + 1).replace("DataFormat", "DataFormatAutoConfiguration");
    final String configurationName = name.replace("DataFormatAutoConfiguration", "DataFormatConfiguration");
    final String dataformatName = (overrideDataFormatName != null ? overrideDataFormatName : model.getName()).toLowerCase(Locale.US);

    Class configClass = generateDummyClass(packageName + "." + configurationName);

    final JavaClass javaClass = new JavaClass(getProjectClassLoader());

    javaClass.setPackage(packageName);
    javaClass.setName(name);
    javaClass.getJavaDoc().setFullText("Generated by camel-package-maven-plugin - do not edit this file!");
    javaClass.addAnnotation(Generated.class).setStringValue("value", SpringBootAutoConfigurationMojo.class.getName());
    javaClass.addAnnotation(Configuration.class).setLiteralValue("proxyBeanMethods", "false");
    javaClass.addAnnotation(Conditional.class).setLiteralValue("{ConditionalOnCamelContextAndAutoConfigurationBeans.class,\n        " + name + ".GroupConditions.class}");
    javaClass.addAnnotation(AutoConfigureAfter.class).setStringValue("name", "org.apache.camel.spring.boot.CamelAutoConfiguration");
    javaClass.addAnnotation(EnableConfigurationProperties.class).setLiteralValue("{DataFormatConfigurationProperties.class,\n        " + configurationName + ".class}");

    javaClass.addImport(HashMap.class);
    javaClass.addImport(List.class);
    javaClass.addImport(Map.class);
    javaClass.addImport(ApplicationContext.class);
    javaClass.addImport(ConditionalOnBean.class);
    javaClass.addImport("org.slf4j.Logger");
    javaClass.addImport("org.slf4j.LoggerFactory");
    javaClass.addImport("org.apache.camel.CamelContext");
    javaClass.addImport("org.apache.camel.CamelContextAware");
    javaClass.addImport("org.apache.camel.spring.boot.CamelAutoConfiguration");
    javaClass.addImport("org.apache.camel.spring.boot.DataFormatConfigurationProperties");
    javaClass.addImport("org.apache.camel.spring.boot.util.CamelPropertiesHelper");
    javaClass.addImport("org.apache.camel.spring.boot.util.ConditionalOnCamelContextAndAutoConfigurationBeans");
    javaClass.addImport("org.apache.camel.spring.boot.util.GroupCondition");
    javaClass.addImport("org.apache.camel.spring.boot.util.HierarchicalPropertiesEvaluator");
    javaClass.addImport("org.apache.camel.support.IntrospectionSupport");
    javaClass.addImport("org.apache.camel.util.ObjectHelper");
    javaClass.addImport("org.apache.camel.RuntimeCamelException");
    javaClass.addImport("org.apache.camel.spi.DataFormat");
    javaClass.addImport("org.apache.camel.spi.DataFormatCustomizer");
    javaClass.addImport("org.apache.camel.spi.DataFormatFactory");
    javaClass.addImport("org.apache.camel.spi.HasId");
    javaClass.addImport(model.getJavaType());

    javaClass.addField().setPrivate().setStatic(true).setFinal(true).setName("LOGGER").setType(loadType("org.slf4j.Logger"))
        .setLiteralInitializer("LoggerFactory\n            .getLogger(" + name + ".class)");
    javaClass.addField().setPrivate().setName("applicationContext").setType(ApplicationContext.class).addAnnotation(Autowired.class);
    javaClass.addField().setPrivate().setName("camelContext").setType(loadType("org.apache.camel.CamelContext")).addAnnotation(Autowired.class);
    javaClass.addField().setPrivate().setName("configuration").setType(configClass).addAnnotation(Autowired.class);
    javaClass.addField().setPrivate().setName("customizers").setType(loadType("java.util.List<org.apache.camel.spi.DataFormatCustomizer<" + model.getJavaType() + ">>"))
        .addAnnotation(Autowired.class).setLiteralValue("required", "false");

    JavaClass groupConditions = javaClass.addNestedType().setName("GroupConditions").setStatic(true).setPackagePrivate().extendSuperType("GroupCondition");
    groupConditions.addMethod().setName("GroupConditions").setConstructor(true).setPublic()
        .setBody("super(\"camel.dataformat\", \"camel.dataformat." + dataformatName + "\");");

    String body = createDataFormatBody(model.getShortJavaType(), dataformatName);
    String methodName = "configure" + model.getShortJavaType() + "Factory";

    Method method = javaClass.addMethod().setName(methodName).setPublic().setBody(body).setReturnType(loadType("org.apache.camel.spi.DataFormatFactory"))
        .addThrows(Exception.class);

    // Determine all the aliases
    // adding the '-dataformat' suffix to prevent collision with component
    // names
    String[] springBeanAliases = dataFormatAliases.stream().map(alias -> alias + "-dataformat-factory").toArray(String[]::new);

    method.addAnnotation(Bean.class).setStringArrayValue("name", springBeanAliases);
    method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue("value", model.getShortJavaType() + ".class");

    sortImports(javaClass);

    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    writeSourceIfChanged(javaClass, fileName, false);
}
 
private void createLanguageAutoConfigurationSource(String packageName, LanguageModel model, List<String> languageAliases, String overrideLanguageName)
    throws MojoFailureException {

    final String name = model.getJavaType().substring(model.getJavaType().lastIndexOf(".") + 1).replace("Language", "LanguageAutoConfiguration");
    final String configurationName = name.replace("LanguageAutoConfiguration", "LanguageConfiguration");
    final String languageName = (overrideLanguageName != null ? overrideLanguageName : model.getName()).toLowerCase(Locale.US);

    Class configClass = generateDummyClass(packageName + "." + configurationName);

    final JavaClass javaClass = new JavaClass(getProjectClassLoader());

    javaClass.setPackage(packageName);
    javaClass.setName(name);
    javaClass.getJavaDoc().setFullText("Generated by camel-package-maven-plugin - do not edit this file!");
    javaClass.addAnnotation(Generated.class).setStringValue("value", SpringBootAutoConfigurationMojo.class.getName());
    javaClass.addAnnotation(Configuration.class).setLiteralValue("proxyBeanMethods", "false");
    javaClass.addAnnotation(Conditional.class).setLiteralValue("{ConditionalOnCamelContextAndAutoConfigurationBeans.class,\n        " + name + ".GroupConditions.class}");
    javaClass.addAnnotation(AutoConfigureAfter.class).setLiteralValue("CamelAutoConfiguration.class");
    javaClass.addAnnotation(EnableConfigurationProperties.class).setLiteralValue("{LanguageConfigurationProperties.class,\n        " + configurationName + ".class}");

    javaClass.addImport(HashMap.class);
    javaClass.addImport(List.class);
    javaClass.addImport(Map.class);
    javaClass.addImport(ApplicationContext.class);
    javaClass.addImport(ConditionalOnBean.class);
    javaClass.addImport(ConfigurableBeanFactory.class);
    javaClass.addImport("org.slf4j.Logger");
    javaClass.addImport("org.slf4j.LoggerFactory");
    javaClass.addImport("org.apache.camel.CamelContext");
    javaClass.addImport("org.apache.camel.CamelContextAware");
    javaClass.addImport("org.apache.camel.spring.boot.CamelAutoConfiguration");
    javaClass.addImport("org.apache.camel.spring.boot.LanguageConfigurationProperties");
    javaClass.addImport("org.apache.camel.spring.boot.util.CamelPropertiesHelper");
    javaClass.addImport("org.apache.camel.spring.boot.util.ConditionalOnCamelContextAndAutoConfigurationBeans");
    javaClass.addImport("org.apache.camel.spring.boot.util.GroupCondition");
    javaClass.addImport("org.apache.camel.spring.boot.util.HierarchicalPropertiesEvaluator");
    javaClass.addImport("org.apache.camel.support.IntrospectionSupport");
    javaClass.addImport("org.apache.camel.util.ObjectHelper");
    javaClass.addImport("org.apache.camel.spi.HasId");
    javaClass.addImport("org.apache.camel.spi.LanguageCustomizer");
    javaClass.addImport(model.getJavaType());

    javaClass.addField().setPrivate().setStatic(true).setFinal(true).setName("LOGGER").setType(loadType("org.slf4j.Logger"))
        .setLiteralInitializer("LoggerFactory\n            .getLogger(" + name + ".class)");
    javaClass.addField().setPrivate().setName("applicationContext").setType(ApplicationContext.class).addAnnotation(Autowired.class);
    javaClass.addField().setPrivate().setName("camelContext").setType(loadType("org.apache.camel.CamelContext")).addAnnotation(Autowired.class);
    javaClass.addField().setPrivate().setName("configuration").setType(configClass).addAnnotation(Autowired.class);
    javaClass.addField().setPrivate().setName("customizers").setType(loadType("java.util.List<org.apache.camel.spi.LanguageCustomizer<" + model.getJavaType() + ">>"))
        .addAnnotation(Autowired.class).setLiteralValue("required", "false");

    javaClass.addNestedType().setName("GroupConditions").setStatic(true).setPackagePrivate().extendSuperType("GroupCondition").addMethod().setName("GroupConditions")
        .setConstructor(true).setPublic().setBody("super(\"camel.component\", \"camel.component." + languageName + "\");");

    String body = createLanguageBody(model.getShortJavaType(), languageName);
    String methodName = "configure" + model.getShortJavaType();

    Method method = javaClass.addMethod().setName(methodName).setPublic().setBody(body).setReturnType(loadType(model.getJavaType())).addThrows(Exception.class);

    // Determine all the aliases
    // adding the '-language' suffix to prevent collision with component
    // names
    String[] springBeanAliases = languageAliases.stream().map(alias -> alias + "-language").toArray(String[]::new);

    method.addAnnotation(Bean.class).setStringArrayValue("name", springBeanAliases);
    method.addAnnotation(Scope.class).setLiteralValue("ConfigurableBeanFactory.SCOPE_PROTOTYPE");
    method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue("value", model.getShortJavaType() + ".class");

    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    writeSourceIfChanged(javaClass, fileName, false);
}
 
源代码评论
动弹
沙发等你来抢
 类方法
 同包方法