What is the difference between #EJB of JEE and #Autowired of Spring? - spring

What is the difference between #EJB of JEE and #Autowired of Spring Framework?
Are those the same thing?
And if not, which are the differencies?
I have seen the following as #EJB's definition which really looks like a #Autowired's one:
An enterprise bean (EJB) is a server-side component that encapsulates
the business logic of an application.
The business logic is the code that fulfills the purpose of the application. It does not perform
display of business data or perform operations directly on the database.

The #EJB is used to inject an EJB into another EJB, in the JEE world.
See: Should I use #EJB or #Inject
In Spring the equivalent injection is done by the use of #Autowired.
Example:
#Service
public class MyServiceImpl implements MyService {
}
#Controller
public class MyController{
#Autowired MyService myService;
}
#Service is a Spring annotation for annotating a class at a service layer.
Other annotations: #Component, #Repository, #Controller, #Service.
See: What's the difference between #Component, #Repository & #Service annotations in Spring?
I would say:
#Component, #Repository, #Controller & #Service are closer to #Stateless or #Statefull, and
#EJB is similar to #Autowired
UPDATE
#Autowired tells Spring framework to find dependecies for you. 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.
See: https://javarevisited.blogspot.com/2017/04/difference-between-autowired-and-inject-annotation-in-spring-framework.html

Related

How to Fix Could not autowire. No beans of error in Spring Boot

How can I solve this error. I'm New to Spring-boot
As I can see the spring unable to find the bean UserDetailsServiceImpl, there might be couple of reason for it.
First, you might forgot to put #Service annotation on top of the class UserDetailsServiceImpl. Also, as the context is about Spring security so make sure that this class UserDetailsServiceImpl must implement the interface UserDetailsService
#Service
public class UserDetailsServiceImpl implements UserDetailsService {
}
Second, spring might be unable to scan this folder. So make sure spring IOC must scan this package while intialization and configure the bean.
In order to #Autowired a bean instance, a class should be decorated with Spring stereotype annotation like #Component, #Service, #Repository, #Controller or #Indexed. Just by decorating the class with one of these role annotations, you can use #Autowired to bind with the instance.
Otherwise, if none of these annotations are used, your class instances, you have to manually registered to the BeanFactory like this;
#Configuration
public class SomeClass {
...
#Bean
public UserDetailsServiceImpl userDetailsService() {
return new UserDetailsServiceImpl()
}
}
This answer just talk about your specific question, but you get to find out why #Configuration is used in preceeding example. Define scopes for bindings, singleton (one instance for the application) is the default scope in Spring, you should define scopes for beans if they should be in different scope on your requirements.

Prevent autowiring by type for certain beans in Spring

In my spring context I am creating a service bean and a proxy for this service bean (explicitly). Both implement the same interface.
Can I ensure that autowiring cannot inject the target bean?
I would like to be able to use the target service with the #Resource or #Qualifier annotations, but when autowiring it should always be the proxy.
Any ideas?
Use the Primary annotation. It will indicate which bean should be use preferably when autowiring.
Hope this helps :)
You can put #Primary annotation in your proxy service like bellow:
#Primary
#Repository
public class ProxyOfSomeService implements SomeService
And after that when you use, #Autowired annotation on SomeService field, the ProxyOfSomeService will be injected by deafault.
But when you need the real service you can have it like bellow:
#Autowired
#Resource(name="someRealService")
private SomeService someService;
I think this serves your need, thanks!

Spring Bean implements interface

public interface Service {
public void doSomething();
}
#Service
public class MyService implements service{
#Transactional
public void doSomething(){
}
}
#Controller
public class MyController {
#Autowired
private MyService service;
}
In above scenario autowiring fails with exception "illegalArgumentException : argument type mismatch". When I remove implements service from MyService everything works fine.
I have searched and found that place <aop:aspectj-autoproxy proxy-target-class="true"/> in applicationContext.xml for successful autowiring and it worked.
I have also found that spring uses JDK proxy when #Transactional is used.
I have some confusions,
How #Transactional relates to Proxying
Why spring uses JDK Proxy for the beans which implements interfaces.
Why I need to place <aop:aspectj-autoproxy proxy-target-class="true"/> in applicationContext.xml
Can anyone please explain ? or refer me any article or blog
Proxying is how Spring implements declarative transaction management. Spring reference is the best place for all your questions on this.
The most important concepts to grasp with regard to the Spring Framework's declarative transaction support are that this support is enabled via AOP proxies, and that the transactional advice is driven by metadata (currently XML- or annotation-based). The combination of AOP with transactional metadata yields an AOP proxy that uses a TransactionInterceptor in conjunction with an appropriate PlatformTransactionManager implementation to drive transactions around method invocations.
and on <aop:aspectj-autoproxy proxy-target-class="true"/>
The proxy-target-class attribute on the
element controls what type of transactional proxies are created for
classes annotated with the #Transactional annotation. If
proxy-target-class attribute is set to true, class-based proxies are
created. If proxy-target-class is false or if the attribute is
omitted, standard JDK interface-based proxies are created. (See
Section 8.6, “Proxying mechanisms” for a discussion of the different
proxy types.)

