Why and When use #Inject instead of #Autowired in Spring? [duplicate] - spring

This question already has answers here:
What is the difference between #Inject and #Autowired in Spring Framework? Which one to use under what condition?
(11 answers)
Closed 3 years ago.
Since Spring 3.0, Spring supports the standard JSR 330: Dependency Injection for Java. In a Spring 3 application, you can use
#Inject instead of Spring’s #Autowired to inject a bean.
#Named instead of Spring’s #Component to declare a bean.
When should I use what?

The #Inject annotation also serves the same purpose, but the main difference between them is that #Inject is a standard annotation for dependency injection and #Autowired is spring specific.
Read more: https://javarevisited.blogspot.com/2017/04/difference-between-autowired-and-inject-annotation-in-spring-framework.html#ixzz6Bwt3RUg9

Long story short these annotations are spring guys efforts to support some parts of Java EE (now Jakarta EE) specification, these are injection official annotations there. Spring guys add them in order to facilitate Spring adoption and system migrations from Java EE.
So when to use this annotation?, use them if you are planning to migrate to/from Jakarta EE it makes sense to used them to facilitate the process or keep your system consistent. Otherwise, go for Spring "official" ones.

If you only use spring, and do not special behaviour, you can use any annotations: they will be handled the same.
If you use (or plan to use) other dependency managers (JavaEE, Guice, XWiki internal container, ...) #Inject and #Named are defined by the JSR-330 and are now supported by all of those containers.
On the other side, if you plan to use all spring specific goodies, spring specific annotations may allow finer configuration.

Related

Using #Named with Quarkus

I used to use the standard annotations #Named and #Inject for dependency injection in my code, and the Spring annotation #Primary to force the usage of a specific implementation in case I have multi implementations.
Unfortunately, this is not working with Quarkus anymore. Is there any specific reason ?
Is there any solution that can work for both Quarkus and a Web Application (The library that I am implementing can be used in both type of applications)?
Thanks

What Spring annotations can we use in Hybris commerce project?

I was reading about Spring core module and came across Spring annotations that I did not see till now in the Hybris project:
#Component,#Qualifier
Are these used in Hybris projects?
Hybris uses both. Annotation Injection and XML Injection. You can also use both. I recommend you, to define a clear strategy when you use which one.
For example:
Controller - Annotation Injection
Facade - XML Injection
Service - XML Injection
To your point, which kind of Annotation you should use, have a look here:
What's the difference between #Component, #Repository & #Service annotations in Spring?
In common said, there is not really a different. It's just nice to use the correct Annotation for the correct class.
Hybris 6.6 uses Spring 4.3. The usual annotations like #Autowired, #Required, #Controller, and many others should work.
If you have access to Hybris Help, have a look at "Spring Framework in SAP Commerce": https://help.hybris.com/6.6.0/hcd/8c63621986691014a7e0a18695d7d410.html
There is:
Dependency Injection
Interface-Driven Design
Beans (and aliasing)
Spring Profiles
Spring MVC
Spring Integration
etc

Spring 4 using Groovy setup

Spring 4.0 has improved support for Groovy e.g. using the GroovyBeanDefinitionReader.
What would be setup to to have a full Spring MVC application using Groovy?
E.g. using GroovyBeanDefinitionReader and AnnotationConfigWebApplicationContext together.
Anyone knows if there is a sample available or some pointers on a blog site?
You might want to check out spring boot, still in milestone release behind Spring 4 but they were really pushing its groovy support at spring eXchange.
Check out the bottom of this spring-boot guide
It's not quite the use of GroovyBeanDefinitionReader and AnnotationConfigWebApplicationContext you asked for, but I can't see why you couldn't do what you are after with the opinionated approach used by spring boot and the standard configuration annotations on groovy classes.
The git hub repository shows a number of annotated groovy examples
with ui.groovy for example, showing a configuration class for the WebMvcConfigurerAdapter defining a bean.
In your main method, do SpringApplication.run(new Object[]{JavaConfig.class, "beans.groovy"}, args), where JavaConfig contains your configurations in java (like #Configuration, #ComponentScan and etc., I generally find these things are easier using annotations) and beans.groovy just contain your spring beans DSL.
Assuming beans.groovy is on classpth (i.e. under src/main/resources)

Spring can be used in Seam?

I understand that Spring has really nice features, such as dependency injection. I am new to Spring. I have understood that I can use Spring alongside with struts and other frameworks too, in order to use its capabilities.
In my project I am going to use Seam 2.0, I am using JNDI to lookup for the EJBs. I am wondering if I can integrate Spring with Seam and use its ApplicationContext in order to get beans from that directly and not use JNDI lookup anymore?
There is a whole chapter in the Seam reference dedicated to this:
27. Spring Framework integration

JBoss 5.1: Spring #Resource annotation not working

I am working on an application using Spring 3 and Hibernate 3.5 with Java 1.6.
So far I've been using JBoss 4.2.1 and everything was fine.
Now while migrating to JBoss 5.1, I encountered lot of issues. One of them is that JBoss is ignoring the Spring #Resource annotation. I get the following exception:
java.lang.RuntimeException: mapped-name is required for serviceManager of deployment pol-1.0.war
at org.jboss.web.tomcat.service.injection.WebResourceHandler.loadXmlResourceEnvRefs(WebResourceHandler.java:287)
at org.jboss.web.tomcat.service.injection.WebResourceHandler.loadXml(WebResourceHandler.java:325)
at org.jboss.web.tomcat.service.TomcatInjectionContainer.processMetadata(TomcatInjectionContainer.java:550)
at org.jboss.web.tomcat.service.WebCtxLoader.start(WebCtxLoader.java:158)
It expects mapped-name for each #Resource like some ejb.
I've seen similar questions but they are without any answer e.g.:
#Resource annotation not working properly with JBoss5.0.1
Please advise.
Adi
Actually your problem is that JBoss doesn't ignore #Resource annotations - it tries to handle them according to EJB rules instead of leaving them to Spring.
Perhaps this feature can be disabled somewhere in JBoss configuration, but the simpliest solution would be to replace #Resource with #Autowired or #Inject.
Sounds like java annotations need namespace support.
Then it would be #Spring:Resource or #EJB:Resource.
Oracle, are you listening?
Short of namespace for Annotations, you could possibly try changing the order of the libraries in your classpath so java would see the Spring annotations first (or last), whichever ends up providing the desired outcome.

Resources