[Download PDF – Spring Boot Sheet]
–Section– | —SQL Queries– |
Part 1: Spring Boot Introduction | Deployment, Embedded Server, Dependencies, Maven, STS, Run from IDE |
Part 2: Maven in Spring Boot | How Maven works, pom.xml, Spring Dependencies, Project Structure |
Part 3: Spring Boot Application | Application Properties, Value Injection, Spring Application Run |
Part 4: Spring Boot Basics | Spring Container, Inversion of Control, Dependency Injection, Autowiring |
Part 5: Spring REST Basics | Rest Controller, Request Mapping, Method Mapping |
Part 6: Spring Component Annotations | Component, Scanning, Constructor Injection, Setter Injection, Value Injection |
Part 7: Spring Multiple Components | Qualifier, Primary, Lazy Initialization, Global Lazy |
Part 8: Spring Bean Basics | Bean Scope, Scope Types |
Part 9: Spring Bean Lifecycle | Post Construct, Pre Destroy |
Part 10: Spring Bean Implementation | Bean Implementation, Configuration, Bean Injection |
Part 11: Spring JPA | Hibernate, Dependencies, Data Source Properties, Command Line Runner |
Part 12: Spring DAO: Create | ORM, Data Access Object DAO, Repository, EntityManager.persist, Transactional |
Part 13: Spring DAO: Read | EntityManager.find |
Download [PDF | Excel | Images | Spring Boot Project]
[Download PDF – Spring Boot Sheet]
Part 1: Spring Boot Introduction – Deployment, Embedded Server, Dependencies, Maven, STS, Run from IDE
Action | How to do? | Details | Images |
---|---|---|---|
Spring vs Spring Boot | 1. Spring Boot uses Spring internally. 2. Spring Boot makes it easier for users to use Spring 3. Spring Boot is easy to start as reduces configurations | ||
Spring Initializr | 1. Spring Initializr Project – https://start.spring.io/ 2. To create a quick startup project for Spring Boot 3. Uses Maven / Gradle 4. Download the project and import it into IDE | ||
Embeded Server | 1. Provides and embeded server like Tomcat inside the JAR file 2. JAR File generated will have the – application code + Tomcat | JAR – myapp.jar –> myapp code –> embedded tomcat | |
Running Spring Boot Apps | 1. App JAR can be run in standalone mode like 2. App can also be run from IDE | java – jar myapp.jar | |
Deployment | 1. Can be deployed as WAR file in external servers – like Tomcat, Apache etc. 2. In this case the WAR will not have the embeded server since it si not required | TOMCAT external server 1. WAR – myapp.war –> only myapp code | |
Dependency JARs | 1. You can manually download all the dependent JAR files in your Spring boot project 2. However, this will take a lot of manual effort | ||
Maven | 1. You can tell Maven the dependencies that you need in your Spring Boot project 2. Maven will automatically download all the dependent JAR files to your project at compile time | ||
STS 4 IDE | 1. Install JDK 17 or higher 2. Install STS 4 for Eclipse | ||
Spring Boot Project | File > New > Spring Starter Project 1. Service URL https://start.spring.io/ 2. Project – Maven 3. Language – Java 4. Spring Boot Version – stable version 5. Group / Package (com.helpercodes.demo1) 6. Artifact / Name (myapp) 7. Packaging – JAR 8. Java Version – 17 9. Dependencies – Spring WEB | spring-starter-projecct.png spring-starter-dependency.png | |
Spring Boot Starters | A collection of Maven dependencies with correct versions that are grouped together. | ||
Project Files | 1. src/main/java – com.helpercodes.MyappApplication.java 2. pom.xml | ||
Run from IDE | Run As > Spring Boot App Application will be started | ide-run-blank-project.png | |
Access App from Browser | Open Browser Hit the app URL – http://localhost:8080/ Likely to see Error since there is no REST controller defined | access-blank-app.png |
Part 2: Maven – How Maven works, pom.xml, Spring Dependencies, Project Structure
Action | How to do? | Details | Images |
---|---|---|---|
Maven Use Case | 1. Project Management tool 2. used for dependency management 3. Maven will automatically download and add the dependent JAR files to the project during compile/run time 4. Manual effort not required | ||
Maven Works? | 1. Reads your project config file (pom.xml) 2. Checks if the dependent JARs are available locally on your computer in Maven Local Repository 3. If not available, get the dependency JARs from Maven Central Repository Online 4. Saves the dependencies in the Maven Local Repository 5. Makes the dependecies avaialble for compilation and Runs | Maven 1. pom.xml 2. Maven Local Repo 3. Maven Central Repo 4. Build / Run | |
Secondary Dependencies | 1. If the dependcies mentioned in you config file pom.xml also need more dependencies 2. Then Maven will also download and pull the secondary dependencies | ||
Class Paths | Maven will add JARs automatically to your class path for compile and run | ||
Project Structure | myapp 1. pom.xml ——————- project config file 2. src/main/java ————- app java files 3. src/main/resources ——– app prop / config files 4. src/main/webapp ———— app web files – css,js… 5. src/test/java ————- unit test code 6. src/test/resources 7. target ——————– compiled code / artifacts 8. mvnw.cmd | project structure is standard and portable between IDEs | |
Project Object Model | pom.xml 1. project meta data 2. dependencies 3. plugins | <project> <groupId>…</groupId> <artifactId>…</artifactId> <version>…</version> <dependencies> … <dependency> <groupId>…</groupId> <artifactId>…</artifactId> </dependency> … </dependency> </project> | pom-structure.png |
Project Identification | Every Project can be uniquely identified by GAV 1. groupId ————– reverse domain name 2. artifactId ———— project name 3. version ————— version number | Example: groupId – com.helpercodes artifactId – myapp version – 1.0 | |
Dependency Identification | Visit the Maven Central Repository – https://central.sonatype.com/ All projects with GAV will be listed here and can be used as a depencency | spring-web-dependecy-repo.png | |
Maven Wrapper | mvnw.cmd or mvnw.sh File in Project Automatically downloads latest maven version if not found on system > mvnw clean compile test If Maven is already installed, then these files can be ignored > mvn clean compile test |
Part 3: Spring Boot Application – Application Properties, Value Injection, Spring Application Run
Action | Annotation | Meaning | Images |
---|---|---|---|
Spring Boot App | @SpringBootApplication public class MyappApplication { public static void main(String[] args) { SpringApplication.run(MyappApplication.class, args); } } | @SpringBootApplication – Defines a Spring Boot Application SpringApplication.run – Runs the Spring Application | spring-app-basic.png |
Application Properties File | Location: src/main/resources/application.properties By default created by Initialzr Loaded by Spring Boot during runtime 1. Can add Spring Boot properties 2. Can add custom properties | Example 1. server.port=8181 2. company.name=Helper Codes | application-properties.png |
Value Injection | To get property value from application.properties file into Class Variable @Value(“${company.name}”) private String companyName; | @Value – Pulls a property value from application.properties company.name=Helper Codes | value-injection.png value-injection-output.png |
Part 4: Spring Boot Basics – Spring Container, Inversion of Control, Dependency Injection, Autowiring
Action | Annotation | Meaning | Images |
---|---|---|---|
Spring Container Basics | 1. Spring Container – will work as Object Factory 2. Application code will ask Spring Container to give a Shop Object 3. Spring Container will use some configuration to decide which type of Shop Object to provide – Food Shop | Game Shop | Dress Shop | ||
Spring Container Functions | 1. Inversion of Control — create and manager objects 2. Dependency Injection — provide objects where required | ||
Spring Container Configuration | Any below config required so that Spring Container can inject correct objects – 1. XML configuration (not used – legacy) 2. Java Annotations 3. Java source Code | ||
Injection Types | 1. Constructor Injection — use when you have manadatory dependencies 2. Setter Injection — use when you haev optional dependencies | @Autowired annotation on constructor is constructor injection @Autowired annotation on setter is setter injection | |
Autowiring | Autowiring is used for Injecting Objects 1. Spring will try to match the expected object by class / interface 2. The Class must be annotated by @Component 3. Object of this class will be created and injected to the code |
Part 5: Spring REST Basics – Rest Controller, Request Mapping, Method Mapping
Action | Annotation | Meaning | Images |
---|---|---|---|
Rest Controller | @RestController @RequestMapping(“/myapp”) public class MyRestController { @GetMapping(“/”) public String sayHello() { return “Hello”; } } | @RestController – Defines Class that provides REST Web Service APIs @RequestMapping – Defines Class level Path for the REST Controller localhost:8080/myapp/ –> Hello | |
Rest Controller Endpoints | @RestController
@RequestMapping(“/myapp”) public class MyRestController { @GetMapping(“/info”) public String sayHello() { return “Hello There”; } } | @GetMapping – Defines Method and Path for endpoint GET request localhost:8080/myapp/info –> Hello | basic-rest-controller.png basic-rest-output.png |
Part 6: Spring Component Annotations – Component, Scanning, Constructor Injection, Setter Injection, Value Injection
Action | Annotation | Meaning | Images |
---|---|---|---|
Component Scanning | @SpringBootApplication – enables the component scanning Component Scanning will be done in — same package — all sub-packages To enable Component Scan for other package outside, @SpringBootApplication({“com.thirdpartycode”,”com.test”}) | @SpringBootApplication composed of 1. @EnableAutoConfiguration — spring boot auto config support 2. @ComponentScan — component scan on current and sub packages 3. @Configuration — register extra beans with @Bean annotation — import other config classes | |
Component Interface | public interface Shop { public String getShopName(); } | shop-interface.png | |
Component Implementation 1 | @Component public class FoodShop implements Shop { @Override public String getShopName() { return “Welcome to Food Shop!”; } } | @Component – Marks the Class as a Spring Bean. Component class can be injected as a dependency | food-shop-component.png |
Rest Controller Constructor Injection | @RestController @RequestMapping(“/shop”) public class ShopRestController { private Shop myShop; @Autowired public ShopRestController(Shop wiredShop) { myShop = wiredShop; } @GetMapping(“/name”) public String sayHello() { return myShop.getShopName(); } } | @AutoWired – Tells Spring Boot to inject the dependency. Since there is only one implementation of the Component Interface Spring will automatically figure out which Component Type to inject. What Spring does internally for autowiring? Shop wiredShop = new FoodShop(); ShopRestController controller = new ShopRestController(wiredShop) | shop-rest-controller.png food-app-output.png |
Rest Controller Setter Injection | @RestController @RequestMapping(“/shop”) public class ShopRestController { private Shop myShop; @Autowired public void setShop(Shop wiredShop) { this.myShop = wiredShop; } @GetMapping(“/name”) public String sayHello() { return myShop.getShopName(); } } | — Any method name can be used for injecting components What Spring does internally for autowiring? Shop wiredShop = new FoodShop(); ShopRestController controller = new ShopRestController(); controller.setShop(wiredShop); | shop-rest-controller-setter-inj.png food-app-output.png |
Rest Controller Field Injection | @RestController @RequestMapping(“/shop”) public class ShopRestController { @Autowired private Shop myShop; @GetMapping(“/name”) public String sayHello() { return myShop.getShopName(); } } | — Not recommended. Makes code harder to unit test — Any attribute can be used with @Autowired to inject it as a dependency | shop-rest-controller-field-injection.png |
Part 7: Spring Multiple Components – Qualifier, Primary, Lazy Initialization, Global Lazy
Action | Annotation | Meaning | Images |
---|---|---|---|
Component Implementation 2 | @Component public class ElectronicShop implements Shop { @Override public String getShopName() { return “Welcome to Electronic Shop!”; } } | — Multiple Interface implementation –Spring will throw error Field myShop in com.helpercodes.shopapp.ShopRestController required a single bean, but 2 were found: | electronic-shop-component.png |
Rest Controller Qualifier based Injection | @RestController @RequestMapping(“/shop”) public class ShopRestController { private Shop myShop; @Autowired public ShopRestController( @Qualifier(“electronicShop”) Shop wiredShop) { myShop = wiredShop; } @GetMapping(“/name”) public String sayHello() { return myShop.getShopName(); } } | @Qualifier(beanId) – tells Spring which bean to inject as the dependency beanId is the class name of the Component in camelCase @Qualifier can be used for both constructor and setter injection | food-app-electronic-output.png shop-rest-controller-qualifier.png |
Component Implementation Primary | @Component @Primary public class FoodShop implements Shop { public FoodShop() { System.out.println(“Contructing Food Shop”); } @Override public String getShopName() { return “Welcome to Food Shop!”; } } | @Primary – tells Spring which is the Primary Component in case of multiple Component implementation Only one Component from the multiple implementations can be Primary In this case if Qualifier is not present in the dependency injection, then the primary component will be injected If Qualifier is present in injection, then Qualifier has higher priority and Qualifier Component will be injected | food-shop-primary.png |
Component Implementation Lazy Initialization | @Component @Lazy public class ElectronicShop implements Shop { public ElectronicShop() { System.out.println(“Contructing Electronic Shop”); } @Override public String getShopName() { return “Welcome to Electronic Shop!”; } } | Normally, all Components and Beans are initialized suring startup. @Lazy – tell Spring to initialze the Bean only if it is needed by dependency injection or it is explicitly requested Since Food Shop is Primary Bean, Electronic Shop will not be initialized auotmatically. | food-shop-lazy.png |
Global Lazy Init | application.properties spring.main.lazy-initialization=true 1. All Beans will be Lazy initialized. 2. This includes Components and Controllers 3. When the REST endpoint is accessed, then the initialization will happen 4. First Component will be initialized for injection. Then Controller will be initialized and injected | Pro: Helps in fast startup for high number of components Con: Controller will not be initialized until requested by web page. Can throw issues like not enough memory in later stage when initialization is required | global-lazy-init.png |
Part 8: Spring Bean Basics – Bean Scope, Scope Types
Action | Annotation | Meaning | Images |
---|---|---|---|
Bean Scope | Scope defines lifecycle of the bean 1. Number of instances of the bean created. 2. Time for which the Bean exists 3. Sharing of the Bean | ||
Scope Types | 1. Singleton — Only one instance of the Bean is created. — The instance is saved in memory. — Same instance is shared and injected for all dependency injections 2. Prototype — New instance is created for each container request / injection 3. Request — New instance is created for each HTTP request 4. Session — New instance is created for each HTTP Session 5. Global Session — New instance is created for each HTTP Global Session | Default Scope in Spring is Singleton | |
Rest Controller Scope Check | @RestController @RequestMapping(“/shop”) public class ShopRestController { private Shop myShop1; private Shop myShop2; @Autowired public ShopRestController( @Qualifier(“foodShop”) Shop wiredShop1, @Qualifier(“foodShop”) Shop wiredShop2) { System.out.println(“Contructing Shop Rest Controller”); myShop1 = wiredShop1; myShop2 = wiredShop2; } @GetMapping(“/check”) public String cehckScope() { if(myShop1==myShop2) { return “Singleton Scope”; } else { return “Prototype Scope”; } } } | shop-controller-scope-check.png singleton-scope-output.png | |
Scope Prototype | @Component @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public class FoodShop implements Shop { @Override public String getShopName() { return “Welcome to Food Shop!”; } } | @Scope – defines an explicit scope for the Bean / Component Prototype beans are lazy by default. Custom Destroy Method is not called by Spring for Prototype Scope Beans | food-shop-prototype-scope.png prototype-scope-output.png |
Part 9: Spring Bean LifeCycle – Post Construct, Pre Destroy
Action | Annotation | Meaning | Images |
---|---|---|---|
Bean Lifecycle | 1. Spring Container start 2. Bean Instantiation 3. Dependency Injection 4. Internal Spring Processing 5. Custom Init Method … 6. Container Shutdown 7. Custom Destroy Method | ||
Use Cases | Custom Init Method can: 1. call custom business logic for init 2. init resource connections to DB, files etc. Custom Destroy Method can: 1. call custom business logic for destruction 2. cleanup resource connections to DB, files etc. | ||
Custom Init Custom Destroy | @Component public class FoodShop implements Shop { public FoodShop() { System.out.println(“Contructing Food Shop”); } @PostConstruct public void initFoodShop() { System.out.println(“Initializing Food Shop”); } @PreDestroy public void destroyFoodShop() { System.out.println(“Destroying Food Shop”); } @Override public String getShopName() { System.out.println(“Welcome to Food Shop!”); return “Welcome to Food Shop!”; } } | @PostConstruct – defines a custom method to call during Bean Initialization @PreDestroy – defines a custom method to call during Bean Destruction For prototype scoped beans, Spring does not call the destroy method. | food-shop-custom-init-destroy.png |
Part 10: Spring Bean Implementation – Configuration, Bean Injection
Action | Annotation | Meaning | Images |
---|---|---|---|
Bean Implementation | public class CarShop implements Shop { public CarShop() { System.out.println(“Contructing Car Shop”); } @Override public String getShopName() { return “Welcome to Car Shop!”; } } | Component Annotation is not used here | car-shop-implementation.png |
Bean Configuration | @Configuration public class ShopConfig { @Bean public Shop carShop() { return new CarShop(); } } | @Configuration – defines Config class to define beans @Bean – defines the Beaan Implementation class as a Bean Bean manually constructs the class and returns instance for it. Bean ID is the method Name by default. @Bean(“myCustomBean”) => will set bean id as myCustomBean that needs to be used with @Qualifier during dependency injection | shop-configuration.png |
Bean Injection | @RestController @RequestMapping(“/shop”) public class ShopRestController { private Shop myShop1; @Autowired public ShopRestController( @Qualifier(“carShop”) Shop wiredShop1 ) { System.out.println(“Constructing Shop Rest Controller”); myShop1 = wiredShop1; } @GetMapping(“/name”) public String sayHello() { return myShop1.getShopName(); } } | @Qualifier – needs to provide the bean id to be injected | shop-rest-controller-bean-injection.png car-shop-bean-output.png |
Bean Use Case | 1. To make existing third party class available to Spring 2. No Access to the Source code to mark it as Component 3. But still want to use the class as a Spring Bean 4. This Bean can now be injected in code |
Part 11: Spring JPA – Hibernate, Dependencies, Data Source Properties, Command Line Runner
Action | Annotation | Meaning | Images |
---|---|---|---|
JPA | JPA is specification (interface) of API that implements ORM 1. ORM Framework – Object to Relational Mapping 2. Map the Java Object Attributes to Database Columns | ||
Hibernate | 1. Implementation of JPA specification 2. Vendor implementation in Spring to implement JPA 3. Save / Retrieve Java objects to Database Spring App –> JPA Spec –> Hibernate Impl –> JDBC –> Database | ||
Dependencies | <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> | add the dependencies in pom.xml for data-jpa and the type of database you want to connect. we will use a H2 database in this case. | pom-db-dependency.png |
Datasource Connection Properties | application.properties spring.datasource.url=jdbc:h2:file:/data/empapp spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password=password | Database connection properties | app-properties-db.png |
Command Line Application | @SpringBootApplication public class MyappApplication { public static void main(String[] args) { SpringApplication.run(MyappApplication.class, args); } @Bean public CommandLineRunner commandLineRunner(String[] args) { return runner -> { System.out.println(“In Command Line Runner”); }; } } | the command line runner code will run in command line and show output in the command line console runs code after spring is loaded | cmd-line-app.png cmd-line-output.png |
Part 12: Spring DAO – Object to Relational Mapping ORM, ID GeneratedValue Strategy, Data Access Object DAO, Repository, EntityManager, Transactional
Action | Annotation | Meaning | Images |
---|---|---|---|
Java Object to Database Mapping ORM | @Entity @Table(name=”employee”) public class Employee { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name=”id”) private int id; @Column(name=”display_name”) private String displayName; @Column(name=”email”) private String email; public Employee() {} //all-arg constructor //getters //setters //to string } | @Entity – defines the class has a mapping with a Database Table No Arg constructor is required @Table – defines the name of the Database Table to which the Java Class maps @Column – defines the name of the database column to which the Class attribute maps to @Id – defines the Primary Key / Identity Column of the Database Table | db-employee.png |
ID Generation | @GeneratedValue(strategy=GenerationType.AUTO) @GeneratedValue(strategy=GenerationType.IDENTITY) @GeneratedValue(strategy=GenerationType.SEQUENCE) @GeneratedValue(strategy=GenerationType.TABLE) @GeneratedValue(strategy=GenerationType.UUID) | @GeneratedValue – defines how the ID column values will be generated IDENTITY – means ID column values are generated by an identity column in the database. They are auto-incremented. SEQUENCE – means ID column values are generated by a database sequence TABLE – means ID column values come from a separate database table column defined UUID – means ID column values will be generated by JPA in UUID format | |
DAO Basic Architecture | App –> DAO –> Entity Manager –> Data Source –> Database | DAO is the Database Access Object Class. This will use the EntityManager API and interact with Database for CRUD operation | |
DAO Interface Create | public interface EmployeeDAO { void create (Employee employee); } | emp-dao-create.png | |
DAO Implementation Create | @Repository public class EmployeeDAOImpl implements EmployeeDAO { private EntityManager entityManager; @Autowired public EmployeeDAOImpl(EntityManager entityManager) { this.entityManager = entityManager; } @Override @Transactional public void create(Employee employee) { entityManager.persist(employee); } } | @Repository – defines class component which is the DAO @Transactional – defines the method should execute in a atomic transaction. Autowiring can inject the EntityManager Component in the Repository Constructor | emp-dao-impl-create.png |
Runner App Create | @Bean @Autowired public CommandLineRunner commandLineRunner (EmployeeDAO employeeDAO) { return runner -> { System.out.println(“In Command Line Runner”); createEmployee(employeeDAO, “Jake Peralta”, “jake.peralta@comedy.com”); }; } private void createEmployee(EmployeeDAO employeeDAO, String name, String email) { System.out.println(“Create Employee Start”); Employee employee = new Employee(name, email); employeeDAO.create(employee); System.out.println(“Employee Created:”+employee.toString()); } | Autowiring can inject the EmployeeDAO Component in the Command Line Runner Bean | emp-create-runner.png emp-create-output.png |