It is possible to use Google Guice (any other DI framework) for injecting EJBs in Java EE 5? By default Java EE 5 uses JNDI to injecting EJBs.
I have no direct experience with Guice - but yes, it's possible to use other frameworks for injecting EJBs in a JEE5 application. For instance, Seam does just that. So in principle, it should be possible as there isn't an inherent restriction in the kind of objects that can be injected, as long as the framework takes care of all the lookup details.
UPDATE:
Take a look at this post detailing how to inject EJBs using Guice.
Related
I know that #postconstruct annotation is used when we want a method to call at the time of creation of its object after constructor. But my question is can we use it inside OSGI service?
It depends on the dependency injection framework you use. If you use declarative services the equivalent would be #Activate. A good example is the enroute microservice example.
If you use Aries CDI then indeed #PostConstruct works. Unfortunately there are not yet good examples for this as it is still quite new but it works for the most part like normal CDI in Java EE.
I have seen a Spring application, and it uses spring dependency injection in addition to that some places I find Java Dependency injection. I am not sure why Java DI is used. I googled it, however, I can’t find an exact answer.
Anybody can explain why Java DI is used in addition to Spring DI?
Dependency Injection in Java is a way to achieve Inversion of control (IoC) in our application by moving objects binding from compile time to runtime. We can achieve IoC through Factory Pattern, Template Method Design Pattern, Strategy Pattern and Service Locator pattern too.
Spring Dependency Injection, Google Guice and Java EE CDI frameworks facilitate the process of dependency injection through use of Java Reflection API and java annotations. All we need is to annotate the field, constructor or setter method and configure them in configuration xml files or classes.
This link may help you more
To be simple, If you need Dependency Injection through Java you need to implement using some Design Patterns, which is time consuming. Frameworks like Spring helps to implement DI using annotations and configurations which are developed using specific design patterns. As a developer we need not worry about DI, we can just concentrate on our business logic.
Since JSF managed beans are not part of a bean container, who manage them for example like Spring beans. Also how managed bean achieve the dependency injection with out a bean container?
How JSF would be a better replacement for Spring with out a bean managed container and with other functions Spring provides?
JSF is basically an MVC framework focused in the view layer. When comparing it with Spring, it could be an equivalent to Spring MVC, but never to the whole Spring framework, which has much more functions.
For your question of who manages the dependency injection, the own JSF framework used to do that, for me, as an example, I'm using Tomcat which is just a servlet container, so I use JSF for that work (this means I can only inject other managed beans and not a Service or a DAO).
In other cases, for JavaEE servers as JBoss or Glassfish, the server itself has an embeded container (which acts like the Spring framework) so you could delegate the injection to its CDI implementation.
Finally you tell about JSF being an Spring replacement. As I've said before, this makes no sense. JSF is a part of JavaEE specification and it's being oriented to be used with CDI injections. Spring MVC, being part of the Spring framework, could be the best choice if you manage your application with Spring instead of with a JavaEE container, although you could also use JSF instead of it.
Anyway, remember there's always the discussion of going with Spring or JavaEE. The first one, being a non-standard, offers a faster support and improvement releasing. On the other hand, JavaEE follows the Java specification, with all the benefits of that.
See also:
Spring 3.0 vs Java EE 6.0
Using JSF as view technology of Spring MVC
This question already has an answer here:
Difference between Request MVC and Component MVC [closed]
(1 answer)
Closed 6 years ago.
I haved used Struts framework in all my past applications and for the new application,my client has requested to use either Spring MVC or JSF? I am not familiar with these two frameworks but our timelines are strict. So, I am not sure which framework I will choose to build the application.
Can anyone please suggest me which framework will be easy to learn in quick time?
Thanks
Of course, it's going to be different for everyone, but I'd suggest Spring MVC, as it's a request-based framework like Struts. Of course, you'll want to learn about core Spring stuff like Inversion of Control / Dependency Injection (but I'd consider that a plus...) and whatever you're going to use for database access (just JDBC? Hibernate? iBatis? etc.).
JSF is component-based, which is a bit different paradigm from request-based frameworks. If you do plan to go the JSF route, I'd suggest looking at Seam from JBoss. It's more of a front-to-back framework that uses JSF as the web/presentation end and EJB as the backend. And pretty much all the people who've used it claim it makes JSF and EJB more usable than they are by themselves.
Good luck on whichever technology you choose, though. (Sounds like you'll need it - strict timelines and a client that's prescribing web frameworks?)
I'd suggest SpringMVC, because of the timeframe:
you need something with less steep learning curve. SpringMVC is more like Struts than JSF
in order to use the power of JSF you need to get familiar with many "tricks", while SpringMVC is more or less straightforward
I'd suggest JSF + Primefaces component library. I am using this combination to build most of our projects. As I remember, I spent one week to learn the technology and finished my first project in one month. The development time at least 30% faster than Struts.
SpringMVC is not a bad technology and it's quite popular.
Really depends on which one your like the most.
JSF is just the view layer of the MVC and wil need to be used with other technologies like Spring/Hibernate or EJB for a full MVC.
I have been using the Spring MVC for about 1 months now, whilst it's probably not the latyest version of SpringMVC I've found it a little annoying that we have so much XML to deal with. All the managed beans and DAO has XML config to it. Also everything seems to have to go thorugh a method called onSubmit().
JSF with something like EJB is far simplier in my opinion... Everything can be done using Annotations so simply use #ManagedBean=theBean in your backing bean and in your JSF put {thebean.param} and you have access to the backing bean's data. Also you can use the Session beans of your EJB as the backing beans for JSF then have direct acces to the DAO (Model layer) Entity bean. Again simply by using the #Entity annotation and the EntityManager class
Spring MVC is a web framework inside the Spring framework. It does provide features as those in JSF 2.0:
ajax-support
validation
dependency-injection etc
Yet, you can use Spring (not Spring MVC) together with JSF 2.0, with spring providing the dependency-injection, aop, transaction management mechanisms, and JSF providing the web layer.
Of course, you'll want to learn about core Spring stuff like Inversion of Control / Dependency Injection (but I'd consider that a plus...)
JSF is indeed based on IoC, and much simpler than the Spring learning curve.
A friend and I are building a fairly complex website based on java. (PHP would have been more obvious but we chose for java because the educational aspect of this project is important to us)
We have already decided to use JSF (with richfaces) for the front end and JPA for the backend and so far we have decided not to use EJB3 for the business layer.
The reason we've decided not to use EJB3 is because - and please correct me if I am wrong - if we use EJB3 we can only run it on a full blown java application server like jboss and if we don't use EJB3 we can still run it on a lightweight server like tomcat. We want to keep speed and cost of our future web server in mind.
So far I've worked on two Java EE projects and both used the full stack with
web
business logic
factories/persistence service
entities
with every layer a seperate module.
Now here is my question, if you dont use EJB3 in the business logic layer. What does the layer look like? Please tell what is common practice when developing java web projects without ejb3? Do you think business logic layer can be thrown out altogether and have business logic in the backing beans? If you keep the layer, do you have all business methods static? Or do you initialize each business class as needed in the backing beans in every session as needed?
EJB3 is split into two layers:
persistance (JPA, old Entity beans)
business logic (old Session beans)
You can have the same architecture, without using explicitely EJB3.
Persistance
JPA is very close to Hibernate. We use, and prefer it to JPA.
The annotations come from JPA, but there is no need for a container, it's not really EJB.
Business
Spring also is very close to the Business layer of EJB3. With many more capabilities...
Some even say they are better than EJB3 !! ;-) They say that EJB3 was created from these two solutions (but still have to reach at their level on many points!).
EJB enhanced a lot over the last 5 years. Especially with version 3 they became a lot more lightweight.
Java EE 6 has a web-profile that makes it possible to even run on a tomcat server a Java EE stack. http://www.oracle.com/technetwork/java/javaee/resources-jsp-139799.html
Further it is possible to install all the necessary components on tomcat:
http://tomee.apache.org/apache-tomee.html
I have an example project including EJBs on github that shows how to use them:
https://github.com/ManuelB/facebook-recommender-demo
Further I would recommend the screencasts of Adam Bien:
http://www.adam-bien.com/roller/abien/
If redeployment times are the reason why you aren't using EJB you should look into:
http://zeroturnaround.com/software/jrebel/
To still answer your question you should look into the following Java Web Frameworks as EJB alternatives:
Apache Wicket https://wicket.apache.org/
Apache Tapestry http://tapestry.apache.org/
The play framework http://www.playframework.com/
Spring http://projects.spring.io/spring-framework/
People that don't use EJB usually use Spring or another dependency injection framework. JSF allows you to create beans and wire them together, however it would be better to use this only for the UI and use Spring for your business layer. A lot of information is available for integrating JSF with Spring.