How to dependecy inject an EJB3 constructor? - ejb

According to EJB3 DI documentation it is possible to inject fields and setters. But how to inject a bean constructor ?

The EJB specification does not support constructor injection. The EJB programming model only uses the no-arg constructor, and can then perform field or setter method injection after the instance has been constructed.
That said, EJB 3.1 is part of EE 6, which includes CDI. If your EJB module is a CDI BDA (bean deployment archive) because it includes beans.xml, then you can use CDI constructor injection.

Related

How to inject objects into groovy servlet?

First, no grails and no spring.
I just use groovy servlet (http://docs.groovy-lang.org/latest/html/api/groovy/servlet/GroovyServlet.html)
if I have a servlet named user_action.groovy, and I have UserDao.groovy, UserService.groovy, how can I inject services or daos into servlet please?
If you're using a servlet container that supports JNDI, such as Tomcat, you could potentially configure your UserDao class as a JNDI resource, and then access it via the JNDI directory lookup. That would provide a level of indirection, but relies on external configuration.
Another option is to include a DI framework such as Tiger or Guice if you're not interested in Spring based DI.
But injection by nature requires some form of Dependency Injection framework to support it. Otherwise you just go:
def dao = new UserDao()

Is Servlet a CDI/Managed Bean in Java EE

In a container environment (such as wildfly, jboss), are servlets treated as Managed bean? i.e. Can I inject the Servlet into any other CDI bean?
I use CdiRunner CDI-Unit to write my tests. And therefore I would like to inject Servlet into my Test class and test its (public) methods.
The lifecycle of a servlet if managed by the servlet container and not by CDI. However, CDI injection is expected to work in servlets.
A servlet container will also provide some built-in beans that can be injected using CDI:
A servlet container must provide the following built-in beans, all of
which have qualifier #Default:
a bean with bean type javax.servlet.http.HttpServletRequest, allowing
injection of a reference to the HttpServletRequest
a bean with bean type javax.servlet.http.HttpSession, allowing
injection of a reference to the HttpSession
a bean with bean type javax.servlet.ServletContext, allowing injection
of a reference to the ServletContext
If you need to inject a servlet somewhere, you are probably doing something wrong.

Starting a EJB dependency in CDI project

Well, i need use #Schedule (EJB) to put a task in crontab. I know that CDI dont have this (unhappy), just EJB. All my beans are CDI-Managed, so i have some doubts to include EJB inside this project, i hope that you can help me:
1) I'll create a new bean to use #Schedule, but i dont know if this bean should use #Singleton (EJB) or #Named + #RequestScoped (CDI). My ideia is that:
#Singleton
public class RoboFtp implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#Inject
private VideoBOImpl videoBO;
#Schedule(...)
public void coletarVideo(){
//do something each 3 seconds
}
}
Note that i have a CDI Bean inside a EJB Bean, this is possible ? This above bean is correct ?
2) How can i add the ejb to my pom.xml ?
3) If i add EJB to my project can have any conflicts ? Because my project is already in production.
To dispel your worries:
1) CDI Bean inside a EJB Bean, this is possible ?
CDI && EJB works pretty well together. In fact CDI will detect EJB beans and turn them into CDI beans as well. So if you create, say #Stateful bean, it will be picked up as CDI bean as well. That means you will be able to perform any CDI-related magic there while still having EJB features.
As for choice of annotation, #Singleton sounds reasonable; or probably #Stateful?. #RequestScoped would only live during request and die afterwards hence killing your periodic task. Choose scope based on nature of your task (one bean vs many/ short lived vs permanent/ ..). Just make sure you make it an EJB bean and CDI will follow.
2) How can i add the ejb to my pom.xml ?
Assuming you have Maven project, add a dependency on EJB api, the implementation will be provided by your Java EE application server.
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>ejb-api</artifactId>
<version>${desiredVersion}</version>
</dependency>
3) If i add EJB to my project can have any conflicts ?
Too general question I am afraid; I do not know your project. But I'll go ahead and say "no". At least as far as CDI and EJB goes, you should be able to deal with it.

Injecting Entity Bean in to EJB Bean using #EJB

I have Stateless Entity Beans(EJB 3.x) and EJB Beans(EJB 2.x) and other classes in my project. I want to inject Entity Bean into EJB Beans. So, far I am able to inject it using JNDI way i.e (BeanName#completename) but I want to inject it using #EJB just like we inject entity beans in another entity beans without any JNDI lookup. Both of these beans are part of different modules but deployed on same server. I am using weblogic with eclipselink.
Entity beans are never injects in EJB beans. You can perform your DB operation on entity beans by using EntityManager. To work with database operation entities must be associated with EntityManager persistence context. so you can not work with your entities until it is not in persistence state.
All the transaction-scoped persistence context are configured by injecting #PersistentContext to EntityManager objects , like this,
#PersistenceContext(name="PersistentUnitName")
private EntityManager entityManager;
Are you using EJB 3 or EJB 2? EclipseLink does not support EJB 2 EntityBean, only EJB 3 Entities.
You cannot inject an Entity bean, it does not make sense, they must be queried from the database through an EntityManager. You can inject an EntityManager using #PersistenceContext.
See,
http://en.wikibooks.org/wiki/Java_Persistence/Runtime#Java_Enterprise_Edition

Seam JNDI injection

How do you inject a JNDI value in Seam? I read that you can use #Resource or #JndiInject, but it is not working.
If you don't need to have an EJB as a Seam component, just use the standard InitialContext().lookup("something").
If you want to use an EJB that can be accessed from JNDI and to be able to use it as a Seam component, then its more complicated. See this.

Resources