Spring #PostConstruct in #Repository dao and #Service that extends dao - java

My application is using Spring Boot (latest version) and has a dependency which uses Spring (latest version).
The dependency has a Dao class annotated with #Repository that extends a couple of unannotated abstract classes up to JdbcDaoSupport.
Moreover, this class has a #PostConstruct-annotated method, that sets the datasource.
A secondary Service class, annotated with #Service, extends from Dao.
I'm finding that an autowired <Service instance>.getJdbcTemplate() - which comes from the above-mentioned JdbcDaoSupport superclass - gives null.
If I omit the #Repository annotation on Dao, the jdbcTtemplate is correctly set.
The same happens if I annotate that Dao with #Service.
A part from the fact that I may have annotated them both for no specific purposes - the whole application code is experimental, though I can see a situation where the main service extends the repository (dao) while a more complex one can implement its logic delegating to several dao's - is this forbidden by Spring or am I missing something?
UPDATE
I added a #PostConstruct method to the service and it's not being called.
UPDATE / 2
I tried autowiring the dao inside the service, but I get the same result; it seems to me a matter of #Repository and PersistenceExceptionTranslationPostProcessor where the latter replaces my bean with a proxy-generated one (GitHub issue).

You should Autowire DAO into Service instead of extend.

Related

Can I use SQL queries within services

The recommended way of executing SQL queries is by creating a repository, using the #Repository annotation. I'm wondering if I can also execute SQL queries within a service, using the #Service annotation, or is this tied to a specific Spring stereotype?
For example: Is there any rule that guarantees that a #Service class must have business logic and #Repository must have query execution? If I execute a query in a #Service class will it throw any exception?
No it won't throw any exception.
But the idea of separating the DB Logic and Business Logic is to use #Service for service implementations (business logic) and #Repository for repositories i.e to handle DB operations (it can be CRUD, PagingAndSorting etc).
Thus, the code becomes modular and obeys the design patterns and coding standards. Service will make use of Repositories. And your handlers will use methods from your Service. That's how it works.
As per the Spring API specification.
A class annotated with #Repository is eligible for Spring
DataAccessException translation when used in conjunction with a
PersistenceExceptionTranslationPostProcessor. The annotated class is
also clarified as to its role in the overall application architecture
for the purpose of tooling, aspects, etc.
So DataAccessException aims user code find and handle the kind of error encountered without knowing the details of the particular data access API in use (e.g. JDBC).
#Service does not have any DataAccessException translation thus you can expect un-translated exception on classes which are annotated with #Service annotation. It indicate that a class is a Business Service Facade.
#Service ,#Repository,#Controller are specialization of #Component are all termed as Spring Beans
#Component generic stereotype for any Spring-managed component
#Repository stereotype for persistence layer
#Service stereotype for service layer
#Controller stereotype for presentation layer (spring-mvc)
It's all about distributing the concerns (Presentation,Business,Database),so it won't through any exception as asked by you.
You can refer here for more-Spring Docs
It will not through any exception there are some specification of each annotation.THIS answer will give you more clarity hope it will help you.
When we are going to develop any Project it should be lossy coupled and maintainable. To achieve this layer separation is important
#Service - Annotate all your service classes with #Service. This layer knows the unit of work. All your business logic will be in Service classes. Generally, methods of service layer are covered under a transaction. You can make multiple DAO calls from service method. if one transaction fails all transactions should rollback.
#Repository - Annotate all your DAO classes with #Repository. All your database access logic should be in DAO classes.
#Component - Annotate your other components (for example REST resource classes) with component stereotype.
Reasons to use them :
The main advantage of using #Repository or #Service over #Component
is that it's easy to write an AOP pointcut that targets, for
instance, all classes annotated with #Repository.
You don't have to write bean definitions in context xml file. Instead
annotate classes and use those by autowiring.
Specialized annotations help to clearly demarcate application layers
(in a standard 3 tiers application).
What is Stereotype Refer Here
#Component generic stereotype for any Spring-managed component
#Repository stereotype for persistence layer
#Service stereotype for service layer
#Controller stereotype for presentation layer (spring-mvc)
For More Details Click Here and Here

