Whats the relationship between Spring and javax.enterprise.inject? - spring

I was reading a Wikipedia article about Java EE application servers here:
http://en.wikipedia.org/wiki/Java_Platform,_Enterprise_Edition#Java_EE_5_certified
It says that 2 APIs that Java App Services implement are:
javax.enterprise.inject
javax.enterprise.context
These both relate to application context and dependency injection JSR-299. I had never heard of these APIs before. Does Spring implement these APIs? Would it matter to anyone if they did?

JSR-330 defines a set of annotations (javax.inject) that are to be used across different dependency injection frameworks. The specification was lead by Rod Johnson (from Spring), and Bob Lea from (Google Guice)
(partly) because of the spec leads, spring and guice support this set of annotations
This is the part of JavaEE that is used by spring.
The same set is used by JSR-299, which is lead by Gavin King from JBoss. However, JSR-299 (also known as CDI) uses javax.enterprise.inejct/context and is a whole new dependency-injection framework. It is based on ideas of spring, guice and seam, but is specified formally as a JSR and aims at covering many corner cases as well as smooth integration with other JavaEE parts.
JSR-299 defines both an API and SPI so that concrete implementations can be developed. Current implementations are JBoss Weld, Apache OpenWebBeans and Resin CanDI.
So, to answer your question - there is no direct relation between javax.enterprise.inject and spring.

Spring does support JSR-330's #Inject - it can be used in place of #Autowired (except that it doesn't have a required property).
You also need to have the JSR 330 jar on the classpath.
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-autowired-annotation

Related

What is autowiring and AOP in Spring Framework?

I want to understand what is the exact meaning of term "autowiring" and AOP in Spring Framework.
From the official Spring Framework Reference:
The Spring container can autowire relationships between collaborating
beans. You can allow Spring to resolve collaborators (other beans)
automatically for your bean by inspecting the contents of the
ApplicationContext.
Which basically means you can leave it up to the Spring Framework to initialize your beans and ensure they're there when you need them. For example, lets say I have a number of Things in my app. Now, if I want to get all the Things and have them placed in a List<Thing>, in Spring (assuming I've configured it properly), I can simply use this code:
#Autowired
private List<Thing> things;
With no additional effort on my part I have the list I needed.
The question concerning Aspect Oriented Programming (AOP) as it relates to Spring would be quite a bit to put in a post. The simple explanation can be seen in the official Spring Framework Reference:
Aspect-Oriented Programming (AOP) complements Object-Oriented
Programming (OOP) by providing another way of thinking about program
structure. The key unit of modularity in OOP is the class, whereas in
AOP the unit of modularity is the aspect. Aspects enable the
modularization of concerns such as transaction management that cut
across multiple types and objects. (Such concerns are often termed
crosscutting concerns in AOP literature.)
One of the key components of Spring is the AOP framework. While the
Spring IoC container does not depend on AOP, meaning you do not need
to use AOP if you don’t want to, AOP complements Spring IoC to provide
a very capable middleware solution.
That section is very detailed and worth a read if you're interested in AOP.
Autowiring is a mechanism of resolving dependencies in Spring IoC container, as per Spring Reference. So instead of directly specifying the dependency (in XML or in Java configuration), you can depend on container itself to provide you with candidate(s). Spring itself should abort if you find more than one matching dependency (unless you are looking for a collection of beans).

How to integrate Spring AOP in CDI - any examples

I would implement a Spring aspect in a j2ee application, can someone post some tutorials?
I've just used Spring in web application but I've not idea how to invoke a cross cutting concern in CDI container.
Thanks in advance
Is there a special need for combining Spring and CDI? If not, I'd suggest to stick to standard Java EE as long as you can solve your problem with it and only if not, look for external components (and even then, you should first check the "sort-of-standard" CDI extensionf from the Apache Deltaspike project.
Cross-cutting concerns can be solved via pure CDI - check our for example the Java EE 7 tutorial about the topic.
Small note: Don't use J2EE anymore if not maintaining an ancient application.

CDI-like conversation scope in Spring

