This is an automated email from the ASF dual-hosted git repository. anatole pushed a commit to branch vote-0.4-incubating-01 in repository https://gitbox.apache.org/repos/asf/incubator-tamaya-site.git
commit a339277483dc7dc79df2a0ff8ae9ac3466002349 Author: Anatole Tresch <atsti...@gmail.com> AuthorDate: Fri Mar 15 00:05:21 2019 +0100 TAMAYA-384 Improved Spring integration. --- content/documentation/extensions/mod_spring.adoc | 82 ++++++------------------ 1 file changed, 20 insertions(+), 62 deletions(-) diff --git a/content/documentation/extensions/mod_spring.adoc b/content/documentation/extensions/mod_spring.adoc index 798431b..a85fff6 100644 --- a/content/documentation/extensions/mod_spring.adoc +++ b/content/documentation/extensions/mod_spring.adoc @@ -14,15 +14,11 @@ Tamaya _Spring_ is an extension module. Refer to the link:../extensions.html[ext === What functionality this module provides ? -Tamaya _Spring_ currently provides full integration with Spring and Spring Boot: +Tamaya _Spring_ allows to use Tamaya as a configuration backend for Spring or Spring Boot: -* A Spring +@Configuration+ implementation that also provides a Tamaya based version of - +org.springframework.context.support.PropertySourcesPlaceholderConfigurer+. -* +org.apache.tamaya.integration.spring.TamayaEnvironment+ is the Tamaya based implementation of the Spring - +Environment+ interface. -* +TamayaSpringPropertySource+ implements an additional Spring +PropertySource+. +* Tamaya is added via a +TamayaSpringPropertySource+ as an additional Spring +PropertySource+. * Finally +org.apache.tamaya.integration.spring.SpringConfigInjectionPostProcessor+ implements a Bean +PostProcessor+, - which adds all the fully fledged Tamaya configuration capabilities to Spring. + which adds all the fully fledged Tamaya configuration injection capabilities to Spring. === Compatibility @@ -45,15 +41,15 @@ To benefit from Tamaya Spring integration add the following dependencies to your ----------------------------------------------- -=== Registering Tamaya Spring Configuration +=== Configuring Tamaya Spring Configuration -Basically to activate the Tamaya Spring support the most simple thing is to a enable the Tamaya package for being -scanned for Spring components, e.g. using by annotation: +Tamaya leverages standard Spring functionality of Spring. To activate and configure Tamaya just add the +corresponding Tamaya annotation to your Spring Configuration: [source, java] -------------------------------------------------------- @SpringBootApplication -@ComponentScan({"org.apache.tamaya.integration.spring"}) +@EnableTamayaConfig public class SampleWebFreeMarkerApplication { public static void main(String[] args) throws Exception { @@ -63,54 +59,27 @@ public class SampleWebFreeMarkerApplication { } -------------------------------------------------------- -Of course, you can still use Spring's XML configuration in a similar way: -[source, xml] --------------------------------------------------------- -<beans xmlns="http://www.springframework.org/schema/beans" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns:context="http://www.springframework.org/schema/context" - xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> - - <context:annotation-config /> - <context:component-scan base-package="org.apache.tamaya.integration.spring"/> - ... - -</beans> --------------------------------------------------------- +=== Extending or Overriding existing Spring Configuration +By default Tamaya is overriding any existing Spring configuration. But you can also configure Tamaya to extend +Spring configuration only as follows: -Though not recommended you can explicitly register the Tamaya related beans in your context configuration by hand: -files: - -[source, xml] --------------------------------------------------------- -<bean id="tamayaInjectionProcessor" name="tamayaInjectionProcessor" class="org.apache.tamaya.integration.spring.SpringConfigInjectionPostProcessor"/> -<bean id="tamayaConfigProvider" name="tamayaConfigProvider" class="org.apache.tamaya.integration.spring.TamayaSpringConfig"/> --------------------------------------------------------- - - -=== Configuring your Context - -After activation you can use Tamaya as a backend for property resolution, e.g. +propertyValue+ -is resolved from the current Tamaya configuration. See example below: - -[source, xml] +[source, java] -------------------------------------------------------- -<bean id="configuredBean" name="configuredBean" class="org.apache.tamaya.integration.spring.ConfiguredSpringBean"> - <property name="message" value="${propertyValue}"/> -</bean> +@EnableTamayaConfig(extendOnly=true) -------------------------------------------------------- === Configuring your Beans -Similarly you can inject any kind of configuration as supported by Tamaya into your Spring managed beans: +Similarly you can inject any kind of configuration as supported by Tamaya into your Spring managed beans. +Hereby both, Spring injection and Tamaya injection mechanisms are supported: [source, java] -------------------------------------------------------- -@ConfigDefaultSections("app.root") // optional <1> +@ConfigSection("app.root") // optional <1> @Component public class ConfiguredSpringBean { @@ -150,27 +119,16 @@ public class ConfiguredSpringBean { Summarizing you get all the nice features of Tamaya out of the box running with your Spring code. -=== Working with Dynamic Values +NOTE: For a full description of Tamaya's injection API please + refer to the link:extensions/mod_injection.html[corresponding documentation]. -Integration into Spring also includes for support for _dynamic values_: -[source, java] --------------------------------------------------------- -@Config(value = "foreground.color", required = false, defaultValue = "#DDDDDD") -private DynamicValue<Color> foregroundColor; --------------------------------------------------------- +=== Disabling Tamaya Injection -Dynamic values are a very flexible mechanism for managing configuration changes. -You can even use an update policy to define how you want to handle configuration -changes for your configuration parameter: +If you dont like Tamaya injection, you can simply disable it: [source, java] -------------------------------------------------------- -foregroundColor.setUpdatePolicy(UpdatePolicy.IMMEDEATE); -foregroundColor.addPropertyChangeListener(() -> { - System.out.println("New forground color: " + foregroundColor.get(); -}); +@EnableTamayaConfig(disableTamayaInjection=true) -------------------------------------------------------- -IMPORTANT: For a full description of Tamaya's injection API please - refer to the link:extensions/mod_injection.html[corresponding documentation].