Today we are going to learn how to setup spring boot application in intellij and eclipse. It is very easy to use and using spring boot we can easily develop enterprise level application on the go.
My personal favorite go to IDE is intellij since it has many shortcuts and easy to use, but most prefer eclipse as well. So we are going to have both tutorial on how to setup and run spring boot application using intellij and eclipse.
Lets get started.
First we are going to see how to use intellij IDEA to setup a spring boot application project. It has an inbuilt feature to create an spring boot project using spring initializer which makes us really easy to set up the project in no time at all.
Setting Up Spring Boot Project / Application with Intellij IDEA
Run intellij IDEA and click create new project or File > New > Project
.
After that it select spring initializer and click next selecting default radio button.
In this part you will be allowed to choose dependencies that you would like to use it in your project. Select according to the category below and press next.
- Core – DevTools, Security
- Web – Web, RestRespositories
- SQL – JPA, MySQL, JDBC
Time to fill up the information for your project. Take a look below.
Press Next and give the folder name for your project and the path where you want to save it.
Press next and wait while spring initializer starts to setup your project and download all dependency that are needed to require to run the spring boot application.
Voila, its ready.
Now, check for the folder in your project directory, you will find the java class named SpringbootApplication.java
Open it and right click > Run Spring Boot Application
.
One of the wonderful things that comes with spring boot is that embedded tomcat server dependency which allows us to directly run project into the server without having to configure it.
It will be up and running in less than a minute or two.
When you run your application, sometimes it may fail to start showing you this kind of error like below :-
We are using spring-jdbc and jpa so our application requires to have configured datasource url in order to proceed. That’s why we need to specify the configuration for datasource.
Create new class where SpringBootApplication.java
is name it AppConfig.java
and annotate it with @Configuration
annotation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.codeinhouse.springboot; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DriverManagerDataSource; import javax.sql.DataSource; @Configuration public class AppConfig { @Bean public DataSource dataSource(){ DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/dbname"); dataSource.setUsername( "root" ); dataSource.setPassword( "" ); return dataSource; } } |
Run it again and check your application in the browser & it will run successfully on port 8080
by default.
Setting Up Spring Boot Project / Application with Eclipse
Setting up spring boot application is pretty straight and forward in eclipse too. What i hate about eclipse is that its damn slow than intellij but has many easy shortcuts to work with.
Open your browser and go to https://start.spring.io/
Take a look at the image below and fill in the options.
Press Generate and a zip with a artifact id which would be app in our case will be downloaded. Extract it in the place where you want your project to be saved.
Open Eclipse and Go to File > Import
and type maven and select existing maven project.
Press next and click browse and select the directory where you have just extract the app.zip
that you have just downloaded. It will look for pom file inside it, after all steps click finish.
If you notice bottom right corner of the your eclipse IDE you will see that eclipse setting up your project. Wait till it gets 100%.
From the project explorer expand your project and expand src/main/java
and open AppApplication.java
and right click > Run as AppApplication.java
Thats, it run and you will finally have spring boot setup and ready to build real world application using spring boot. Must not forget that we need to configure datasource url also like we did in intellij IDEA.
Leave a Reply