I've been developing Spring 3 + JSF 2.0 applications for some time. They run on simple Tomcat or Jetty container. Looking ad CDI I envy a conversation scope.
Are you aware of simple conversation scope implementation for Spring? I don't want to use full-blown SpringMVC + WebFlow + SpringFaces stack. I just need a web filter that enables conversation scope and lets me inject conversation object and start or stop it. Like in CDI.
Not exactly on your question, but since you're asking for a solution in order to avoid using Spring Webflow, Spring Faces, etc ... it looks like you're using Spring just for DI (i mean you're not so dependent on the Spring ecosystem). Well, maybe in this case it would worth the effort to think about migrating to a Java EE 6 stack. I don't think the migration would be that difficult, since you're using not that much from Spring, and in case you have beans in spring you still need, you could write some CDI producers for them. You can google for some migration paths from Spring to Java EE 6.
And if you're afraid of the "heavy Java EE containers", well jboss 7.1, Glassfish 3.1.2 and TomEE all start on my notebook in under 2 seconds, and I bet your tomcat + spring container don't start much faster.
Just my opinion.
you should give a look to Apache TomEE + CODI project (subject of Apache MyFaces) and/or Apache Deltaspike (not sure the doc is up to date because it is pretty young)
TomEE gives you the strength of CDI and CODI adds a small integration layer between cdi and jsf to make it very powerful (it will be integrated in deltaspike but that's still a bit in progress)
MyFaces Orchestra (also works with Mojarra) is currently the best conversation scope implementation for JSF 2.0 on top of Spring.
Use the JSF #ViewScoped annotation on your managed bean that gives you the same conversation-like semantics as the CDI conversation scope. One difference between CDI's conversation scope and JSF's view scope is that CDI gives you explicit and straightforward control over the conversation while the JSF viewscope is pretty much canned for use. But with JSF's ViewScope, you know exactly where the boundaries of a conversation are: A conversation starts when you load a page backed with a #ViewScoped bean and ends when the page is closed. This is convenient for many use cases
Cagatay of primefaces also has this hack to simulate the CDI scope in Spring.
This tutorial by Max Katz from the the Richfaces team also gives a good intro the JSF ViewScope

JSR330 DI vs. Spring DI

Why are people using Spring DI vs. JSR330 DI? I see many projects still going forward at a huge speed with spring DI oblivious to the JSR330 specification. Many don't even know it exists. Was it not marketed enough and spring was?
I do see posts of Guice vs. spring, but the real argument should be JSR330 vs. spring since spring does not implement the spec yet(and hopefully one day it will). Any ideas on why such a large portion of the community seems oblivious to JSR330 and not evolving to it?
NOTE: I should make a correction here. Spring 3.0 does implement JSR330 and even has a way to override the default bindings much like guice except you have to supply an xml file instead of a Module file written in java.
I would say it is because Spring is so much more than just a simple DI container. Many teams use Spring for these other various reasons:
Transaction Management
Security
MVC
Aspects
Data Access
Batch processes
Webflow
Web Services
Many others...
They have their hands in so many things that it just makes it easy mix and match Spring technologies to do general enterprise development.
Spring 3.X supports JSR-330 out fo the box - http://blog.credera.com/topic/technology-solutions/java/springone-2gx-2011-summary/
It means that you can use the spring annotations or the JSR-330 ones.

JAX-RS annotations

I want to learn Restful web service using Apache CXF. Could you please suggest any resource to refer to except for the official CXF site? Where can I learn all those annotations with example uses?
One more question is “Are those annotations going to be different depending on the implementations framework like apache CXF or Jersey or any other framework?”
Are those annotations going to be different depending on the implementations framework like apache cxf or jersey or any other framework?
No. The point of the JAX-RS standard (and the rest of Java EE) is exactly to ensure the annotations work the same across implementations. Of course, there will always be bugs and differences in interpretation that result in different behavior, but in general it tends to work pretty well.
However, there may be additional, implementation-specific annotations that provide desirable functionality that is not covered by the standard (there's quite a lot of these for EJB and JPA implementations, not sure about JAX-RS). Using those would make your app depend on that particular implementation.
Check out Jersey user guide - it has a section on JAX-RS, which explains these annotations and should work with any other JAX-RS implementation: http://jersey.java.net/nonav/documentation/latest/jax-rs.html
Apache CXF documentation is very useful and it provides lots of additional resources. Consider going through it and checking the examples.
Additionally it might be useful for you to go through some JAXB tutorials and resources.

Resources