Saturday 6 April 2019

Top 20 Spring Boot interview Question and answer

Q:What is spring boot?
ans:spring boot is best java framwork for microservices,

Q:-spring boot vs Spring MVC vs Spring - How do they compare.
Ans:
Most important feature of spring framwork.
dependency injection. At the core of all spring module is dependency
injection or IOC inversion of control.

So Spring is dependency injection,
its makes the code lossly coupled.

Spring MVC:-
Spring mvc framwork provide decoupled way of developing web application with
simple concpt like DispatcherServlet,ModelAndView Resolver
it make it easy to develop web application



Spring boot:- The problem with Spring framwork and Spring Mvc is
The amount of configruation that is needed.

Like...With spring spring MVC you need to configure data source , viewResolver webjar and
lot of stuff you need to confugure.



Spring boot says ok i'll look at it what are the jar availble in the class path,
and I will configure every thing imedaitly
thats what spring Boot enables

Spring Boot solve this problem through a combination of Auto Configuration
and starter project, Spring Boot also provide a few non functional feature
it help making the monitoring appliaction very very easy.

Q:What is Auto Configruation?
Ans:-Auto configuration is feature of spring boot where it look at a framworks
on the CLASSPATH and it looks Existing configuration for the application.

its decide what configuration is needed what can be automatically configured.
Spring Boot provide basic configuration needed to configure the application with these framwork this is called Auto configuration./


to make building production ready application faster.


Example:- if ti see JPA jar on CLASSPATH it automatically cofigured entity-manager
it automatically configured data sources

2nd:- if it see spring MVC jar on CLASSPATH
it would automatically configured DispatcherSerlet ,
it will auto matically configyred error-page and error response


Q:- What are Spring Boot Starter Project?
An:-when ever u developed project with spring or spring mvc or Hibernate
you need to add lot of dependency , you need to manage their version n lot of stuff you need to configured

" SO Spring-boot-Starter-web project help you an avoiding  all that"

 if you add simple starter jar dependency in pom.xml  application
like..

<dependency>
     <groupdId>org.springframwork.boot<groupId/>
      <artifactId>spring-boot-starter-web<artifact/>
</dependency>

it will bring lot of jar alog with all spring related jar


Q: What are the other Starter Project Option that Spring Boot Provide?

Ans:Spring boot provide five varetiy of sarter

spring-boot-starter-web-services -- SOAP web Service
spring-boot-starter-web-Web& RestFul application
spring-boot-starter-data-jpa --Spring Data JpA with Hibernate
spring-boot-starter-test --Unit testing and Integration Testing
spring-boot-starter-data-rest -Expose Simple REST Services using Spring Data REST

spring-boot-starter-jdbc - Tradinal JDBC
spring-boot-starter-security - Authentication and Authorization using spring security



Q:- How does spring boot enable creating production reaady application in quick time?

Ans:-spring-boot aims to enable production ready application in quick time
Spring boot provide few non-functional feature out of box,
like caching, logging,monitoring and embebed server.

spring-boot-starter-actuator  - To use advanced feature like monitoring & tracing to your application out of box.
spring-boot-starter-logging
spring-boot-starter-cache - enabling Spring Framork's caching support.


Q:- What is the minimum baseline Java Version for Spring Boot 2 and Spring 5?
Ans:spring boot 2.0 requires java 8
java 7 java 7 are no longer supported.


Q:- What is the easieest approach to create a Spring Boot Project?
Ans:-
Spring Initializer go to http://start.spring.io/
and choose the dependency enter the groupId and ArtifactId
and click Genearte project.

Q:is Spring Initilallzer the only way to create spring boot Project?
Ans:-NO
Spring boot makes it easy to create Spring Boot Project.
But you can setup a maven project and add the right dependencies to start off.

We have two way to create spring boot project
firts one is sart.spring.io
other one- setting up porject manually is used in the section titled
"Basic web application"
like... Go to exlipse, use File-> New Maven project to create a new project
        Add dependencies
        Add the maven plugin
        Add th spring Boot Application class

Q: Why do we need spring-sping-boot-maven-plugin.?
Ans:-spring-boot-maven-plugin provides a few command which enable to package
the code as a jar or run the application

spring-boot-maven:run runs your Spring Boot application
spring-boot-repackage repackage your jar/war to be executable.
spring-boot:start and spring-boot:stop to manage the lifecyc of your Spring
boot application ()i.e.. for integration tests)

spring-boot:build-info generates build information that can be used the Actuator.

Q: How can I enable auto reload of my application with spring Boot?
Ans:-Using Spring Boot Developer Tools dependecy jar.
just add this dependency in pom.xml  and restart the application
spring-boot-devtools 

Q:-What and why Embedded Servers?
Ans:-what you would need to be deploy your pplication
on virtual machine, you need to install...

Step:- install java
step2:- install the web/Application server(Tomcat/weblogic)
step3:-deploy the application war


