문제

I have a requirement, my current project is with ejb3, jpa and jsf. I have a core development project and customer specific project. In the customer specific project, we planned to inherit the core classes and extend or override the core functionality. So we planned to create the customer specific classes with some customer prefix. So in the final war file, we will have the core class and all customer prefix classes. For example, if Authenticator is a core class and XXAuthenticator, YYAuthenticator are customer specific classes exist in the build. So, for example if i have a line of code in a bean like this:

@Inject Authenticator authenticator;

Can i programmatically and/or dynamically inject the inherited classes based on some logic like logged in user has a customer specific functionality.

what i am expecting is, i dont want to change the above line to inject, because it will be big change in every class. But expecting some kind of dynamic logic or configuration file to change the core class injection to customer specific class..

So finally with out touching the @Inject Authenticator authenticator; line. Can I inject the xxAuthenticator or YYAuthenticator through some logic? We dont have Spring in project technology stack.. So please suggest me with out spring only.. Thanks in advance

도움이 되었습니까?

해결책 2

In CDI this is done with "producer methods".
It would look like this:

@ApplicationScoped
public class AuthenticatorProducer {
   private Authenticator xxAuthenticator; // = ...
   private Authenticator yyAuthenticator; // = ...

   @Produces
   public Authenticator getAuthenticator() {
        if (someCondition) {
            return xxAuthenticator;
        } else {
            return yyAuthenticator;
        }
    }
} 

No need to change injection points and Authenticators don't have to be CDI beans themselves.

다른 팁

It sounds like your use case is more around Qualifiers. If a user follows a certain, you should be injecting different qualified versions of classes to use, no?

@Inject
private Instance<SomeService> someServiceInstance;


// later on...

SomeService someService = null;

if(someCondition) {
    someService = someServiceInstance.select(new FooLiteral()).get();
}
else {
   someService = someServiceInstance.select(new BarLiteral()).get();
}

Where FooLiteral and BarLiteral are annotation literals for @Foo and @Bar, which are qualifiers.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top