How can I use #Nullable annoataion to suport Guice in JAVA11/JDK11? - annotations

Considering Guice forbids null by default, we have to use #Nullable annotations for that.
But how can I use #Nullable annoataions in java11/jdk11? Javax package are deprecated...

Related

Injection in entity listener with Quarkus

I'm trying to inject a bean into an entity listener in a Quarkus-application:
#ApplicationScoped
public class MyEntityListener implements Serializable {
#Inject
MyService service;
#PrePersist
#PreUpdate
public void checkWrite(BaseEntity entity) {
service.check(entity);
}
}
But service is always null. Changing scope to #SessionScoped has no effect.
According to this 2 SO-discussions, this should be possible:
CDI injection in EntityListeners
How can I use an EJB in a EntityListener?
I couldn't find any information about which JPA-version Quarkus is using, but since it is a state-of-the-art-framework I think it is JPA 2.1?
So should this be possible and if yes, what am I doing wrong?
I found a Quarkus-issue addressing this problem: https://github.com/quarkusio/quarkus/issues/6948
Seems like Quarkus is lacking support for this feature and maybe it will be implemented in the future. There's also a workaround described.

Issues with CDI when injecting generic type : Wildfly 8.2.0.Final

We are facing weird injection issues in Widfly due to CDI changes. We have interface
public interface Command<I, O> {
}
and many classes implement this interface like this
public class ApproveUserRequests implements Command<ApproveUserRequestsRequest, List<String>> {
}
Application listener classes likes to get list of all classes available and uses injection like this
#Inject
private Instance<Command<I, O>> mActions;
However instance returned by mActions were always null. After debugging source found that the only way to get list of all instances is to use
#Inject
private Instance<Command<?, ?>> mActions;
Also we faced injection issues while using generic types , however using wildcard type helped us.
- See more at: https://developer.jboss.org/thread/256783#sthash.1s6tuXsR.dpuf
The rules for parameterized types have been clarified in CDI 1.2. Have look at Section 5.2.4 Assignability of raw and parameterized types of the spec.

How to use Injection Annotation

Until to now i use (like beginner) JSF. Usually I make xhtml page and backend bean. And in backing bean I use annotation like #ManagedBean, #SessionScoped, #Application and so on. i think that is annotation for javax.faces.bean, if it is correctly that is faces annotations.
Bath I find one tutorial on internet where one guy use annotation like #Model an most interesting in backand bean He uses the #Inject annotation. I find that is very useful. He use injection for Logger class and in same bean can use methods from that class. Something like these:
#Model
public class FileUploadBean {
#Inject Logger log;
private Part file;
public void upload(){
...
}
}
Are there he use enterprise java, maybe CDI and how I can use this annotation. Because when i write #Inject in my Backing bean have message that is ambiguous annotation.
What i have to know and about what?
I working with eclipse and glassfish!
You're talking about the javax.enterprise.Inject annotation. See the cdi tag wiki page for some basic information. The only thing you'd need to have is the corresponding import statement.
A word of warning: if you already have a project with JSF beans, note that most scope annotations (#SessionScoped, #RequestScoped, ...) have a CDI equivalent that has the same name. Make sure you import the correct annotation or you'll find that your "session bean" suddenly has only request scope.
See also:
Weld Reference
JEE6 CDI Tutorial

Injecting Guice into JAX-WS POJO

I am using JBoss 7.1 and Java 1.6.
I would like to integrate a Guice service with by JAX-WS endpoint. Using the interceptor pattern described by Gunnar.Morling I am able to properly instantiate the Guice modules when using a stateless bean as a webservice. However i am not able to do the same with a simple POJO annotated webservice. Is this possible has anyone found a workaround. Below is a summary of my efforts so far.
#UsesGuice #Interceptor
public class GuiceInterceptor {
#Inject
private GuiceInjectorHolderBean injectorHolder;
#AroundInvoke
public Object aroundAdvice(final InvocationContext ctx) throws Exception {
if (ctx.getTarget().getClass().isAnnotationPresent(UsesGuice.class)) {
final Injector injector = injectorHolder.getInjector();
injector.injectMembers(ctx.getTarget());
}
return ctx.proceed();
}
}
The GuiceInjectorHolderBean is the a sinlgeton bean responsible for triggering the guice wiring. The annotation class required follows
#Retention(RUNTIME)
#Target(TYPE)
#InterceptorBinding
public #interface UsesGuice {}
the JAX-WS POJO class
#UsesGuice
#WebService(serviceName = "EchoServiceService", portName = "EchoServicePort", ame = "EchoServiceImpl", targetNamespace = "lala")
public class EchoServiceImpl implements EchoService
{
#Inject
MyGuiceInjection injection;
#Override
#WebMethod
public String sayHello(final String msg)
{
return "Hello " + injection.call(msg);
}
}
Thanks in advance
Dimitri
Your Current Approach
In your code, javax.interceptor annotations #Interceptor, #InterceptorBinding and #AroundInvoke are supported by CDI and EJB standards and not by Guice. Guice uses proprietary AOP interception via org.aopalliance.intercept.MethodInterceptor interface and calling the method AbstractModule.bindInterceptor.
So you're trying to bootstrap Guice injection on your endpoint by:
using a non-Guice interceptor on the endpoint's method
within the #AroundInvoke method, programmatically invoking the Guice Injector, with injection target being the intercepted endpoint
That begs the Q, what to use for 1?
'Bootrap' Interception Mechanism for Your Current Approach
Obviously, an EJB interceptor works, as you've stated.
Other than an EJB or Guice AOP interceptor... an obvious alternative would be the standard, a CDI interceptor.
But that would make it all rather circular and heavy-weight... Why use CDI just to boostrap, so that you can configure and execute to your desired DI competitor: Guice?
Suggested Alternative Solution - JAX-WS Support for Manual Endpoint Instance Initialisation
If you want POJO web services, maybe consider back-tracking a bit, instead of interceptor-driven Guice initialisation, maybe this could be what you need:
javax.xml.ws.Endpoint.publish(String address, Object implementor)
Endpoint.publish javadoc
Initialise Guice in the standard way, use injector.getInstance() to construct your endpoint instance, and then use Endpoint.publish to set the endpoint instance against the port. The following gives a good example:
Using Guice 3 with JAX-WS in Java 6 outside web container

CDI Bean-Injection failed at Hazelcast map-store class

I'm using JBoss AS 7.1 and leveraging Contexts and Dependency Injection. There is no spring involved here.
My question is how can i inject a dependency into a hazelcast MapStore implementation? Might there be a programmatic way? Any help is appreciated.
For instance
public class ClientRepositoryCache implements MapStore<Integer, ClientItem> {
#Inject
ClientRepository repository;
#Override
public ClientItem load(Integer clientNumber) {
return repository.getClientById(clientNumber);
}
}
At the moment Hazelcast supports dependency injection using only Spring. Instead you can use MapStoreFactory which gives ability to create your own MapStore instance.
See a related Hazelcast group post;
MapStore/MapLoader configuration
...
To integrate with Guice, for example, you can supply the name of a singleton MapStoreFactory implementation that is statically injected with enough information to implement newMapStore(String name, Properties properties) with Injector-aware logic.
If you use programmatic configuration, as I do, you can avoid the static injection by passing an already-injected factory to MapStoreConfig.setFactoryImplementation.
-Tim Peierls-
See also MapStoreFactory and MapStoreConfig javadocs.
https://github.com/hazelcast/hazelcast/issues/440
This works very well! Integration with CDI done with a CDI Extension.