On 09/10/2016 01:20, Matthew Madson wrote:
Hi there Project Jigsaw Devs,
I was viewing a recent talk posted to the vjug on Jigsaw and had a question
about cross module service dependencies.
Basically I was wondering if the functionality of the ServiceLoader could
be abstracted away using dependency injection (CDI or otherwise).
For example, if I have a CLI module which uses ServiceLoader to obtain
implementations of some dictionary service api, and one of the
implementations requires a DataSource, I would like to declare that
dependency on the DataSource in the ctor of the
DbBackedDictionaryServiceImpl perhaps with an @Inject annotation and have
(perhaps the module system?) automatically look for a suitable providers of
the DataSoruce using the ServiceLoader. Alternatively I could perhaps
declare @Inject on a Set or List of DataSource to support multiple
DataSources.
Basically I'm interested in moving the dependencies provided by the
ServiceLoader into the constructor to make them more explicit.
I suspect your mail/scenario can be interpreted in several ways. One
read is that you are describing the following scenario:
- A CLI module `uses p.DictionaryService`
- A provider module `provides p.DictionaryService with
q.DbBackedDictionaryServiceImpl`.
- In q.DbBackedDictionaryServiceImpl you have @Inject @DataSource.
- Some other module, the producer module, you have @Produces @DataSource.
(I'm assuming here that @DataSource is your annotation rather than
javax.sql.DataSource).
If this is the scenario then all I can suggest is try it out and report
back your experiences. I expect it should just work when the producer is
in the same module as q.DbBackedDictionaryServiceImpl. That is,
something like this should just work:
weak module dbprovider {
provides p.DictionaryService with q.DbBackedDictionaryServiceImpl;
requires weld.se;
}
assuming there is code somewhere to initialize the CDI container and
select the DataSource instance.
If they are in separate modules then I assume some configuration is
needed so that the CDI container will locate the producer. Also since
nobody will directly depend on the producer then you'll need to use
`--add-modules` to add it to the set of modules to resolve.
As I said, your mail can be read in many ways. I've read it that you are
replacing the use of SL in DbBackedDictionaryServiceImpl but maybe you
are actually looking to replace it in the CLI module. Alternatively you
might be asking if the module system could somehow know about the
@Inject/@Produces annotations so that service binding will automatically
locate producers and resolve them.
-Alan