How to load configuration in spring -webflux without spring-boot?

I'm just experimenting with spring webflux 5.0.0 and Kotlin, and I have a problem loading the configuration from application.yml

For the basic project, I start with this spring-kotlin-functional example.
But there is only manual loading of beans and routing without any loading from the configuration files, or an example of how to implement the analogue of the @ConfigurationProperties class this way.

I am trying to accept the environment in the beans section:

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)
        }
    }
}

but only system properties exist in the environment

So, the question is how to load the configuration from application.yml and how to implement the @ConfigurationProperties analogue in such a functional style?

, spring -boot (, @Bean, @Repository, @Transactional ) Beans?

: github

2017-10-21

. , BeanPostProcessor. :

bean<CommonAnnotationBeanPostProcessor>()
bean<ConfigurationClassPostProcessor>() 

@Configuration, @Bean @PostConstruct . @ConfigurationProperties spring-boot, yml spring-boot-starter..

spring-boot-starter bean<ConfigurationPropertiesBindingPostProcessor>() beans @ConfigurationProperties, config 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.

:

+4
1

Spring - , , . Spring. , .

webflux. beans, bean .

, ...

0

Source: https://habr.com/ru/post/1687826/


All Articles