I have a custom JBoss 7 module which provides services (for example, EmailService
for sending e-mails). I want to use these services in applications that are deployed on the same AS.
I specified jars of the service in module.xml
(located in modules/jboss/module/main
).
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="jboss.module">
<resources>
<resource-root path="email-service-api.jar" />
<resource-root path="email-service-impl.jar" />
</resources>
</module>
The email-service-api.jar
contains only interface of the service. I use this as a dependency for an implementation of the interface (in email-service-impl.jar
) and in the applications that use the service.
In email-service-impl.jar
there is a file named jboss.module.EmailService
(in META-INF/services
folder). The file contains fully qualified names of all my implementations (so far I have only one):
jboss.module.impl.DefaultEmailService
I would like to inject the service into an application.
Currently, I use producer method to get instance of the service from an application.
package bean;
public class Bean {
@Inject
EmailService emailService;
@Produces
public EmailService getEmailService() {
ServiceLoader<EmailService> emailServices = ServiceLoader.load(EmailService.class);
for (EmailService emailService : emailServices) {
if (emailService != null) {
return emailService;
}
}
return null;
}
}
When I leave out the producer method I get org.jboss.weld.exceptions.DeploymentException
saying WELD-001408 Unsatisfied dependencies for type [EmailService] with qualifiers [@Default] at injection point [[field] @Inject bean.Bean.emailService]"}}
.
I have jboss-deployment-structure.xml
file in the application:
<jboss-deployment-structure>
<deployment>
<dependencies>
<module name="jboss.module" services="export" />
</dependencies>
</deployment>
</jboss-deployment-structure>
And I have tried to add beans.xml
file into the "implementation-project" (i.e. email-service-impl.jar
), but it had no effect. The exception occured anyway.
Is it possible to inject the service and omit the producer method using CDI?
Thank you,
Denis
Summing up what I wrote in the comments:
@DefaultImpl
, @ProUser
etc).beans.xml
).Extension
here since they are executed as CDI boots therefore allowing you to register the implementations as beans.