0% found this document useful (0 votes)
535 views101 pages

504 Spring Boot Project Structure

The document discusses the standard project structure generated by Spring Initializr when creating a new Spring Boot project. It describes the key directories including src/main/java for Java code, src/main/resources for properties files, and src/test/java for tests. It also explains the Maven wrapper files that allow running Maven without installing it locally, and the pom.xml file which specifies the dependencies and other metadata for the project.

Uploaded by

Rohit Solanki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
535 views101 pages

504 Spring Boot Project Structure

The document discusses the standard project structure generated by Spring Initializr when creating a new Spring Boot project. It describes the key directories including src/main/java for Java code, src/main/resources for properties files, and src/test/java for tests. It also explains the Maven wrapper files that allow running Maven without installing it locally, and the pom.xml file which specifies the dependencies and other metadata for the project.

Uploaded by

Rohit Solanki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 101

Spring Boot Project Structure

m
Spring Initializr

www.luv2code.com
Spring Initializr
• Spring Initializr created a Maven project for us

http://start.spring.io

www.luv2code.com
Spring Initializr
• Spring Initializr created a Maven project for us


http://start.spring.io
• Let's explore the project structure

www.luv2code.com
Maven Standard Directory Structure

www.luv2code.com
Maven Standard Directory Structure

Directory Description

src/main/java Your Java source code

www.luv2code.com
Maven Standard Directory Structure

Directory Description

src/main/java Your Java source code

src/main/resources Properties / config files used by your app

www.luv2code.com
Maven Standard Directory Structure

Directory Description

src/main/java Your Java source code

src/main/resources Properties / config files used by your app

src/test/java Unit testing source code

www.luv2code.com
Maven Wrapper files

www.luv2code.com
Maven Wrapper files

www.luv2code.com
Maven Wrapper files

www.luv2code.com
Maven Wrapper files
• mvnw allows you to run a Maven project

www.luv2code.com
Maven Wrapper files
• mvnw allows you to run a Maven project

• No need to have Maven installed or present on your path

www.luv2code.com
Maven Wrapper files
• mvnw allows you to run a Maven project

• No need to have Maven installed or present on your path

• If correct version of Maven is NOT found on your computer

www.luv2code.com
Maven Wrapper files
• mvnw allows you to run a Maven project

• No need to have Maven installed or present on your path

• If correct version of Maven is NOT found on your computer

• Automatically downloads correct version


and runs Maven

www.luv2code.com
Maven Wrapper files
• mvnw allows you to run a Maven project

• No need to have Maven installed or present on your path

• If correct version of Maven is NOT found on your computer

• Automatically downloads correct version


and runs Maven

• Two files are provided

www.luv2code.com
Maven Wrapper files
• mvnw allows you to run a Maven project

• No need to have Maven installed or present on your path

• If correct version of Maven is NOT found on your computer

• Automatically downloads correct version


and runs Maven > mvnw clean compile test
• Two files are provided

• mvnw.cmd for MS Windows

www.luv2code.com
Maven Wrapper files
• mvnw allows you to run a Maven project

• No need to have Maven installed or present on your path

• If correct version of Maven is NOT found on your computer

• Automatically downloads correct version 



and runs Maven > mvnw clean compile test
• Two files are provided

• mvnw.cmd for MS Windows

• mvnw.sh for Linux/Mac $ ./mvnw clean compile test

www.luv2code.com
Maven Wrapper files

www.luv2code.com
Maven Wrapper files
• If you already have Maven installed previously

www.luv2code.com
Maven Wrapper files
• If you already have Maven installed previously

• Then you can ignore/delete the mvnw files

www.luv2code.com
Maven Wrapper files
• If you already have Maven installed previously

• Then you can ignore/delete the mvnw files


• Just use Maven as you normally would

$ mvn clean compile test

www.luv2code.com
Maven POM file

www.luv2code.com
Maven POM file
• pom.xml includes info that you entered at Spring Initializr website

www.luv2code.com
Maven POM file
• pom.xml includes info that you entered at Spring Initializr website

<groupId>com.luv2code.springboot.demo</groupId>
<artifactId>mycoolapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

www.luv2code.com
Maven POM file
• pom.xml includes info that you entered at Spring Initializr website

<groupId>com.luv2code.springboot.demo</groupId>
<artifactId>mycoolapp</artifactId>
<dependencies>
<version>0.0.1-SNAPSHOT</version>
<dependency>
<packaging>jar</packaging>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

www.luv2code.com
Maven POM file Spring Boot Starters

A collection of Maven dependencies


• pom.xml includes info that you entered at Spring Initializr website
(Compatible versions)

<groupId>com.luv2code.springboot.demo</groupId>
<artifactId>mycoolapp</artifactId>
<dependencies>
<version>0.0.1-SNAPSHOT</version>
<dependency>
<packaging>jar</packaging>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

