• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

spring Initializr 笔记

开发技术 开发技术 3小时前 1次浏览

1. 根据项目,修改Application.properties, 

需要继承类ProjectContributor,并且对外公布为@Bean

public class ApplicationPropertiesContributor implements ProjectContributor {

    private ProjectDescription description;

    public ApplicationPropertiesContributor(ProjectDescription description) {
        this.description = description;
    }

    private final String relativePath = "src/main/resources/application.properties";
    private final String resourcePattern = "classpath:configuration/application.properties";

    private final PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

    @Override
    public void contribute(Path projectRoot) throws IOException {

        Path output = projectRoot.resolve(this.relativePath);
        if (!Files.exists(output)) {
            Files.createDirectories(output.getParent());
            Files.createFile(output);
        }

     //将现有配置项中Application.properties复制到target路径,然后Append自定义配置 Resource resource
= this.resolver.getResource(this.resourcePattern); FileCopyUtils.copy(resource.getInputStream(), Files.newOutputStream(output, StandardOpenOption.APPEND)); if (this.description.getRequestedDependencies().containsKey("nacos-discovery")) { try (PrintWriter writer = new PrintWriter(Files.newBufferedWriter(output, StandardOpenOption.APPEND))) { writer.println(); writer.println("spring.cloud.nacos.discovery.username=nacos"); writer.println("spring.cloud.nacos.discovery.password=nacos"); writer.println("spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848"); writer.println("spring.cloud.nacos.discovery.namespace=public"); } } } }

 

2. 如果需要对启动Main方法做私有化定制,需要实现 MainApplicationTypeCustomizer,如pom引入了openFeign依赖,则需要在main方法中添加注解@EnableFeignClients

    @Bean
    public MainApplicationTypeCustomizer<TypeDeclaration> nacosDiscoveryApplicationAnnotator(ProjectDescription projectDescription, Build build) {
        return typeDeclaration -> {
            if (projectDescription.getRequestedDependencies().containsKey("nacos-discovery")) {
                typeDeclaration
                        .annotate(Annotation.name("org.springframework.cloud.client.discovery.EnableDiscoveryClient"));
            }
            if (build.dependencies().has("cloud-feign")) {
                typeDeclaration
                        .annotate(Annotation.name("org.springframework.cloud.openfeign.EnableFeignClients"));
            }
        };
    }

 

最终直接结果如下:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosConfigDemoApplication {
  public static void main(String[] args) {
    SpringApplication.run(NacosConfigDemoApplication.class, args);
  }

}

 

 

3.通过实现 HelpDocumentCustomizer, 实现在Help.md中添加自定义信息,如:

public class LixiangHelpDocumentCustomizer implements HelpDocumentCustomizer {
    private ProjectDescription projectDescription;

    public LixiangHelpDocumentCustomizer(ProjectDescription projectDescription) {
        this.projectDescription = projectDescription;
    }

    @Override
    public void customize(HelpDocument document) {

        if (projectDescription.getRequestedDependencies().containsKey("nacos-config")) {
            document.gettingStarted().addGuideLink("https://github.com/alibaba/spring-cloud-alibaba/tree/master/spring-cloud-alibaba-examples/nacos-example/nacos-config-example",
                    "Nacos Configuration Example");
            document.getWarnings().addItem("如果spring-boot版本高于2.4.0,并且配置未生效的话, 请在systerm.Envrionment中尝试配置如下属性'spring.cloud.bootstrap.enabled=true'.");
        }
        if (projectDescription.getRequestedDependencies().containsKey("nacos-discovery")) {
            document.gettingStarted().addGuideLink("https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme.md",
                    "Nacos Service Discovery Example");
        }
    }
}

 

 

 

 

 

 

  

 


程序员灯塔
转载请注明原文链接:spring Initializr 笔记
喜欢 (0)