spring - 如何在没有 spring-boot 的情况下在 spring-webflux 中加载配置?

标签 spring spring-mvc spring-boot kotlin

我只是对 spring webflux 5.0.0 和 Kotlin 进行了一些实验,我在从 application.yml 加载配置时遇到了问题

对于基础项目,我从这个例子开始 spring-kotlin-functional
但是只有手动加载 bean 和路由,而没有从配置文件中加载任何内容,或者如何以这种方式实现 @ConfigurationProperties 类的模拟。

我尝试在 bean 部分采取环境:

data class DbConfig(
    var url: String = "",
    var user: String = "",
    var password: String = ""
)

fun beans(): BeanDefinitionDsl = beans {
    bean {
        //try to load config from path=db to data class DbConfig
        env.getProperty("db", DbConfig::class.java)
    }

    bean<DBConfiguration>()

    //controllers
    bean { StatsController(ref()) }
    bean { UserController(ref()) }

    //repository
    bean { UserRepository(ref()) }

    //services
    bean { StatsService(ref()) }

    //routes
    bean { Routes(ref(), ref()) }
    bean("webHandler") {
        RouterFunctions.toWebHandler(ref<Routes>().router(), HandlerStrategies.builder().viewResolver(ref()).build())
    }

    //view resolver
    bean {
        val prefix = "classpath:/templates/"
        val suffix = ".mustache"
        val loader = MustacheResourceTemplateLoader(prefix, suffix)
        MustacheViewResolver(Mustache.compiler().withLoader(loader)).apply {
            setPrefix(prefix)
            setSuffix(suffix)
        }
    }
}

但环境中只有系统属性

所以问题是如何从 application.yml 加载配置以及如何以这种功能风格实现 @ConfigurationProperties 的模拟?

我是否正确理解没有spring-boot所有注释(如@Bean、@Repository、@Transactional 等)都不适用于Beans?

我的消息来源:github

2017 年 10 月 21 日更新

寻找解决方案。问题与没有任何 BeanPostProcessor 的事实有关。在我包含这两个处理器之后:
bean<CommonAnnotationBeanPostProcessor>()
bean<ConfigurationClassPostProcessor>() 

注释 @Configuration , @Bean@PostConstruct开始工作。但是注释@ConfigurationProperties仅存在于 spring-boot我仅在 spring-boot-starter 中找到的依赖项和 yml 解析类..

包含依赖后spring-boot-starter并添加 bean<ConfigurationPropertiesBindingPostProcessor>()到 beans 部分,注释 @ConfigurationProperties开始工作,但来自 application.yml 的配置也不包括在内。所以我添加了这个部分:
val resource = ClassPathResource("/application.yml")
val sourceLoader = YamlPropertySourceLoader()
val properties = sourceLoader.load("main config", resource, null)
environment.propertySources.addFirst(properties)

GenericApplicationContext配置。现在一切都按我的预期工作,但包含一个依赖 spring-boot-starter .

完整代码示例:version with fixes

最佳答案

Spring Boot 只是一个依赖管理,它构建了您可以覆盖的自动配置。
所有的特性都继承自 Spring 框架和模块。所以基本上你可以在有或没有引导的情况下做同样的事情。

我还没有使用 webflux。但是当您引用其他 bean 时,您需要在其他地方声明一个配置 bean。

我相信我会帮你的...

关于spring - 如何在没有 spring-boot 的情况下在 spring-webflux 中加载配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46834767/

相关文章:

spring - Autowiring 使用 @Component 注释的非托管 Bean

java - 如何从 Spring 字符编码中排除文件?

java - 使用 Spring MVC 显示 XML 字符串

spring-boot - springboot2.0中使用@cacheable时如何为每个redis缓存配置不同的ttl

java - Spring MVC 如何解析和验证处理程序方法参数?

java - 为什么我的代码不更新我的实体对象? Spring Hibernate MVC 应用程序

java - 在 spring 中通过登录用户持久化实体

spring - 在 IntelliJ 14 上运行 spring mvc Web 应用程序时出现 Tomcat 错误

java - 如何让 spring-boot @Async 与 Java 8 一起工作

java - 读取程序集时出错 : Descriptor with ID <xml path> not found