504 Spring Boot Project Structure
504 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
www.luv2code.com
Maven Standard Directory Structure
Directory Description
www.luv2code.com
Maven Standard Directory Structure
Directory Description
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
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
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
www.luv2code.com
Maven Wrapper files
• mvnw allows you to run a Maven project
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
www.luv2code.com
Maven Wrapper files
• If you already have Maven installed previously
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
<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
<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
<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:
www.luv2code.com
Java Source Code
www.luv2code.com
Java Source Code
www.luv2code.com
Java Source Code
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 {
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 {
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
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
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
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 {
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 {
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 {
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 {
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 {
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);
}
}
www.luv2code.com
More on Component Scanning Best
Pract
ice
www.luv2code.com
More on Component Scanning Best
Pract
ice
www.luv2code.com
More on Component Scanning Best
Pract
ice
www.luv2code.com
More on Component Scanning Best
Pract
ice
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
www.luv2code.com
More on Component Scanning
www.luv2code.com
More on Component Scanning
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
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
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
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
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
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 my props
coach.name=Mickey Mouse
team.name=The Mouse Crew
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
www.luv2code.com
Application Properties
• Read data from: application.properties
@Value("${team.name}")
private String teamName;
www.luv2code.com
Application Properties
• Read data from: application.properties
@Value("${team.name}")
private String teamName;
…
}
www.luv2code.com
Static Content
www.luv2code.com
Static Content
www.luv2code.com
Static Content
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.
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
www.luv2code.com
Unit Tests
www.luv2code.com
Unit Tests
www.luv2code.com
Unit Tests
www.luv2code.com
Unit Tests
www.luv2code.com