We are using JDeveloper 11g.
Both a Model and ViewController project that makes use of ADF fusion and what not.
My web-xml has an EJB reference for a stateful session EJB .
I have a managed bean that's declared in faces-config.
I am trying to access a EJB from inside the managed bean.
I declare
#EJB (name ="LocationServicesEJB")
private LocationServicesEJB locationServices;
When accessing this in a method, the locationServices is null.
I do not see any JNDI type problems in the console,
so I imagine that it's not even bothering to lookup.
Do I need to enable injection or something?
Or do I need to define 'locationServices' as managed property in my faces-config?
Please advise.
Thanx
You are confusing two different types of injection.
There is EJB injection that happens among EJBs by the respective annotation. This type is handled by the EJB container (your application server or OpenEJB e.t.c)
Then there is "normal" injection (ala Spring) that happens between normal Java beans
and is defined in faces-config. This type is handled by JSF.
So decide what you want to do.
My proposal would be to download the official Java EE tutorial and skim through all topics.
Related
I was wondering if there was a way using Quarkus to see how beans are being pulled in. Like a tree or graph showing the hierarchy of bean resolution. I have a few beans that I think should be considered "unused", but they are still pulled in (I know because they fail with missing config properties).
That's an interesting idea. Would you care to file a new issue here: https://github.com/quarkusio/quarkus/issues?
By the way, "unremovable" beans do not need to be injected anywhere. There are other rules that apply. For example, a #Named bean or a bean that declares an observer method is never removed. Furthermore, Quarkus extensions can add their own rules.
Added enhancement issue to github: https://github.com/quarkusio/quarkus/issues/26065
PR: https://github.com/quarkusio/quarkus/pull/26153
Will be built to 2.11
I just started a quarkus proof of concept. The containers-start time is amazing!
Right now, I'm working on the Dependency Injection part. And figuring out the options.
https://quarkus.io/blog/quarkus-dependency-injection/
My preferences are:
I prefer constructor injection. (This has been going ok).
I prefer "java config" so I can follow the "Composition Root" pattern of putting all my application dependency injections in a common place. (See https://blog.ploeh.dk/2011/07/28/CompositionRoot/ )
With Spring DI, this is done with the
org.springframework.context.annotation.Configuration
and declaring the Beans there.
Aka, I prefer not to place "#ApplicationScoped" annotations all over my classes.
Does CDI/Quarkus support a "java config" model? The reason I ask about quarkus is that I read quarkus has a limited CDI implementation.
//start quote//Our primary goal was to implement a supersonic
build-time oriented DI solution compatible with CDI. This would allow
users to continue using CDI in their applications but also leverage
Quarkus build-time optimizations. However, ArC is not a full CDI
implementation verified by the TCK - see also the list of supported
features and the list of limitations.//end quote
So my question isn't a solely CDI question.
I've tried different internet search terms, but they keep showing me Spring links. :(
You should create a CDI bean that will produce your beans, this is the standard CDI approach to what Spring calls Java Configuration.
So something like this
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
#ApplicationScoped
public class MyConfiguration {
#Produces
public MyBean myBean(){
return new MyBean();
}
}
I have a multi module maven project (a Java EE app with ear, persistence, web, ejb, api, bl and other modules), where I'd like to use Weld CDI.
When I try to inject a bean, where will Weld search for injectable beans? Will Weld search in all the modules, where a beans.xml is placed to WEB-INF or META-INF regardless from their dependencies (or dependencies matter somehow?), or will Weld be able to inject a bean from an other deployment? I tried to find a document, which describes the mechanism and boundaries of bean lookup, but I haven't found anything yet.
What you're describing is part of the overall EE spec, not the CDI spec. but yes, in general the contents of your EAR will be discovered as distinct applications (per the EE spec). This means that you may not be able to access individual beans across JARs. It is highly recommended to use WAR deployment with CDI.
it's been a while now and still there are several things that are not clear to me.
Lets start with the basics: what I need to do.
I'm building a web application that manipulate some data, do some computation and give the user an output. There will be also a standalone version, with some simplifications.
So I thought this is the right time to use EAR packaging structure and EJB. I've created with netbeans a basic j2ee ear project with maven support:
All of them have their own pom.xml.
For what i understand, the first one is just a wrapper, the second one is the "real" project that doesn't contain source code, but encapsulate the other two.
In the web project I put all of the web stuff plus jsf backing beans. In the ejb one I was planning to put my data model with all the required annotations, and that comprehend also JPA and JAXB. There should be also some additional classes: a facade class, or session bean, that allow me basic data manipulation with the database, and some classes that contain my business logic strictly related to my data.
Question: is it correct to put all this things into an EJB project? Why not a simple project that the web module depends on?
Moving forward: the web project. First things is, netbeans doesn't put the EJB project dependency into its pom. So I can't see my beans from here. Is that correct or I have to manually add the SRA-ejb into the pom as a dependency?
Secondly, using some netbeans macro, like the ones that generate session beans from entity, or CRUD jsf pages from entities, it looks like he try to generate and put session beans here, in the web project. I think they fits better in the ejb one. What do you think?
One last question: in my stand alone application I was planning to use just the ejb module. But I think I have to carry with me an EJB container, am I right?
it's been a while now and still there are several things that are not
clear to me.
I know that feeling for sure :D
All of them have their own pom.xml. For what i understand, the first
one is just a wrapper, the second one is the "real" project that
doesn't contain source code, but encapsulate the other two.
Correct, it doesn't contain any sourcecode but can contain XML files for declarations and a common lib folder which contains shared libraries of the web and EJB projects.
In the web project I put all of the web stuff plus jsf backing beans.
In the ejb one I was planning to put my data model with all the
required annotations, and that comprehend also JPA and JAXB. There
should be also some additional classes: a facade class, or session
bean, that allow me basic data manipulation with the database, and
some classes that contain my business logic strictly related to my
data.
Question: is it correct to put all this things into an EJB project?
Why not a simple project that the web module depends on?
I don't know what you mean with a "simple project", if you mean just a plain JAR file with classes: an EJB module is nearly the same plus a deployment descriptor which invokes scanning of the annotations.
Moving forward: the web project. First things is, netbeans doesn't put
the EJB project dependency into its pom. So I can't see my beans from
here. Is that correct or I have to manually add the SRA-ejb into the
pom as a dependency?
Yes, in your case you need this dependency. You web project depends on your "service".
Secondly, using some netbeans macro, like the ones that generate
session beans from entity, or CRUD jsf pages from entities, it looks
like he try to generate and put session beans here, in the web
project. I think they fits better in the ejb one. What do you think?
I think it makes more sense to put them in the EJB project because they come from the javax.ejb.* package and belong to the EJB layer. You can put them in the we project, but then you don't really need an EAR and you can use a normal web project like you did before.
One last question: in my stand alone application I was planning to use
just the ejb module. But I think I have to carry with me an EJB
container, am I right?
No, you don't need an EJB container. You can lookup your EJBs via JNDI but you'll need an EJB with a remote interface and a copy of this interface in your standalone client.
You can find some tutorials about that here:
EJB invocations from a remote client using JNDI (JBoss)
How do I access a Remote EJB component from a stand-alone java client? (GlassFish)
GlassFish Project - Simple EJB 3.0 Stateless Session Bean Example
See also:
Maven2: Best practice for Enterprise Project (EAR file)
EAR package structure
Maven structure and Java EE applications
I'm in the middle of porting a JSF 2.1 application to Jboss EAP 6.1 (JBoss AS 7.2). The application runs smoothly on Glassfish 3.1, and Weblogic 12c. Moreover a slightly similar application was ported successfully.
The problem occurs in a specific EJB singleton. In this, I have a method with #PostConstruct annotation, which runs at startup and fills a List with SelectItems. During deploy, a java.lang.ClassNotFoundException exception is thrown, which says JBoss can't find the javax.faces.model.SelectItem class.
If I comment out the method, everything is fine. The SelectItem class is used in some other beans in the web module without any problem.
My question is: Is there any known reason for this behaviour? Do I need some special configuration to use classes from the javax.faces package in an EJB module, running on JBoss?
JSF is not packaged with the EAR, so the application uses the server's implementation.
Thank you!
You've there a design mistake. The business service (EJB) tier is not supposed to have any web (JSF) tier related dependencies. Having javax.faces.* imports in an EJB class is in first place already wrong. It makes the business service unreusable across different web tiers such as JAX-RS, Wicket, Struts, plain servlets, etc. In well designed application servers the unability to reach web tier classes from the EJB tier on is already enforced by (Java EE default) restrictions in classloaders.
Keep the EJB free of JSF dependencies. Perform the desired job in an eagerly initialized application scoped JSF managed bean instead.
#ManagedBean(eager=true)
#ApplicationScoped
public class App {
private List<SelectItem> someItems;
#EJB
private AppService service;
#PostConstruct
public void init() {
// Populate your List<SelectItem> here.
}
}