Cannot Autowire interface in spring boot app

I am developing a REST API using spring boot. Following is my package structure
Getting the following exception when I try to start my application
***************************
APPLICATION FAILED TO START
***************************
Description:
Field articleRepository in com.abc.service.ArticleService required a bean of type 'com.abc.dao.ArticleRepository' that could not be found.
Action:
Consider defining a bean of type 'com.abc.dao.ArticleRepository' in your configuration.
Following is my project structure-
com.abc
com.abc.bean
-Article.java
com.abc.controller
-ArticleController.java
com.abc.dao
-ArticleRepository.java (Interface)
com.abc.service
-ArticleService.java
com.abc.web
-AbcApplication.java (main Springboot class)
In AbcApplication.java as it is not in the root package, I have the below annotations
#SpringBootApplication
#ComponentScan(basePackages="com.abc.*")
I tried few ways -
I moved AbcApplication.java to root package com.abc but no success
Instead of interface(ArticleRepository.java) I made it a class, it is working
I keep it as interface but changed annotation from #Repository to #Service/Component still no success.
I am confused how it is working if I change it to class instead of interface.
#Repository
public interface ArticleRepository {
}
You don't have any bean for ArticleRepository. If you will use Spring Data Jpa you have to extend a type of Repository: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.
If you will use your own repository, you must implement it.
If you are using any RDBMS and if ArticleRepository repository is responsible for interacting with your database, then you need to either extends CrudRepository or JpaRepository in your ArticleRepository, then only Spring will be able to create the bean of ArticleRepository,and you will be able to autowire your repository.
If you are not extending any of CrudRepository or JpaRepository,then at the time of bean creation,ArticleRepository is only plain java interface and a plain interface can not be instantiated.
And as for your question:
Instead of interface(ArticleRepository.java) I made it a class, it is working
Its because when you declare it as a class, then Spring does instantiate a concrete class, so actual object will be created at the bean creation time and everything will be working as it should be.
Ideally you should have one class that implements interface ArticleRepository . Annotate that class with #Repository, spring will take care of wiring.
I was also encountered with the same issue. What I missed was the spring-boot-starter-jpa dependency. It worked when I added this dependency in the pom.xml file.
MyBatis gives you two samples to access your databases with spring boot.# Component or #Mapper.
Sample link:
mybatis-spring-boot-sample-xml
Suggestion:
Show your ArticleRepository code fully.
interface does not require any annotation (neither #repository not #Service).
If its a DAO file add #Repository annotation to the class implementing the corresponding interface.
And, if its a service then add #Service annotation to the class implementing the corresponding interface.

Generic Autowiring Not Working with #Transactional

Spring appears fully capable of autowiring the correct type based on generic parameters without the need for #Qualifiers. However, as soon as I tack on a #Transactional annotation, it can no longer autowire based on generic parameters. Consider this example, invented only for purposes of illustrating the issue:
interface Product {}
interface Book extends Product {}
interface Toy extends Product {}
interface Store<P extends Product> {}
#Component
class BookStore implements Store<Book> {}
#Component
class ToyStore implements Store<Toy> {}
#Component
class BookDealer {
#Autowired
BookDealer(Store<Book> store) {
...
}
void inventoryBooks() {
... doesn't really matter what this does ...
}
}
Note that the above code wires up fine. The BookStore class is autowired into the BookDealer constructor without any issue. I can call inventoryBooks() and it works fine.
However, if I add a #Transactional annotation to a method upstream from the call to inventoryBooks(), e.g. on the client method that calls it, the BookDealer will no longer autowire, and I must resort to either injecting concrete types, or using a #Qualifier. The error is that there are two matching beans for the constructor argument of BookDealer, meaning both the BookStore and the ToyStore and Spring can't decide which one is needed. That tells me that Spring can no longer detect the generic types now that some upstream method has been proxied for the #Transactional. Something like that anyway...
I would like to stick to interfaces and not use #Qualifiers. Is there a way to do this with #Transactional, or is it a known limitation of generics and autowiring and things like #Transactional?
The Spring uses the JDK Proxy to generate the proxy used in AOP by default. The JDK proxy is based on the interface. So it maybe the problem, when you need to autowire the concrete class. So use the cglib instead(add the denpendency) and set the "" in the spring config file to "" and have a try. Hope it helps.
Using #Transaction annotation with #Autowired - Spring

What will happen if we interchange #service and #repository annotation in the spring MVC

Why we needs to use #service inside the service Implementation and #repository in the DAO Implementation. There are no problem occur when I interchange the #service and #repository annotation in the spring MVC.
According to documentaion #Repository,#Service,#Controller are all synonyms. They all are just specializations of #Component annotation. So, generally, they can be used one instead of other. But ... you should not do this.
First reason: any of these annotations make clear the role of your component in the application. Shows - is this component belongs to the controller, service, or data layer.
Second reason: some of these annotations processed differently by different Spring modules. For example, Spring Data JPA will process #Repository and will try to replace with implementation any interface marked by this annotation. Spring also will apply automatic exception translation to such classes. Another example: Spring Web MVC processes #Controller, and uses classes marked with it in URL mappings.
Actually, in future versions, some modules of Spring could process #Service in a particular way. Not as simple #Component. That's why documentation advises:
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.
It depends on what you use for the remainder of the framework. In theory nothing changes as the #Service and #Repository annotations are basically #Component annotations. The same could be said for #Controller or #Endpoint (for Spring Ws and there are more).
However they express an intent of what a class is (a service, a repository) and makes it clear to the user to what layer that class belongs.
However if you also use Spring for transaction managemnt then #Repository is also a trigger for adding exception translation to that class (also see the reference guide).
Although nothing has to break it probably will at some point.

what are #Repository and #Autowired used for. (Spring)

I am learning java for 3 months and sometimes
i can not understand the usage purpose of something.
one topic was dependency injection and spring beans i figured out the finally =)
now i confused with the two annotations #Autowired and #Repository.
First What does Autowiring mean? then
Why should i use them and what is the difference between using them and not using?
Also today i tried to use hibernate in a spring mvc project and i had to search for about 15(cause of class not found errors) jar files beacuse of the dependencies of other jar files used in the project.
is this had to be this way? this makes learning java very hard for the beginners
thanks...
#Repository is an annotation that marks the specific class as a Data Access Object, thus clarifying it's role. Other markers of the same category are #Service and #Controller
#Autowired is an annotation with a completely different meaning: it basically tells the DI container to inject a dependency. More info at http://apollo89.com/java/spring-framework-2.5.3/api/org/springframework/beans/factory/annotation/Autowired.html
Edit
More info at tutorialpoint
or docs.spring.io
Both the annotations have different purposes to be used.
#Autowired: This is same as <bean="xyz" autowire="byType"> you define in the configuration file. The reference variable (dependency) that is annotated with #Autowired, will be injected by Spring container as any matching #Bean found in #Configuration class.
Plus the classes annotated with #Component, #Service, #Repository are too considered as beans so their objects are injected into the matching dependencies.
Spring container scans the beans in the classes you mentioned for "component-scan" or #ComponentScan("xyz").
#Repository: This is also a spring-framework's annotation. When you annotate a class #Repository, spring container understands it's a DAO class and translates all unchecked exceptions (thrown from DAO methods) into Spring DataAccessException.
DAO class is the class where you write methods to perform operations over db.
#Autowired and #Repository are very 2 different concepts.
1.# Repository: This define a class to be a repository, In general term you can use simply #Component but to define specifically, there are 3 more annotations like Controller,service and repository.Mainly 2 advantages:
1.If you have defined(context:component-scan)in servlet.xml to scan the defined package and find its own by spring.
2. More advantages you get from spring like database access error translation, so it is mainly defined to use with class in which you are connecting with database either with hibernate or jdbc.
#Autowired: to inject dependency at run-time by spring, means in a class, autowire a object ,and use it ,so this bean will automatically be made without defining in xml file

Categories

Resources