Is there any Spring #Required annotation equivalent in EJB 3.0? - dependency-injection

Is there any equivalent annotation in EJB for #Required (Spring)? I do dependency injection using setters and I want to be sure that resource was injected (almost no probability of NullPointerException ;)). In Spring it is easy:
#Required
public void setProperty(Property p) {
this.property = p;
}
Is there any way to do such a validation in EJB? (Maybe some other solution than annotatations). Thanks

In ejb injection is done via #EJB and #Resource (as stated above).
If the bean for the given (or auto-generated) name doesn't exist you get an error from the container (in many cases this happens at deployment time).
The only way to (maybe) get a nullpointer exception inside a ejb bean is if you try to access an injected object in the default constructor. By spec injection happens after the constructor and before the #PostConstruct lifecycle is called.

Related

CDI failure with Dependent/applicatio Scope class

I am trying to Inject ServletContext in my dependant scope class but it always gives the failure.
java.lang.IllegalStateException: No CXF message usable for JAX-RS #Context injections in that thread so can't use interface javax.servlet.ServletContext
I am not able to undestand the reason that why can't I inject it here. I am using a producer method and when inside producer method,I try to access ServletContext obj then it gives above exception. I have also checked if the Injected servlet context is null but it is not null. But When I call any method using it for example sc.getContextPath() it gives the above exception. Below is the code snippet:
#Dependent
public class AuthContexthandler {
#Context
ServletContext sc;
#Produces
JWTAuthContextInfo getInfo() {
try{
System.out.println(sc.getContextPath()); //here I get the above mentioned error
//rest of the code
}catch(Exception e){
e.printStackTrace();
}
}
}
you must use #inject instead of #Context.
you can use #Context to inject object instances related to the context of HTTP requests into to JAX-RS source class and as AuthContextHandler(as #Christoph Böhme said) is not a JAX-RS source class so you cannot use #Context
but as http://docs.jboss.org/weld/reference/latest/en-US/html_single/ says:
An object bound to a lifecycle context is called a bean. CDI includes
built-in support for several different kinds of bean, including the
following Java EE component types:
managed beans, and EJB session beans. Both managed beans and EJB
session beans may inject other beans. But some other objects, which
are not themselves beans in the sense used here, may also have beans
injected via CDI. In the Java EE platform, the following kinds of
component may have beans injected:
message-driven beans,
interceptors,
servlets,
servlet filters and
servlet event listeners,
JAX-WS service endpoints and handlers,
JAX-RS resources,
providers and javax.ws.rs.core.Application subclasses, and
JSP tag handlers and tag library event listeners.
it means you also can use #Inject annotation in your JAX-RS source class.
there are also some predefined Beans in CDI such as ServletContext that you can use #inject annotation to inject them.
https://docs.jboss.org/seam/3/servlet/latest/reference/en-US/html/injectablerefs.html
https://docs.oracle.com/javaee/7/tutorial/cdi-adv004.htm

Inject singleton bean into session bean via remote interface, object always "null"

I need to inject a singleton bean into the session bean. Below are the corresponding classes. The problem is that the injected object is always null. I tried all of the JNDI lookup strings which my JBoss 7.0.1 server showed me during startup (i.e. JNDI bindings for session bean named GlobalBean in deployment unit subdeployment .. of deployment .. are as follows: ..). I also tried commenting out the #EJB annotation in GlobalBean.java and also tried to use the "ejb/GlobalBean" during injection. However, no luck. What could be the reason? Thx.
GlobalBean.java:
#Startup
#Singleton
#Remote(GlobalBeanRemote.class)
#EJB(name="ejb/GlobalBean", beanName="GlobalBean", beanInterface=GlobalBeanRemote.class)
#ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
public class GlobalBean implements GlobalBeanRemote
{
// CODE
}
SessionBean.java:
#Stateful
public class SessionBean extends ParentBean
{
#EJB(name="java:module/GlobalBean!project.framework.interfaces.GlobalBeanRemote")
private GlobalBeanRemote globalBeanAPI3;
// CODE
}
In your SessionBean class try changing name attribute of #EJB to mappedName.
#EJB(mappedName="java:module/GlobalBean!project.framework.interfaces.GlobalBeanRemote")
This will, of course, only work if your two beans are in the same module.
Update
Given that your beans are in separate modules, try using the java:app namespace:
#EJB(mappedName="java:app ...")
The java:app namespace is used to look up local enterprise beans packaged within the same application. That is, the enterprise bean is packaged within an EAR file containing multiple Java EE modules. JNDI addresses using the java:app namespace are of the following form:
java:app[/module name]/enterprise bean name[/interface name]
Also try removing GlobalBean's #EJB annotation. #EJB is used to define a dependency.

