开发者

Exclude/remove services from Dependency Indejection in JBoss

开发者 https://www.devze.com 2023-02-27 08:22 出处:网络
We currently have a project that consists of multiple applications as well as a base library. Both, the applications as well as the base library contain stateless EJBs and each application might intro

We currently have a project that consists of multiple applications as well as a base library. Both, the applications as well as the base library contain stateless EJBs and each application might introduce an EJB that inherits from a base library EJBand thus implements the same interface.

A short example:

In the base library we have:

@Stateless
@Local( IUserService.class ) 
public UserServiceBean implements IUserService {
  public void login(String user, String password) {...开发者_Python百科}
}

In the application we need to override login(...) and thus we have:

@Stateless
@Local( { ISpecificUserService.class, IUserService.class } ) 
public SpecificUserServiceBean extends UserServiceBean implements ISpecificUserService {
  public void login(String user, String password) { ... } //override
}

If I now have another EJB in the application that needs to have a reference to SpecificUserServiceBean I'd do @EJB ISpecificUserService userService;.

However, if there is an EJB in the base library, it would contain @EJB IUserService userService; and here's the problem:

We need to get the specific service in the application be injected in the base library EJB. However, there are two EJBs registered for the same local interface, which means that the container might return the base EJB or the specific EJB.

You could say "Don't add the base library jar as a module in application.xml" but that's not possible right now, since it contains other EJBs that need to be deployed. We could move the EJBs to be overridden to a different jar but since almost every EJB might be overridden, depending on the application, we'd end up with a jar per EJB.

Thus, I'd like to exclude or remove the base EJB from the dependency injection container if there exists a specific override. Since I know that at deploy time, I could also use a configuration file to tell the container not to load a specific EJB class.

I might add a service that modifies the container afterwards but that would be my last resort.

Do you have any ideas on what we could do?

Thanks in advance.

Btw, we're working on JBoss 4.2.3 with EJB 3.0.


The problem is that you're wiring your app partially in the base lib which is bad since you can't override this wiring.

So the solution is to remove @Stateless @Local( IUserService.class ) from UserServiceBean in your base lib; the base lib just provides default implementations for the beans but it must not wire them.

In your apps, you need this code:

@Stateless
@Local( IUserService.class ) 
public AppUserServiceBean extends UserServiceBean {}

to create the wiring. Move all those beans into a special package which you can copy into each app so you get the default wiring for everything.

0

精彩评论

暂无评论...
验证码 换一张
取 消