For the spring autodetection, what's the difference between component and service?

I think both #Component and #Service can be used to detect bean automatically, anyone can show me the difference between those two annotations?
The basic difference between both annotations is that #Service is a specialization of #Component.
See also spring documentation for #Service:
Indicates that an annotated class is a "Service" (e.g. a business
service facade).
This annotation serves as a specialization of #Component, allowing for
implementation classes to be autodetected through classpath scanning.
A specialization of component is also a #Repository and a #Controller
Further information can be found e.g. here.
As of and up to Spring 3.1, there is no difference in the way that Spring handles them. The docs say this, but in a rather obscure way:
Spring 2.5 introduces further stereotype annotations: #Component, #Service, and #Controller. #Component is a generic stereotype for any Spring-managed component. #Repository, #Service, and #Controller are specializations of #Component for more specific use cases, for example, in the persistence, service, and presentation layers, respectively. Therefore, you can annotate your component classes with #Component, but by annotating them with #Repository, #Service, or #Controller instead, your classes are more properly suited for processing by tools or associating with aspects. For example, these stereotype annotations make ideal targets for pointcuts. It is also possible that #Repository, #Service, and #Controller may carry additional semantics in future releases of the Spring Framework. Thus, if you are choosing between using #Component or #Service for your service layer, #Service is clearly the better choice. Similarly, as stated above, #Repository is already supported as a marker for automatic exception translation in your persistence layer.
So for now, #Service will be treated by Spring exactly the same as #Component, but #Service can be considered a form of documentation.
I'm not really sure why #Service was included in Spring 2.5 at all, since it doesn't seem to have any really purpose.
check the source code
#Target({ElementType.TYPE})
#Retention(RetentionPolicy.RUNTIME)
#Documented
#Component
public #interface Service {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* #return the suggested component name, if any
*/
String value() default "";
}
Service annotation is in turn annotated with #Component . There's nothing much in difference .
here is the explanation to why we need such specialisation...
In Spring 2.0 and later, the #Repository annotation is a marker for any class that
fulfills the role or stereotype (also known as Data Access Object or DAO) of a repository. Among the uses of this marker is the automatic translation of exceptions.
Spring 2.5 introduces further stereotype annotations: #Component, #Service, and #Controller. #Component is a generic stereotype for any Spring-managed component. #Repository, #Service, and #Controller are specializations of #Component for more specific use cases, for example, in the persistence, service, and presentation layers, respectively.
Therefore, you can annotate your component classes with #Component, but by annotating them with #Repository, #Service, or #Controller instead, your classes are more properly suited for processing by tools or associating with aspects. For example, these stereotype annotations make ideal targets for pointcuts.
Thus, if you are choosing between using #Component or #Service for your service layer, #Service is clearly the better choice. Similarly, as stated above, #Repository is already supported as a marker for automatic exception translation in your persistence layer.

What is the most appropriate way of injecting daos in services, services in controllers in Spring?

There are many annotations in the Spring framework like #Component, #Service, #Repository, #Service #Resource and #Autowired etc.
What is the most appropriate way of injecting my daos in services, and my service class in the Spring Controller.
With so many annotations it is getting confusing especially with #Autowired working for all situations.
#Service and #Repository are just "sub-annotations" for #Component to specify the bean a bit more (to separete Services from Repositories for more sophisticated stuff). From the point of injection this three are equal.
For injection, there are 3:
#Resource
#Inject
#Autowired
#Autowired is the most powerful annotation, but #Resource (JSR-250) and #Inject (JSR-330) are standardized. — Anyway if you not plan to reuse your application in a non-Spring environment, then I would not pay to many attention to this concern.
See Annotation based configuration in Spring, best Spring Annotation tutorial for me.
I prefer to avoid annotations, especially if they start getting confusing. Nothing wrong with good old getter and setters in this case. Just gotta wire the bean on your own, which isn't so difficult that annotations are necessary.

Resources