www.luv2code.com
Maven POM file Spring Boot Starters

A collection of Maven dependencies


• pom.xml includes info that you entered at Spring Initializr website
(Compatible versions)

<groupId>com.luv2code.springboot.demo</groupId>
<artifactId>mycoolapp</artifactId>
<dependencies>
<version>0.0.1-SNAPSHOT</version>
<dependency>
<packaging>jar</packaging>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
spring-web
<artifactId>spring-boot-starter-test</artifactId>
spring-webmvc
<scope>test</scope> hibernate-validator
</dependency>
tomcat

</dependencies> json

www.luv2code.com
Maven POM file Spring Boot Starters

A collection of Maven dependencies


• pom.xml includes info that you entered at Spring Initializr website
(Compatible versions)

<groupId>com.luv2code.springboot.demo</groupId>
<artifactId>mycoolapp</artifactId>
<dependencies>
<version>0.0.1-SNAPSHOT</version>
<dependency>
<packaging>jar</packaging>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
Save's the developer from having to list all of the
<groupId>org.springframework.boot</groupId>
spring-web
individual dependencies
<artifactId>spring-boot-starter-test</artifactId>
spring-webmvc
<scope>test</scope> hibernate-validator
</dependency>
Also, makes sure you have compatible versions tomcat

</dependencies> json

www.luv2code.com
Maven POM file

www.luv2code.com
Maven POM file
• Spring Boot Maven plugin

www.luv2code.com
Maven POM file
• Spring Boot Maven plugin

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

www.luv2code.com
Maven POM file To package executable jar
or war archive

• Spring Boot Maven plugin Can also easily run the app

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

www.luv2code.com
Maven POM file To package executable jar
or war archive

• Spring Boot Maven plugin Can also easily run the app

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
$ ./mvnw package

$ ./mvnw spring-boot:run

www.luv2code.com
Maven POM file To package executable jar
or war archive

• Spring Boot Maven plugin Can also easily run the app

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
$ ./mvnw package
Can also just use:

mvn package $ ./mvnw spring-boot:run


mvn spring-boot:run

www.luv2code.com
Java Source Code

www.luv2code.com
Java Source Code

Main Spring Boot application class

Created by Spring Initializr

www.luv2code.com
Java Source Code

Main Spring Boot application class

Created by Spring Initializr

RestController that we created


in an earlier video

www.luv2code.com
Java Source Code

www.luv2code.com
Java Source Code
File: MycoolappApplication.java

package com.luv2code.springboot.demo.mycoolapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MycoolappApplication {

public static void main(String[] args) {


SpringApplication.run(MycoolappApplication.class, args);
}
}

www.luv2code.com
Java Source Code
File: MycoolappApplication.java

package com.luv2code.springboot.demo.mycoolapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MycoolappApplication {

public static void main(String[] args) {


SpringApplication.run(MycoolappApplication.class, args);
}
}

www.luv2code.com
Java Source Code
File: MycoolappApplication.java

package com.luv2code.springboot.demo.mycoolapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MycoolappApplication { Enables
public static void main(String[] args) {
Auto configuration
SpringApplication.run(MycoolappApplication.class, args);
} Component scanning
} Additional configuration

www.luv2code.com
Java Source Code
File: MycoolappApplication.java

package com.luv2code.springboot.demo.mycoolapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MycoolappApplicationComposed
{ of Enables
following annotations
public static void main(String[] args) {
@EnableAutoConfiguration
Auto configuration
SpringApplication.run(MycoolappApplication.class, args);
} Component
@ComponentScan
scanning
} Additional
@Configuration
configuration

www.luv2code.com
Annotations

www.luv2code.com
Annotations
• @SpringBootApplication is composed of the following annotations:

Annotation Description

www.luv2code.com
Annotations
• @SpringBootApplication is composed of the following annotations:

Annotation Description

@EnableAutoConfiguration Enables Spring Boot's auto-configuration support

www.luv2code.com
Annotations
• @SpringBootApplication is composed of the following annotations:

Annotation Description

@EnableAutoConfiguration Enables Spring Boot's auto-configuration support

Enables component scanning of current package


@ComponentScan
Also recursively scans sub-packages

www.luv2code.com
Annotations
• @SpringBootApplication is composed of the following annotations:

Annotation Description

@EnableAutoConfiguration Enables Spring Boot's auto-configuration support

Enables component scanning of current package


@ComponentScan
Also recursively scans sub-packages

Able to register extra beans with @Bean



@Configuration
or import other configuration classes

www.luv2code.com
Annotations
• @SpringBootApplication is composed of the following annotations:

Annotation Description

@EnableAutoConfiguration Enables Spring Boot's auto-configuration support

Enables component scanning of current package


