javamoney-examples
javamoney-examples copied to clipboard
How to inject a custom SPI using Spring Injection
From https://github.com/JavaMoney/jsr354-ri/issues/198, as it is not really the place in the Moneta issue tracker.
Having to modify a 3rd-party .jar, as to having to edit the *.properties file, is not something that I would pursue, i.e., JavaMoney API CDI example. That is why I asked if you have considered a different path, using Spring. Spring DI is an application of the CDI concept, just using a known and popular framework.
The Java ServiceLoader concept exists ever since Java 6, it is a popular framework within the JDK: https://docs.oracle.com/javase/7/docs/api/java/util/ServiceLoader.html
It is theoretically possible for you to combine them as described here: https://dzone.com/articles/java-service-loader-vs-spring-factories-loader
But as a dependency in the RI Spring seemed unreasonable. Spring also uses Moneta, that could have lead to circular dependencies or other dependency issues.
And of course you don't have to patch or put anything into the Moneta JARs ;-O These extension modules add exchange rate providers e.g. for Yahoo: https://github.com/JavaMoney/javamoney-lib/tree/master/exchange
They show, how to use the service loader to add new exchange rate providers.
Thank you. I will take a look at these recommendations.
I tried to follow the examples. I wanted to use the ServiceListFactoryBean but it did not work with spring boot/java 8. Somehow the ServiceLoader invoked by the bootstrap is always winning. It seems a timing issue. Even though I see my defined ServiceListFactoryBean and my own xxxProviderSpi, once I am inside the spring application, and I call the bean ServiceListFactoryBean directly, it only returns the providers defined in META-INF/services. Monetary also only recognizes the ones in META-INF/services.
Using spring.factories and also overriding the META-INF/services files works, but I was trying to avoid this.
I wonder if there is a way to reload the configurations. I mean, let the Money API do its thing, and before my app is available, reload all desired overrides.
On a related point, I do not see the public jars for moneta 1.2.*, only pom files. http://oss.jfrog.org/libs-release/org/javamoney/moneta/1.2.1/
Most of those are Spring issues. Spring Framework itself uses the ServiceLoader on various occasions and since it is a Java platform mechanism it usually has preference over extra layers of injection. Spring Boot is just a Fat JAR mechanism similar to running Jetty or Tomcat "Embedded" or the likes of Payara Micro, Thorntail (ex Wildfly Swarm) etc. They do not bring their own DI mechanisms but use those by Spring Framework, JSR330 or CDI. Using such DI does not override the ServiceLoader mechanism. Above examples showed how you may add or expose an SPI element via Spring. It allows you to control your own SPI element but it won't undo or override those already there. For exchange rate providers there are means like the "chain" for that.
Moneta 1.2+ is a Parent POM holding together the individual modules, so the parent has no JAR.
So, if I want to user moneta 1.2.1, how do I download a public .jar?, better said, what is the snippet to add to my pom.xml?
No Maven does that automatically. See http://javamoney.github.io/ri.html for the right snippet. Note, the version is 1.2.1 now, and what seems necessary even when you use dependencyManagement in all places is the type: pom. I'll update the website when I can.