Which Spring annotation to use for Hibernate DAO class? - spring

What Spring annotation should I use for Hibernate DAO classes so they could be found in scanning process? #Repository, #Service or #Component? I couldn't figure out the difference. I'm on Spring 2.5.6 now.
P.S. Can someone guide me quickly through the layer idea? I only have heard a thing like presentation layer, but don't have exact understanding what should I call so and what is the business layer? Are there other?

#Repository would be my recommendation.
Presentation tier means web UI, so those should use the #Controller annotation.
Services implement use cases using POJO interfaces; mark this as #Service. Controllers will use services to fulfill use cases.

It doesn't matter much, but #Repository is a good bet. The Spring manual has this to say:
Beginning with Spring 2.0, the
#Repository annotation was introduced
as a marker for any class that
fulfills the role or stereotype of a
repository (a.k.a. Data Access Object
or DAO)

In core Spring, I don't believe there is any difference. Generally, these stereotype annotations are used for auto-detection when using annotation-based configuration and classpath scanning (from Spring docs). It is possible to have some software to make use of them but in absence of such software I choose stereotype that makes most sense to me. In case of DAO I usually choose #Component, although #Repository is a good option as well.

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

Why to use AOP for transaction management in Spring?

I am learning AOP and came to know that is useful for separation of concern, for example, logging, transaction management, security etc.
So far good to know AOP.
Now, I read about Spring transaction management in Spring framework we can have annotation #Transactional.
It is this point which is make me confused why should we use AOP in which we have to create Aspects rather than using the annotation which Spring provides.
For example:
#Transactional
public void dataAccessTrans() {
}
When Spring is already having transaction related functionality, then why should we use AOP to do transaction management?
If we use AOP, then don't we have to create Aspect and create advice which would act on the method; does't this make us to do manual work, rather than handling it by spring framework itself by its own annotations.
Can anyone help me understand this which I am not able to understand clearly.
Spring relies on AOP to implement declarative transactions.
The Spring Framework’s declarative transaction management is made
possible with Spring aspect-oriented programming (AOP), although, as
the transactional aspects code comes with the Spring Framework
distribution and may be used in a boilerplate fashion, AOP concepts do
not generally have to be understood to make effective use of this
code.
So as you use the #Transactional annotation :
#Transactional
public void dataAccessTrans() {
...
}
you use indirectly AOP.
So in the very most of cases, you never need to declare any custom aspects to handle the transaction management.

Spring boot with spring #Transactional works without enabling transactional management

In my spring boot application spring #Transactional annotation works without specifying #EnableTransactionManagement explicitly.
Is there any official documentation saying that it is enabled automatically?
Or there is something else happening .... ?
btw: I'm using Spring Data JPA
Yes, this is enabled as long as you have spring-tx and some transactional resource in your application. Effectively if you are using spring-boot-starter-jdbc or spring-boot-starter-data-jpa, Spring Boot will configure a DataSource for you, start Hibernate (in the latter case) and configure transaction management.
Not all "Enable" annotations require to be explicitly set. When there is a reasonable amount of things that we can check to validate it makes sense to configure that for you, we'll do it. In this case, if you have a DataSource you must probably want to have transactions. If you have JPA (and no JTA infrastructure), you probably want a JpaTransactionManager). If we auto-configure that, the easiest way to use it is via #Transactional so we'll enable that in that case as well.
I guess you kept asking to get some sort of "official" answer, so here's one.
#SpringBootApplication adds #EnableAutoConfiguration it detects Spring Data JPA on your classpath. According to it Spring registers PlatformTransactionManager - JpaTransactionManager, datasource, entitymanager, repositories.
Not sure there are precise articles, but there are proper answer on stack. An official spring sample article
#Transactional annotation can work fine if the "< tx:annotation-driven/ >" tag is in your Spring XML configuration. Look at this reference : http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/EnableTransactionManagement.html

Spring boot JPA without Spring data

I'm gradually introducing Spring Boot to a Spring JPA project. My intent was to first introduce Spring Boot, than at some later stage Spring Data, but I was not able to find any examples (nor a suitable starter) that uses Spring Boot + JPA without Spring Data.
How come? Is there any benefit of introducing Spring Boot to Spring JPA project, without Spring Data, or does it make sense only with Spring Data in place.
Any article link or example code would be helpfull and appreciated, thanks
More context
I'm working with a live project so every change introduces risk. We're discussing of moving from XML to JAVA based configuration, and I'm advocating adopting Spring Boot at a same time, but I lack persuasive selling points.
Personally, I want to include Spring Boot on all layers to boost future productivity, but I need to argue better the direct immediate benefits of using it in our Service/DAO module which is at the moment based on Spring/JPA/Hibernate with the good old manual CRUD implementations.
So I need selling points for using Spring Boot on a persistence layer, but ones that span beyond Spring Data (e.g. configuration gains, maintenance, testing...anything)
As folks have said above, there is no Spring Boot JPA. It's either Spring Boot Data JPA, or JPA on its own.
The immediate benefits that I could think of:
With Spring Data JPA you don't write the Dao layer. For all CRUD operations, the CrudRepository interface gives you all you need. When that is not enough, all you have to use is the #Query annotation to fine-tune your SQLs
Configuration by convention. For example, with Spring Boot, just having the H2 dependency in the classpath gets Spring to use the H2 in-memory database, gives you Datasource configuration and transaction management (only at the JPA repository level) by default
Ability to create micro-services. With Spring Boot, you can create micro services that can be deployed and run on a number of boxes with java -jar ...
You can enable annotation-based transaction with one simple annotation: #EnableTransactionManagement
Java configuration over XML. This advantage is not to be underestimated
A lot less code (the DAO layer) means also a lot less maintenance
The native ability to provide a RESTful API around data: https://spring.io/guides/gs/accessing-data-rest/
It all depends where your company is heading for. If they want to deliver business value faster and move towards more a DevOps operating model, then the above advantages should be enough selling points for any organisation
Spring wiht JPA (for example Hibernate) but without Spring-Data-Jpa means that you direct interact with the JPA Entity manager and. Typical you use it to implement your own DAO from it and use the #Respository annotation.
#Respository
public class UserDao {
#PersistenceContext EntityManager em;
public User findUserByLogin(Sting login) {
....
}
}
Even if there is no starter project, you could use a Spring-Data-JPA project, and implement the Repository in this old fashion style. (And then you could show how simple it become when you just write Spring-Data-JPA interfaces)
As far as I known, spring-boot means more convenient not any independent business feature.
In other words, spring-boot helps you to start, configure your application in some automatically way. But you can do that without spring-boot with your own specific configuration.
So, you are going to use spring-boot in your application means you are going to use spring-boot's auto configuration feature with your original application.
Actually, Spring JPA implemented in spring-data-jpa is what you are looking for not spring-boot. Of course, spring-boot can simplify your work dramatically.

Resources