Does Spring Data JPA require Spring Boot? - java

I'd like to write a small example program with Spring Data JPA.
Is Spring Boot a requirement to use this spring project?

No, you are perfectly fine to use Spring Data JPA by itself.
Do note that Spring Boot makes it easier to set up a project, all Spring Data JPA examples use:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

After trying, I found it is possible to use Spring Data JPA with plain Spring, without spring boot.
When not using boot, the following are needed:
1) spring-core maven dependency
2) spring-context dependency
3) spring-data-jpa dependency
4) hibernate-entitymanager or some other JPA provider
5) mysql-connector-java or some other DB connector
6) javax.persistence-api dependency
7) AnnotationConfigApplicationContext instead of SpringBootApplication
8) #EnableJpaRepositories("mypackage")
9) #ComponentScan("mypackage")
10) #Bean for LocalContainerEntityManagerFactoryBean and dataSource
11) Setting Hibernate properties to EntityManager
12) #Bean for PlatformTransactionManager

You can use Spring without Boot module but, in your case, you'll have to consider building an project containing Spring Core + Data + JPA and run it inside an Application Server.
If you just do some small example, like you said, Spring Boot + Data + JPA might be faster and easier to setup and run.

Related

Springboot annotations in plain/vanilla java

Can I use springboot annotations like #Bean, #Component, #Autowired etc in plain java project with just using gradle dependencies?
To use spring frameworks annotations the project should be either spring boot or a project with all the required spring dependencies to support the above mentioned annotations.
No. Spring uses those annotations for the inversion of control. Without this framework they are useless in plain java.

SpringBoot configuration with pure Hibernate ORM without JPA

I'm new to SpringBoot development and I want to add Hibernate ORM framework to my SpringBoot project. I know
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
this dependency gives that capability but it's not supporting for every Hibernate features in pure Hibernate framework.
Example:
session.createNativeQuery(query).addEntity(LectureSchedule.class).list();
is there any way to configure Hibernate framework for SpringBoot applications not via JPA dependency and autowire SessionFactory so I can perform basic hibernate coding after that without using JPA methods?
If you don't want to use the spring starter pom file, you can add the hibernate dependencies one by one - depending on your needs.
The minimums are the hibernate-core package and spring-orm.
This tutorial describes how to do it step by step.

Can I use Spring integration modules in my Spring boot application?

I am trying to make a filedownload endpoint for my ftp server. I see that there is a Spring boot integration starter module for Spring boot but this module doesen't contain classes like DefaultFtpSessionFactory . I've read on the web that there are other modules available like Spring integration http and Spring integration ftp. However, these are not spring boot modules. Is it save to include those modules in my POM anyway? Or shoulden't I use Spring boot starter integration in the first place?
I see in this example: https://blog.pavelsklenar.com/spring-integration-sftp-upload-example/ That the author is using spring boot next to Spring Integration 4.3.8 regular Spring. He does say those are managed by Spring boot but i'm not exactly sure what he means by that.
Can anyone tell me what modules I should include if I want to make the download function? Thanks
Since it's unlikely that an application would require all Spring Integration modules (ftp, sftp, http, mqtt, ... etc), the starter only includes the core and java dsl jars on the classpath (in Spring Integration 5.0, the DSL is built in so boot 2.0 only includes the core jar).
Otherwise, you'd end up with many jars on the classpath that you don't need.
So, yes, you have to manually add the dependencies you need...
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-ftp</artifactId>
</dependency>
Note that you don't need a <version/> - boot will manage that for you and bring in the right version corresponding to the core.
The modules themselves will bring in any additional transitive dependencies they need. So you just need to add the top level module(s) to your pom.

Understanding Spring Boot