@ComponentScan Same annotations
Also recursively scans sub-packages
that we've used before
Able to with traditional
register Spring
extra beans apps
with @Bean

@Configuration
or import other configuration classes

www.luv2code.com
Annotations
• @SpringBootApplication is composed of the following annotations:

Annotation Description

@EnableAutoConfiguration Enables Spring Boot's auto-configuration support

Enables component scanning of current package


@ComponentScan
Also recursively scans sub-packages

Able to register extra beans with @Bean



@Configuration
or import other configuration classes

www.luv2code.com
Java Source Code

www.luv2code.com
Java Source Code
File: MycoolappApplication.java

package com.luv2code.springboot.demo.mycoolapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MycoolappApplication {

public static void main(String[] args) {


SpringApplication.run(MycoolappApplication.class, args);
}
}

www.luv2code.com
Java Source Code
File: MycoolappApplication.java

package com.luv2code.springboot.demo.mycoolapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MycoolappApplication {

public static void main(String[] args) {


SpringApplication.run(MycoolappApplication.class, args);
}
}

www.luv2code.com
Java Source Code
File: MycoolappApplication.java

package com.luv2code.springboot.demo.mycoolapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MycoolappApplication {

public static void main(String[] args) {


SpringApplication.run(MycoolappApplication.class, args);
}
}

Bootstrap your Spring Boot application

www.luv2code.com
Java Source Code
File: MycoolappApplication.java

package com.luv2code.springboot.demo.mycoolapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MycoolappApplication {

public static void main(String[] args) {


SpringApplication.run(MycoolappApplication.class, args);
}
}

Bootstrap your Spring Boot application

www.luv2code.com
Java Source Code
File: MycoolappApplication.java

package com.luv2code.springboot.demo.mycoolapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MycoolappApplication {

public static void main(String[] args) {


SpringApplication.run(MycoolappApplication.class, args);
}
}

Bootstrap your Spring Boot application

www.luv2code.com
Java Source Code
File: MycoolappApplication.java
Behind the scenes …

package com.luv2code.springboot.demo.mycoolapp;
Creates application context
import org.springframework.boot.SpringApplication; and registers all beans
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
Starts the embedded server
public class MycoolappApplication { Tomcat etc…
public static void main(String[] args) {
SpringApplication.run(MycoolappApplication.class, args);
}
}

Bootstrap your Spring Boot application

www.luv2code.com
More on Component Scanning Best
Pract
ice

www.luv2code.com
More on Component Scanning Best
Pract
ice

Place your main application class in


the root package above your other packages

www.luv2code.com
More on Component Scanning Best
Pract
ice

Place your main application class in


the root package above your other packages

• This implicitly defines a base search package

• Allows you to leverage default component scanning

• No need to explicitly reference the base package name

www.luv2code.com
More on Component Scanning Best
Pract
ice

Place your main application class in


the root package above your other packages

• This implicitly defines a base search package

• Allows you to leverage default component scanning

• No need to explicitly reference the base package name

Common pitfall in traditional Spring apps

Anyone use the wrong package name before in traditional Spring apps???

www.luv2code.com
More on Component Scanning

www.luv2code.com
More on Component Scanning

Main Spring Boot application class

Automatically component scans sub-packages

www.luv2code.com
More on Component Scanning

Main Spring Boot application class

Automatically component scans sub-packages

Includes "rest" sub-package


www.luv2code.com
More on Component Scanning

Main Spring Boot application class

Automatically component scans sub-packages

Includes "rest" sub-package


Any other sub-packages you create


Can give them any name

www.luv2code.com
More on Component Scanning

www.luv2code.com
More on Component Scanning
• Default scanning is fine if everything is under

• com.luv2code.springboot.demo.mycoolapp

www.luv2code.com
More on Component Scanning
• Default scanning is fine if everything is under

• com.luv2code.springboot.demo.mycoolapp

• But what about my other packages?

www.luv2code.com
More on Component Scanning
• Default scanning is fine if everything is under

• com.luv2code.springboot.demo.mycoolapp

• But what about my other packages?


• org.acme.iot.utils

www.luv2code.com
More on Component Scanning
• Default scanning is fine if everything is under

• com.luv2code.springboot.demo.mycoolapp


• But what about my other packages?


• org.acme.iot.utils
• edu.cmu.wean

www.luv2code.com
More on Component Scanning
• Default scanning is fine if everything is under

• com.luv2code.springboot.demo.mycoolapp


• But what about my other packages?


• org.acme.iot.utils
package com.luv2code.springboot.demo.mycoolapp;
• edu.cmu.wean …
@SpringBootApplication(
scanBasePackages={"com.luv2code.springboot.demo.mycoolapp",
"org.acme.iot.utils",
"edu.cmu.wean"})
public class MycoolappApplication {

}

www.luv2code.com
More on Component Scanning
• Default scanning is fine if everything is under

• com.luv2code.springboot.demo.mycoolapp

Explicitly list
base packages to scan
• But what about my other packages?
• org.acme.iot.utils
package com.luv2code.springboot.demo.mycoolapp;
• edu.cmu.wean …
@SpringBootApplication(
scanBasePackages={"com.luv2code.springboot.demo.mycoolapp",
"org.acme.iot.utils",
"edu.cmu.wean"})
public class MycoolappApplication {

}

www.luv2code.com
Application Properties
• By default, Spring Boot will load properties from: application.properties

www.luv2code.com
Application Properties
• By default, Spring Boot will load properties from: application.properties

Created by Spring Initializr

Empty at the beginning

www.luv2code.com
Application Properties
• By default, Spring Boot will load properties from: application.properties

Created by Spring Initializr

Empty at the beginning

Can add Spring Boot properties


server.port=8585

www.luv2code.com
Application Properties
• By default, Spring Boot will load properties from: application.properties

Created by Spring Initializr

Empty at the beginning

Can add Spring Boot properties


server.port=8585

Also add your own custom properties


coach.name=Mickey Mouse

www.luv2code.com
Application Properties
• Read data from: application.properties

www.luv2code.com
Application Properties
• Read data from: application.properties

www.luv2code.com
Application Properties
• Read data from: application.properties

# configure server port


server.port=8484

# configure my props
coach.name=Mickey Mouse
team.name=The Mouse Crew

www.luv2code.com
Application Properties
• Read data from: application.properties

# configure server port


server.port=8484 @RestController
public class FunRestController {
# configure my props
coach.name=Mickey Mouse
team.name=The Mouse Crew

www.luv2code.com
Application Properties
• Read data from: application.properties

# configure server port


server.port=8484 @RestController
public class FunRestController {
# configure my props
coach.name=Mickey Mouse @Value("${coach.name}")
team.name=The Mouse Crew private String coachName;

www.luv2code.com
Application Properties
• Read data from: application.properties

# configure server port


server.port=8484 @RestController
public class FunRestController {
# configure my props
coach.name=Mickey Mouse @Value("${coach.name}")
team.name=The Mouse Crew private String coachName;

www.luv2code.com
Application Properties
• Read data from: application.properties

# configure server port


server.port=8484 @RestController
public class FunRestController {
# configure my props
coach.name=Mickey Mouse @Value("${coach.name}")
team.name=The Mouse Crew private String coachName;

@Value("${team.name}")
private String teamName;

www.luv2code.com
Application Properties
• Read data from: application.properties

# configure server port


server.port=8484 @RestController
public class FunRestController {
# configure my props
coach.name=Mickey Mouse @Value("${coach.name}")
team.name=The Mouse Crew private String coachName;

@Value("${team.name}")
private String teamName;


}

www.luv2code.com
Static Content

www.luv2code.com
Static Content

By default, Spring Boot will load static


resources from "/static" directory

www.luv2code.com
Static Content

By default, Spring Boot will load static


resources from "/static" directory

Examples of static resources


HTML files, CSS, JavaScript, images, etc …

www.luv2code.com
Static Content

www.luv2code.com
Static Content

WARNING:
Do not use the src/main/webapp directory if your application is packaged as a JAR.


Although this is a standard Maven directory, it works only with WAR packaging. 


It is silently ignored by most build tools if you generate a JAR.

www.luv2code.com
Templates

www.luv2code.com
Templates
• Spring Boot includes auto-configuration for following template engines

www.luv2code.com
Templates
• Spring Boot includes auto-configuration for following template engines

• FreeMarker

www.luv2code.com
Templates
• Spring Boot includes auto-configuration for following template engines

• FreeMarker

• Thymeleaf

www.luv2code.com
Templates
• Spring Boot includes auto-configuration for following template engines

• FreeMarker

• Thymeleaf

• Mustache

www.luv2code.com
Templates
• Spring Boot includes auto-configuration for following template engines

• FreeMarker

• Thymeleaf

• Mustache

www.luv2code.com
Templates
• Spring Boot includes auto-configuration for following template engines

• FreeMarker
By default, Spring Boot will load
templates from "/templates" directory
• Thymeleaf

• Mustache

www.luv2code.com
Templates
• Spring Boot includes auto-configuration for following template engines

• FreeMarker
By default, Spring Boot will load
templates from "/templates" directory
• Thymeleaf

• Mustache

Thymeleaf is a popular template engine


We will use it later in the course

www.luv2code.com
Unit Tests

www.luv2code.com
Unit Tests

www.luv2code.com
Unit Tests

Spring Boot unit test class

Created by Spring Initializr

www.luv2code.com
Unit Tests

Spring Boot unit test class

Created by Spring Initializr

You can add unit tests to the file

www.luv2code.com

You might also like