Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to inject a custom SPI using Spring Injection #34

Open
keilw opened this issue May 26, 2018 · 7 comments
Open

How to inject a custom SPI using Spring Injection #34

keilw opened this issue May 26, 2018 · 7 comments
Labels

Comments

@keilw
Copy link
Member

keilw commented May 26, 2018

From JavaMoney/jsr354-ri#198, as it is not really the place in the Moneta issue tracker.

@gsbtech
Copy link

gsbtech commented May 28, 2018

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.

@keilw
Copy link
Member Author

keilw commented May 28, 2018

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.

@gsbtech
Copy link

gsbtech commented May 29, 2018

Thank you. I will take a look at these recommendations.

@gsbtech
Copy link

gsbtech commented May 29, 2018

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/

@keilw
Copy link
Member Author

keilw commented May 30, 2018

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.

@gsbtech
Copy link

gsbtech commented May 30, 2018

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?

@keilw
Copy link
Member Author

keilw commented May 30, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants