Saturday, 14 September 2019

Spring Boot Interview Questions


create a demo Project-






you can also go directly to https://starts.spring.io to create spring boot starter project

it will open spring initialize and give the same option as in spring tool suits.it will give option to download the project structure.


now just have a look what just happened-

↣Boilerplate structure created
having mainly

  •   folder structure
  •   Applicaion class+test
  •   Maven POM( optionally gradle)


Dependency Management

in STS

click on File ↣ new ↣ spring starter Project

give project details

Name- microserviceBoot
Type- select maven or gradle
packaging - select jar/war just selected jar
language -java
version-1.8
group/artifact/version/description/description/package i left as it is for default

click Next and finish .

now just have a look what just happened-

↣  Boilerplate structure created
       having mainly

  •   folder structure
  •   Applicaion class+test
  •   Maven POM( optionally gradle)


↣   Dependency Management
if i take a look project then i am having an application class

Application class-

package com.demo;

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

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}


}

Test Class

package com.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

@Test
public void contextLoads() {
}


}

POM file
-- NOitice some things marked as green
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>DemoProject for spring boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

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

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

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


</project>




Spring boot simply manage dependencies and typically says that these are inter dependencies that i wanted to use.

Just we can see these in Spring Dependency Hierarchy::




spring-boot-starter::




in dependency section I added 2 dependecies


1- spring-boot-starter
2- spring-boot-starter-test

now magically it resolve all transitive dependencies

spring boot starter defined no of transitive dependencies-




and each of these dependencies has other transitive dependencies.




Running spring Boot application what just happened -

➤Spring Application - there is one liner code executes

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}

➥ created Spring Application Context
       @SpringBootApplication
Combination of
1 ➥   @Configuration

  • marks a configuration file
  • java bean files
2 ➥@ComponentScan
     looks for components 
3. ➥@EnableAutoConfiguration
  • Master run time switch for Spring Boot
  • Examin ApplicationContext & classpath
  • creates missing beans based on intelligent defaults

Adding web capabilities-
 -adding spring-boot-started-web dependency
 -Adding Controller class


package com.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class InitialController {

@RequestMapping
public @ResponseBody String helloWorld() {
 
return "Hello World";
}
 
}

even i am running as a java application it has just started as a web-application.


if you will run below URL


it will open in browser and print Hello World

Adding web what just happened?

spring boot started web dependencies
  • it adds spring web, spring-mvc jars
  • adds embedded tomcat jars
when application starts
1. your beans are created
2. @EnableAutoConfiguration looks for 'missing' beans
  • based on your beans + classpath
  • Notices @Controller / Spring MVC jars
3- Automatically crates MVC beans
  • Dispatcher Servlet
  • Handler Mappings
  • Adapters
  • View Resolver
4- Launches embedded tomcat instance

Convert JAR to WAR
change POM packaging
Extend springbootservletinitializer 



package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer{
      //run as a jar
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
    
       //run as a war
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}
}



-Deploy to app Server

http://localhost:8080/<app>/

Spring boot automatically establish defaults

  • InternalResourceViewResolver for JSPs
  • ThymeleafVewResolver
  • if Thymeleaf is on the class path


dependency- 

  • spring-boot-starter-thymeleaf
spring boot view options
JSP
Freemaker
Velocity
Thymeleaf

pom.xml

              <dependency>

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

Thymeleaf web pages

spring-boot-starter-thymeleaf
/template folder
controller class changes
web pages

spring boot looks for template folder in resources folder

crate Hello.html in template folder

Hello.html

<html>
<body>
<p>Hello <span th:text="${name}">name-apper here</span> from thymeleaf page.</p>
</body>
</html>

Changes in Controller

package com.demo;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class InitialController {

@RequestMapping("/hi/{name}")
public @ResponseBody String helloWorld(Map model,@PathVariable String name) {
model.put("name", name);
return "Hello World";
}
 

}

run on 


What if i wan to use JSP

i have folder src➥main ➥webapp
create WEB-INF folder
create new foldr views
src➥main➥web➥views
create Hello.jsp here


   pom.xml
           <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

now do entry for prefix and suffix

application.properties


spring.mvc.view.prefix=/WEB-INF/views
spring.mvc.view.suffix=.jsp


Spring and Rest


No comments:

Post a Comment

Note: only a member of this blog may post a comment.