0

我想使用这些属性将一些大张旗鼓的案卷设置为 spring,但是当我实现 ImportBeanDefinitionRegistrar 并出现错误时我无法获取这些属性

引起:java.lang.NoSuchMethodException: com.github.sofior.swagger.SwaggerAutoConfiguration.<init>()

@Configuration
@EnableSwagger2
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerAutoConfiguration implements ImportBeanDefinitionRegistrar {

    private final SwaggerProperties properties;

    public SwaggerAutoConfiguration(SwaggerProperties properties) {
        this.properties = properties;
    }

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

        System.out.println(properties);
        properties.getDockets().forEach((docketName, docketProperties) -> {
            BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(Docket.class);
            builder.addConstructorArgValue(docketProperties.getType());
            builder.addConstructorArgValue(docketProperties.getType());
            registry.registerBeanDefinition(docketName, builder.getRawBeanDefinition());
        });
    }

}
4

4 回答 4

1

我认为这是不可能的,因为春天有两个阶段

1.bean注册

2.bean初始化和实例化

SwaggerProperties 只能在阶段 2 完成实例化后使用,但registerBeanDefinitions是阶段 1

于 2018-09-07T06:00:03.210 回答
0

这个问题的解决方法是在 registerBeanDefinitions 期间读取一个新属性

启用CustomSwagger

import org.springframework.context.annotation.Import;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(SwaggerAutoConfiguration.class)
public @interface EnableCustomSwagger {
    String path() default "";
}

Swagger自动配置

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;


public class SwaggerAutoConfiguration implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        String clsName = EnableCustomSwagger.class.getName();
        AnnotationAttributes attrs = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(clsName, false));
        if (!attrs.getString("path").equals("")) {
            String path = attrs.getString("path");
            ResourceLoader loader = new DefaultResourceLoader();
            Resource resource = loader.getResource(path);
            // you can get the value from your property files
        }

        //how can I get properties here,the properties is null

//        properties.getDockets().forEach((docketName, docketProperties) -> {
//            BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(Docket.class);
//            builder.addConstructorArgValue(docketProperties.getType());
//            builder.addConstructorArgValue(docketProperties.getType());
//            registry.registerBeanDefinition(docketName, builder.getRawBeanDefinition());
//        });
    }

}

应用

@SpringBootApplication
@EnableCustomSwagger(path="classpath:docklet.properties")
public class Application {
}
于 2018-09-07T08:54:12.107 回答
0

弹簧 2.x

import org.springframework.boot.context.properties.bind.Binder;
public class MultipleDataSourceComponentRegistrar implements ImportBeanDefinitionRegistrar, EnvironmentAware {
...

private Environment environment; @Override public void setEnvironment(Environment environment) { this.environment = environment; }

 @Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    ConfigurationProperties annotationCp = MultipleDataSourceSetProperties.class.getAnnotation(ConfigurationProperties.class);
    MultipleDataSourceSetProperties properties = Binder.get(environment).bind(annotationCp.prefix(), MultipleDataSourceSetProperties.class).get();

}

...

于 2019-12-31T06:40:52.247 回答
0

基本上,您需要将属性注入到您的类构造函数中。因此,配置应该是 Autowired 以便它们工作。

@Autowired
public SwaggerAutoConfiguration(SwaggerProperties properties) {
    this.properties = properties;
}

这应该可以解决您的“属性”为空问题。

于 2018-09-07T05:39:15.920 回答