Search code examples
javajakarta-eejbosscdi

Can I inject with 2 interfaces with same name in JavaEE 8?


My project techstack is: JavaEE 8, Wildfly, Jboss and structure like this:

companycontext
  - company
  - workplace
  - employee
  - web (beans.xml here)

I have EmployeeAdapter interface in both company and workplace project. Because CDI will discover every bean in every project so I'll get an error

A component named 'EmployeeAdapterImpl' is already defined in this module

For now my solution is name like: ComEmployeeAdapter, WorkEmployeeAdapter... Can I use something like @Named

// Implement
@Stateless @Named("company")
EmployeeAdapterImpl implement EmployeeAdapter

// Using
@Inject @Named("company") private EmployeeAdapter

// Implement
@Stateless @Named("workplace")
EmployeeAdapterImpl implement EmployeeAdapter

// Using
@Inject @Named("workplace") private EmployeeAdapter

I think it's much better than a rule to name our adapters.

Thank you very much.


Solution

  • This is an EJB issue, not a CDI issue.

    CDI bean type are defined by a fully-qualified class name, so there is no problem having two beans with the same name in different packages.

    Since your two EmployeeAdapterImpl beans are stateless session beans, the EJB container will generate JNDI names for them. You can see them in the WildFly logs in a message like

    16:09:39.812 [MSC service thread 1-8] INFO  org.jboss.as.ejb3.deployment - WFLYEJB0473: JNDI bindings for session bean named 'Foo' in deployment unit 'deployment "myapp.war"' are as follows:
    

    One of the generated names has the form

    java:module/EmployeeAdapterImpl
    

    taking only the simple class name, so this is causing a conflict in your case.

    You might want to try the name or mappedName element of the @Stateless annotation.