Why does the #PostConstruct method not get called using BeanManager.getReference() to obtain an instance?

We are currently moving from JSF-ManagedBeans to CDI. Unfortunately we have made excessive use of the EL-Resolver in the past in order to gain static access to session scoped beans managed by JSF.
Since CDI dependency injection is not available everywhere I rewrote the existing static lookup to make use of the BeanManager (Using SEAM-Solder extending BeanManagerAware).
Iterator<Bean<?>> iterator = beans.iterator();
Bean<T> bean = (Bean<T>) iterator.next(); // possible NPE, I know :)
CreationalContext<T> creationalContext = beanManager.createCreationalContext(bean);
T contextual = (T) beanManager.getReference(bean, type, creationalContext);
return contextual;
The code works and returns a container managed instance of the desired bean. BUT: the methods annotated with #PostConstruct do not get called using getReference(). Perhaps you guys know how to do it. Couldn't find anything googling the issue :-/
Best regards!
You should be using Application#evaluateExpressionGet() for this. Not only for CDI beans, but actually also for JSF beans you previously had.
FacesContext context = FacesContext.getCurrentInstance();
Bean bean = (Bean) context.getApplication().evaluateExpressionGet(context, "#{beanName}", Bean.class);
// ...
Much cleaner, however, is to just use CDI's #Inject or JSF's #ManagedProperty instead.
See also:
Get JSF managed bean by name in any Servlet related class

JBoss 6: Injecting EJB into servlet

Folks,
I am very annoyed by having to re-learn and waste time with this stuff every time a new version of JBoss rolls around.
I have a stateless EJB that is discovered and declared in the JNDI space:
10:01:53,044 INFO [org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
DTalk/UserManager/local - EJB3.x Default Local Business Interface
DTalk/UserManager/local-com.doctalk.ejb.UserManagerLocal - EJB3.x Local Business Interface
I need to use this EJB in a servlet which is part of a war which is part of the EAR that contains the EJB. I'd like to do it using injection.
When I use the most intuitive notation:
#EJB
private UserManager userManager;
I get an exception in JBoss logs.
When I use a more flowery notation such as:
#EJB( mappedName = "UserManager" )
private UserManager userManager;
Or
#EJB( mappedName = "DTalk/UserManager/local" ) // EAR is called DTalk
private UserManager userManager;
I get no injections errors in jboss but the injected bean is null.
This is maddening and a huge waste of time and makes me question why I don't dump the Eclipse/jboss tools franchise in favor of NetBeans and GlsssFish.
Any insights appreciated.
Thanks.
You are trying to inject (a proxy to) the bean instance itself, instead of its interface.
Yet, according to the deployment logging you've shown, you have only declared the bean to be bounded in JNDI via its (local) interface. In order to make the injection happen, you should either declare the variable in which you're injecting as the interface:
#EJB
private UserManagerLocal userManager;
OR declare that a no-interface view should be created for your bean:
#Stateless
#LocalBean
public class UserManager implements UserManagerLocal {
...
}
after which you can declare the variable as you did earlier:
#EJB
private UserManager userManager;

Injecting entity manager into managed bean

It is possible to inject entity manager (or its factory) into jsf managed bean using #PersistenceContext (or #PersistenceUnit)?
I Tried it but nothing, I obtain a NullPointerException.
Yes it is possible. This is the syntax.
#PersistenceContext
EntityManager em;
You need to have a persistence.xml in your project. Btw: I'm running Glassfish 3.
After this you can then use methods like em.createNamedQuery.
Also remember the injection takes place after the constructor so if your trying to do database functions in the constructor this will not work. You will have to add the #PostConstruct annotation to a method. This is probably the problem your having.

Resources