jpa:repositories / #EnableJpaRepositories vs #Repository - spring - java

Does using the #EnableJpaRepositories or jpa:repositories (on the xml) gives the developer not to use #Repository tag of Spring? As I look on the example guidelines of Spring most of their examples is that they do not use the #Repository tag anymore on their interfaces that extends JpaRepository or CrudRepository interface of Spring Data.
I tried to use them together but Intellij warns me that it can not autowired my repository because there is more than one bean. I tried to remove the jpa:repositories on my xml file but the an error appeared that the application can not create a bean for my entity manager.

These are two different things. #Repository annotation is much older than Spring Data and is used to tell Spring to translate all exceptions thrown by #Repository annotated components to DataAccessException (more to read here: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/stereotype/Repository.html)
In SpringData you have #EnableJpaRepositories + entire underlying scanning and repository bean generation mechanism and there is no need to mark your repository interfaces (or custom classes) with #Repository.

The Spring Data Documentation is IMHO not entirely clear about this.
In chapter 3.1.1 it says:
Using the repositories element looks up Spring Data repositories as described in “Creating Repository Instances”. Beyond that, it activates persistence exception translation for all beans annotated with #Repository, to let exceptions being thrown by the JPA persistence providers be converted into Spring’s DataAccessException hierarchy.
To me this sounds like you would still need to add #Repository explicitly in order to activate persistance exception translation.

Related

Why EntityScan , EnableJpaRepositories annotations required if we are already using componentScan annotation?

I am already using componentScan annotation in main class of spring boot app , but If I use only this annotation it will gives issue while getting repositories reference. So to overcome this I am using EntityScan and EnableJpaRepositories annotations with componentScan.
#EntityScan(basePackages={"com.gonkar.fleetms.models"})
#EnableJpaRepositories(basePackages={"com.gonkar.fleetms.repositories"})
So my question is why its required to use other two annotations? if I am already using componentScan.
The #ComponentScan annotation is used to create beans for classes annotated with #Component, #Controller / #RestController, #Service, #Repository. It marks them for being added to the Spring container (making them eligible for dependency injection and allowing them to be #Autowired).
The #EntityScan annotation doesn't create any beans, it identifies which classes should be used by a JPA persistence context.
The #EnableJpaRepositories annotation is used to create repository classes from Spring Data interfaces.
All three annotation are often used together, but they are responsible for different things.

Spring boot - new project

I would like to create a java project (none web) to take advantage of the spring features like dependency injection, transactional data, Autowiring etc,
Some of the features I would like to use:
#Service
#Autowired
#Repository
#Transactional
JdbcTemplate
Stuff I don't need:
Hibernate
#Controller
#Path
I am struggling to create that initial Spring project with the initial configuration
I was wondering if I can create that initial Spring project using the new spring boot integration in eclipse?
if its possible what is the correct modules to select from this screen?
The annotations are included in the org.springframework (spring-context) so if you create the default project you will get a pom with the spring-boot-starter dependency which includes the org.springframework.
Regarding to the JDBC it's included in the spring-boot-starter-jdbc.
I think selecting Aspects from Core for all annotations except #Transactional. For #Transactional you should go within SQL and select dependency for any particular db that you are using like PostgreSQL,MySql,SQL Server etc.
Hope this helps !

Do I need to configure hibernate before configuring spring-data?

I am a little bit lost here, my main goal is to create a MVC pattern with Spring MVC and include Spring-security with user/passwords from the database.
So far, I have Spring security and MVC running well, but I dont know how to include the database (I must use spring data in some point).
I've read the tutorials and info of the site, and it says its a layer that works with other ORM (such as hibernate). So my question is, Should I configure hibernate before Spring data?
Is there any guide on how to do it (where they use annotations only?).
The answer to the question "do I need hibernate configured to use Spring Data JPA" is "yes" if you want Hibernate to be your JPA implentation.
So here's what the stack looks like for a typical Spring MVC + Spring Data JPA application:
#Controller class with #RequestMapping
calls
#Service class
calls
#Repository class (this annotation is optional, extend CrudRepository interface)
The repository you write uses an #Entity (JPA) class to access the database through Hibernate as described in this tutorial. Spring does a great job hiding most of the setup details of Hibernate from you (you don't need a persistence.xml if you do it right).

Autowired Dependency Injection with Spring Security

I have a spring webapp with annotation driven configuration.
All Controllers, Repositories are autowired.
When integrating Spring Security I defined a separate security-app.xml. I created a Service called LoginUserService which implements UserDetailsService. Now the method loadUserByUsername() method of this class gets invoked for authentication.
This class has an autowired dependency for UserRepository. Now this autowired dependency turns out to be null. To fix this I enable annotation driven configuration and add the package name for the repository class in component scan configuration.
This solution is also discussed here
spring security with custom user details
But now the problem is that the UserRepository has an EntityManager field with #PersistenceContext annotation. For the spring security configuration it is able to locate the UserRepository but not able to locate the entity manager. Should I create a new EntityManagerFactory here? I guess that will create two persistence units in my application?
How can I inject an autowired dependency to UserRepository created with the original servlet xml?
Update
This is briefly discussed here:
https://stackoverflow.com/a/7078395/161628
But I guess a canonical detailed answer will be more useful to me.
Update
How about using ApplicationContext to get the UserRepository at runtime?
if (userRepository == null) {
userRepository = ApplicationContextProvider.getApplicatonContext().getBean(UserRepository.class);
}
Why is Spring's ApplicationContext.getBean considered bad?
EDIT: Beans that you declare in your config for your DispatcherServlet are not going to be available to any beans you declare or component scan in your contextConfigLocation config files. So in this case, if you're setting up your JPA config in your config file that you load for your DispatcherServlet there is no way to wire that in to beans your declare in your security config. You need to move any "core" bean config like that (datasource config, db connection pool config, JPA/Hibernate config, repository/service component scanning, etc.) into a config file that you load via the contextConfigLocation. Then that stuff will be available both to your security beans and your MVC beans. I think generally the idea is to only load MVC specific beans in your DispatcherServlet config (e.g. Controllers, views, request handlers, request scoped beans, etc.). That way you ensure you have a clean separation between MVC code and non-MVC code, with only a one-way dependency from the MVC code to the "core" code, and no dependencies on MVC code in your "core" code. This helps make your code more modular, and makes it easier to reuse your "core" code in other ways, specifically in unit tests.
(Original comment text was asking about how the security config is loaded, if it's in the contextConfigLocation or somewhere else.)

What is #Service in Spring MVC

If I use #Service on a service class, do I need to make a service class bean in my servlet xml file or do I have to do both?
You don't have to declare a bean in your context file if you:
1) Annotate the class with:
#Component, #Service, #Controller or #Repository
2) Include the context:component-scan element in your context file like this:
<context:component-scan base-package="your.package" />
Hope that helps.
Last time I looked (Spring 2.5) #Service was a marker annotation subclassed from #Component, but with no additional behaviour. Which means that beans tagged with #Service become candidates for auto detection if you are using annotation-based configuration via classpath scanning.
As per the docs, the intention is that this annotation might include service layer specific functionality in future Spring releases. It can also act as an AOP point cut for all of your service layer components.

Categories

Resources