Embedded server is when our deployable unit contains the binaries for the server(tomcat.jar)


Q:- How can i add custom JS code with Spring boot?
Ans:- create folder called static under resource folder.
you can put your static content in that folder.
path.. resource\static\js\app.js

<script src="/js/app.js"></script?


Q: What is spring data REST?
Ans:-spring data RESt can be use to expose HATEOAS RESTFUL resource
around Spring Data respositorries.

@RepositoryRestResource(collectionSourceRel = "todos", path ="todos")
public interface TodoRepository extends PagingAnd SortingRepository<Todo,Long>{

without writtng a lot of code we can expose RESTFUL API around DATA
repositories.
========================================================================

Q:- What is Spring boot?

Spring Boot is a Spring framework module
 which provides RAD (Rapid Application Development) feature to the Spring framework.
It is highly dependent on the starter templates feature which is very powerful and works flawlessly.

1. What is starter template?
Spring Boot starters are templates that contain a collection of all the relevant transitive dependencies.

For example, If you want to create a Spring WebMVC application then in a traditional setup,
you would have included all required dependencies yourself

With String boot, to create MVC application all you need to import is spring-boot-starter-web dependency.


2. Spring boot autoconfiguration
Autoconfiguration is enabled with @EnableAutoConfiguration annotation.
 Spring boot auto configuration scans the classpath, finds the libraries in the classpath

Spring boot auto-configuration logic is implemented in spring-boot-autoconfigure.jar.



For example, look at auto-configuration for Spring AOP. It does the followings-

Scan classpath to see if EnableAspectJAutoProxy, Aspect, Advice and AnnotatedElement classes are present.
If classes are not present, no autoconfiguration will be made for Spring AOP.
If classes are found then AOP is configured with Java config annotation @EnableAspectJAutoProxy.
It checks for property spring.aop which value can be true or false.
Based on the value of property, proxyTargetClass attribute is set.
AopAutoConfiguration.java
@Configuration
@ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class,
        AnnotatedElement.class })
@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)
public class AopAutoConfiguration
{

    @Configuration
    @EnableAspectJAutoProxy(proxyTargetClass = false)
    @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = false)
    public static class JdkDynamicAutoProxyConfiguration {

    }

    @Configuration
    @EnableAspectJAutoProxy(proxyTargetClass = true)
    @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = true)
    public static class CglibAutoProxyConfiguration {

    }

}



3. Embedded server
Spring boot applications always include tomcat as embedded server dependency.
It means you can run the Spring boot applications from the command prompt without needling complex server infrastructure.

You can exclude tomcat and include any other embedded server if you want.

For example, below configuration exclude tomcat and include jetty as embedded server.

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

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


Q:- To run application which annotation is required?
Ans:-
To run the application, we need to use @SpringBootApplication annotation. Behind the scenes,
that’s equivalent to @Configuration, @EnableAutoConfiguration, and @ComponentScan together.

MyApplication.java
@SpringBootApplication
public class MyApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(Application.class, args);
    }
}

------------------------------------------------
To execute the application, you can run the main() method from IDE such eclipse, or you can build the jar file and execute from command prompt.

Console
$ java -jar spring-boot-demo.jar
------------------------------------------------

5. Advantages of Spring boot
Ans:
1-Spring boot helps in resolving dependency conflict. It identifies required dependencies and import them for you.
2-It has information of compitable version for all dependencies. It minimizes the runtime classloader issues.


3-It helps in avoiding boilerplate code, annotations and XML configurations.
4-It provides embedded HTTP server Tomcat so that you can develop and test quickly.
5-It has excellent integration with IDEs like eclipse and intelliJ idea.



This annotation is a shortcut of applying 3 annotations in one statement –

@SpringBootConfiguration
@SpringBootConfiguration is new annotation in Spring boot 2. Previously, we have been using @Configuration annotation. You can use @Configuration in place of this.
Both are same thing.


@EnableAutoConfiguration
This annotation is used to enable auto-configuration of the Spring Application Context, attempting to guess and configure beans that you are likely to need. Auto-configuration classes are usually applied based on your classpath and what beans you have defined.

@EnableAutoConfiguration(excludeName = {"multipartResolver","mbeanServer"})
Auto-configuration is always applied after user-defined beans have been registered.

@ComponentScan
This annotation provides support parallel with Spring XML’s context:component-scan element.

Either basePackageClasses() or basePackages() may be specified to define specific packages to scan. If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.


Spring-boot-starter-parent Example
Boot, Spring Boot 2

 spring-boot-starter-parent dependency which is used internally by all spring boot dependencies

What is spring-boot-starter-parent dependency?
The spring-boot-starter-parent dependency is the parent POM providing dependency and plugin management for Spring Boot-based applications. It contains the default versions of Java to use, the default versions of dependencies that Spring Boot uses, and the default configuration of the Maven plugins.



No comments:

Post a Comment