blob: d95eb0da745ba2681226732a14aaff9437dcc6e3 [file] [log] [blame]
:jbake-type: page
:jbake-status: published
= Apache Tamaya - Extension: Spring Integration
toc::[]
[[Spring]]
== Tamaya Spring Integration (Extension Module)
Tamaya _Spring_ is an extension module. Refer to the link:../extensions.html[extensions documentation] for further details.
=== What functionality this module provides ?
Tamaya _Spring_ allows to use Tamaya as a configuration backend for Spring or Spring Boot:
* 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 injection capabilities to Spring.
=== Compatibility
Both modules are based on Java 8, so they will run on Java 8 and beyond. The extension shown here works in
Spring Framework as well as Spring Boot.
=== Installation
To benefit from Tamaya Spring integration add the following dependencies to your module:
[source, xml, subs=attributes+]
-----------------------------------------------
<dependency>
<groupId>org.apache.tamaya.ext</groupId>
<artifactId>tamaya-spring</artifactId>
<version>{tamaya_version}</version>
</dependency>
-----------------------------------------------
=== Configuring Tamaya Spring Configuration
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
@EnableTamayaConfig
public class SampleWebFreeMarkerApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleWebFreeMarkerApplication.class, args);
}
}
--------------------------------------------------------
=== 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:
[source, java]
--------------------------------------------------------
@EnableTamayaConfig(extendOnly=true)
--------------------------------------------------------
=== Configuring your 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]
--------------------------------------------------------
@ConfigSection("app.root") // optional <1>
@Component
public class ConfiguredSpringBean {
@Value("${application.message:Hello World}") <2>
private String message;
@Autowired
private Environment env;
@Config(value = "alternateMessage", required = false) <3>
private String anotherMessage = "N/A";
@Config("java.version")
private String javaVersion;
@Config(value={"number", "testNum", "[notavailable]"}, defaultValue="23") <4><5>
private int testNumber;
...
}
--------------------------------------------------------
<1> You can configure default section prefixes. This is an optional feature.
<2> Tamaya does not require you to change your code. You can still work with
Spring injection for your configuration, but Tamaya will override Spring
configuration by default.
<3> You can also define entries as optional, which allows you to perform
default inialization using Java idoms.
<4> Tamaya allows you to define an ordered list of key candidates, which are
combined with the section prefix, if present, to the full keys. Keys added
in brackets ([]) are interpreted as absolute keys, so the example above
the key candidate list evaluates to +app.root.number", "app.root.testNum",
"notavailable"+.
<5> You can configure default values used, if no other value can be evaluated
for the given keyset.
Summarizing you get all the nice features of Tamaya out of the box running
with your Spring code.
NOTE: For a full description of Tamaya's injection API please
refer to the link:extensions/mod_injection.html[corresponding documentation].
=== Disabling Tamaya Injection
If you dont like Tamaya injection, you can simply disable it:
[source, java]
--------------------------------------------------------
@EnableTamayaConfig(disableTamayaInjection=true)
--------------------------------------------------------