I am trying to understand the difference between spring boot and spring boot web. From this boot tutorial the pom contains spring boot as the parent and spring boot web as a dependency like so:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
What are the uses for the 2 different versions? Do you always use them together? This spring boot documentation tells me if the program is production ready to use:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
But if that's the case then why isn't there one for web like so:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-actuator</artifactId>
</dependency>
</dependencies>
There are lots of these different 'starter' poms for spring boot. Each one of them tells maven to bring in the dependencies needed for that particular functionality. So spring-boot-starter-web brings in all the stuff needed for MVC and autoconfigures it with sensible defaults. The real trick to spring boot is it when it autoconfigures things it uses a whole of #ConditionalOnClass or other such annotations that look at what dependencies are on the classpath and provides configuration for those dependencies. What this means is when you have that spring boot actuator starter, when it's doing its autoconfiguration it will look at what other spring boot start poms you have in your pom.xml and it will configure different endpoints for the actuator so you can see the various metrics the actuator provides for that particular module.
Spring Boot is a framework, spring-boot-starter-web is one of the packages that comes with it, a jar file.
Just like JDK is a library, and util is one of the packages included in the JDK.
From: https://docs.spring.io
Spring Boot provides a number of “Starters” that auto-configures your application adds jars to your classpath. The spring-boot-starter-parent is a core starter that provides useful maven defaults. It also provides a dependency-management section so that if you import additional starters then you can omit version tags for “blessed” dependencies. Therefore you should only need to specify the Spring Boot version number on this dependency
Starters are a set of convenient dependency descriptors that you can include in your application considering each starter covers a specific area.The starters contain a lot of the dependencies that you need to get a project up and running quickly and with a consistent, supported set of managed transitive dependencies.For example, if you want to get started using Spring and JPA for database access, just include the "spring-boot-starter-data-jpa" dependency in your project, and you are good to go.
Spring Boot
spring-boot-starter-web is starter for building web, including RESTful, applications using Spring MVC. It uses Tomcat as the default embedded container
Spring Boot has different groups of starters like
1-Spring Boot application starters: spring-boot-starter-web , spring-boot-starter-jdbc , spring-boot-starter-jpa etc
2-Spring Boot production starters : spring-boot-starter-actuator which provides production ready features to help you monitor and manage your application
3-Spring Boot technical starters: spring-boot-starter-jetty , spring-boot-starter-tomcat these starters can be used to exclude or swap specific technical facets
1.spring-boot-starter-parent deals with the auto start of main method and run methods so on..
2.and spring-boot-starter-web deals with the spring MVC things like controller, autowired so on.. Hope this helps..
Spring boot is a very cool tool of Spring Source. In many conference the team talck about of Spring Boot as one of the three DSR(Domain Specific Run-Time) of Pivotal.... Spring Boot, Spring XD and Grails(now Pivotal didn't support Groovy & Grails).
That said compare Spring boot stand alone and Spring boot web enviroment may be a cool conversation. First of all Spring boot give you many production ready istruments such as actuator(avaiable whit sprin boot in web enviroment), spring remote shall and so on. The main difference is the same of had a spring stand-alone context or a Spring web context. Of course some of the potentiality that yon could have are avaiable in a web context, Actuator is an example. but the main difference is in what kind of application you need, web or stand-alone.

Properly (and easily ?) initiate a Spring MVC Project

What's the best method to initiate a new Spring Project ?
Initiating Spring project is a lot of pain with various xml and db configuration.
Is there an "official" repository with complete sample project, as MVC with db access and so ?
Spring Boot may also be the solution, but some point still not clear to me :
How to add external components (such as Quartz) without creating some xml config ? (No such xml in Boot apparently)
Is a Spring Boot builded app is production-proof ?
As writen in the comments http://start.spring.io/ is the best way to start Spring boot project(STS has integration for it).
If you want to use something that is not supported by Spring Boot you can init spring beans the same way you do it in xml, just use java configuration. See this for example: http://www.tutorialspoint.com/spring/spring_java_based_configuration.htm
Also useing xml is still available. You can add #ImportResource on your Configuration class
#EnableAutoConfiguration
#Configuration
#ImportResource({"classpath*:applicationContext.xml"})

Categories