spring - Grails 3 @Conditional from external configuration files -
in grails 3 app need define spring bean based on condition.
what have done created bean , annotate conditional
@component @conditional(mycondition.class) class mybean {
here mycondition
import org.springframework.context.annotation.condition import org.springframework.context.annotation.conditioncontext import org.springframework.core.type.annotatedtypemetadata class mycondition implements condition { @override boolean matches(conditioncontext context, annotatedtypemetadata metadata) { return 'xxx' == context.environment.getproperty("myproperty") } }
this works fine until need load value of myproperty external (in prod env config file outside of application) source.
i loading external configuration files following
class application extends grailsautoconfiguration implements environmentaware { static void main(string[] args) { grailsapp.run(application, args) } @override void setenvironment(environment environment) { def app = metadata.getcurrent().getapplicationname() def grails_home = "${system.getproperty("user.home")}/.grails" ['properties', 'groovy', 'yml'].each { def name = "${app}-config.${it}" def config = new file(grails_home , name) if (config.exists()) { resource resourceconfig = new filesystemresource(config); log.info("loading config {}", config.getabsolutepath()) def properties switch (it) { case 'properties': properties = new properties() properties.load(resourceconfig.inputstream) break case 'groovy': properties = new configslurper(grails.util.environment.current.name).parse(resourceconfig.url) break case 'yml': yamlpropertiesfactorybean ypfb = new yamlpropertiesfactorybean(); ypfb.setresources(resourceconfig); ypfb.afterpropertiesset(); properties = ypfb.getobject(); break } environment.propertysources.addfirst(new mappropertysource(name, properties)) } } } } }
problem invoke mycondition before loading of external properties. content of external configuration files not take in consideration in mycondition.
i know can move definition of beans
/grails-app/conf/spring/resources.groovy
and create beans after loading of properties.
so question how define beans whit @component + @conditional(mycondition.class) take external properties in consideration.
Comments
Post a Comment