Lazy Initialization :

hitarth's Vault // Exported at - 10:04:PM

Pasted image 20240120132838.png

Scope :

Pasted image 20240120132904.png
Pasted image 20240120133402.png
Pasted image 20240120133623.png

Post Construct and Pre Destroy :

Pasted image 20240120142641.png

Evolution of Jakarta EE: vs J2EE vs Java EE

Pasted image 20240120142802.png

Jakarta Contexts & Dependency Injection (CDI)

Pasted image 20240120142925.png

package com.example.demo.examples.jakarta;

import com.example.demo.businessAssignment.components.DataService;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jdk.jfr.Name;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Arrays;

//@Component
@Named // we can use @Named and @Inject instead of @Component and @Inject respectively 
class BusinessService {
	private DataService2 dataService;

//	@Autowired
	@Inject
	public void setDataService(DataService2 dataService) {
		System.out.println("Setter called");
		this.dataService = dataService;
	}

	public DataService2 getDataService() {
		System.out.println("Setter injection");
		return dataService;
	}

}

//@Component
@Named
class DataService2 {

}

@Configuration
@ComponentScan // performs a component scan on the current package
public class CdiContextLauncherApp {

	public static void main(String[] args) {
		var context = new AnnotationConfigApplicationContext(CdiContextLauncherApp.class);
		Arrays.stream(context.getBeanDefinitionNames()).forEach((System.out::println));
		System.out.println(context.getBean(BusinessService.class).getDataService());
	}

}

Spring Stereotype Annotations - @Component & more

Pasted image 20240120154617.png

Quick Review of Important Spring Annotations

Pasted image 20240120154707.png
Pasted image 20240120154718.png
Pasted image 20240120154823.png
Pasted image 20240120154836.png

Quick Review of Important Spring Concepts :

Pasted image 20240120154933.png

Spring Boot Dev Tools :

  • Allows us to restart the server for every change made in the code.
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>

Application Properties :

Logging Levels :

Pasted image 20240121172010.png

Pasted image 20240121172024.png

  • Trace : Prints all the errors
  • Debug prints the errors listed below it
  • similarly info prints errors listed below it and so on...

To add a LOT of different configuration properties :

Picking up configs from application.properties file
Pasted image 20240121182214.png

Running the .jar file :

Pasted image 20240121183814.png

Monitor Applications using Spring Boot Actuator

Pasted image 20240121184010.png

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-actuator -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

Pasted image 20240121184800.png

Pasted image 20240121185801.png

Getting Started with JPA, Hibernate, Spring and Spring Boot :

Pasted image 20240122161326.png
Pasted image 20240122163433.png

  • ignore the user=SA part
  • this is a dynamic url so it changes every time we restart the application, so lets configure a static url
    Pasted image 20240122163809.png
  • Pasted image 20240122163830.png

JDBC vs Spring JDBC

Both the code blocks will perform the same task but the Spring JDBC requires less code compared to JDBC

indexing : command line runner, jdbc, sql, insert into, @Repository, JdbcTemplate

Pasted image 20240122175134.png

Using Spring JDBC to execute SQL queries and much more :

Pasted image 20240122194734.png
Pasted image 20240122194845.png
Pasted image 20240122194931.png
Pasted image 20240122194951.png
Pasted image 20240122195006.png

  • Pasted image 20240122195237.png
    This approach is not so efficient and with a huge database things will start to fall apart. This is where Spring JPA comes into play :

Using JPA

  • we'll create a mapping between our java bean and the table and we'll use this mapping to insert or retrieve values.
    Pasted image 20240122215829.png
    Pasted image 20240122215903.png
    Pasted image 20240122215840.png
    Pasted image 20240122215915.png
    Pasted image 20240122215926.png
    Pasted image 20240122221028.png

Using Spring Data JPA :

Pasted image 20240122221324.png

  • with this, we don't even have to worry about entity manager now.
    Pasted image 20240122231726.png
  • in the image below, in <Course, Integer> integer is the ID type
    Pasted image 20240122231735.png
    Pasted image 20240122231749.png
    Pasted image 20240122231759.png

Hibernate vs JPA :

Pasted image 20240122233839.png

"myfirstwebapp" : ToDoList ☠️☠️ :

Pasted image 20240123184031.png
or
Pasted image 20240123184047.png

Model 1 :

Pasted image 20240125235801.png
Pasted image 20240125235812.png
Pasted image 20240125235832.png
Pasted image 20240125235846.png
Pasted image 20240125235902.png
Pasted image 20240125235924.png


Model 2 :

  • <form method="post">...</form> is to be used whenever you want to transfer the data
    • get method reveals the name and password in the URL

Pasted image 20240126223841.png
Pasted image 20240126223847.png
Pasted image 20240126223900.png
Pasted image 20240126223912.png
Pasted image 20240126223924.png

Model 3 : To Do List :

Pasted image 20240126235138.png

  • SessionAttributes are to be added like this :
    Pasted image 20240126235217.png

JSTL Dependencies :

<dependency>  
    <groupId>jakarta.servlet.jsp.jstl</groupId>  
    <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>  
</dependency>  
  
<dependency>  
    <groupId>org.eclipse.jetty</groupId>  
    <artifactId>glassfish-jstl</artifactId>  
</dependency>

Pasted image 20240127001344.png

Validations :

Description: <input type="text" name="description" required/>

Predicate Object : Lambda Function :

Pasted image 20240127162723.png