Tuesday 25 February 2020

6 years exp java interview preparation_Spring_SpringBoot

Spring:



differences between bean factory and applcationContext

BeanFactory doesn't provide support for internationalization i.e. i18n but 
ApplicationContext provides support for it.
  • Bean instantiation/wiring
  • Automatic BeanPostProcessor registration
  • Automatic BeanFactoryPostProcessor registration
  • Convenient MessageSource access (for i18n)
  • ApplicationEvent publication(event propagation)
  • simple integration with Spring's AOP 



AppliactionContextXML

Spring Bean life cycle




Spring Boot
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan
@Configuration


How SpringBoot autoConfiguratoin works?
When we write starter pom, by doing dependency resolution maven will down load all jars, inside jars META-INF/spring.factories  will decide, which should be configured automatically by @configuration  annotation based on @Conditional annotation.
for example: Hibernate jar is there in class path,
then @ConditionalonClass(Hibernate.class) then Hibernate class will automatically configured.


Spring Boot features
xmlconfiguration not required
Webdevelopment,
Standalone applications
Spring Application
Autoconfiguration
Starter POMs
SpringBoot CLI commandLineInterface

Actuator:It provides a lot of insight and metrics about running applications in production
you can find out exactly which beans are configured in the Application context, what are auto-configuration decisions made, what environment variables, system properties, command line arguments are available to an application, and many more.
You can even get a trace of HTTP requests handled by the application, along with various useful application metrics, e.g. CPU and Memory usage, garbage collection details, web requests, and data source usage.
Spring Boot Initializer
It gives project structure

spring.profiles.active=dev
spring.profiles.active=PRD

========================================

Spring vs Spring boot

Spring: Provides dependency injection: loosely couple, and provides 
wide range of many feature(flexibilty) for j2ee applications.

Spring Boot: Aims to shorten the code length and provides you with the easiest way to develop a web application. 
Shortens the time to develop because of  annotations and default code.
it provides standalone application with zero configuration or less configuration.
Autoconfiguratoin is a special feature.

Benifits of Spring boot  over Spring. or Problems of spring 
1.Dependency resolution.--> list out all dependencies  and need to check with proper version.No need to mention vesion. Spring boot
2.Minimum configuration. need to enable datasource, session factory then transaction, need to mention componentscan. in spring .
in Sprin boot: spirng boot internally handles all above datasoruce, sessionfactory.
just need to mention in applicatoin.properteis files as belowlspring
spring.datasource.driver-class=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:9010/tdms
spring.datasource.username=root
spring.datasoruce.password=password

3.Embedded tomcat server.
4.Bean auto scan.If you are giving same package structre then @ComponentScan will scan all beans, other wise we need to write the @ComponentScan (com.package,com.package2)
5.Health metrics.what are the thread, memory, gc logs.
Actuator.

Difference between @Component and @Bean and @Configuration. and @Autowired
Spring boot life cycle.
when start pom added,

it will take the parent version and resolves the dependencies. and versions,
  1. creates application context.
  2. 
    check application type.initializes context and 
  3. 
    Register the annotated bean classes with context.
  4. 
    creates the instance of embedded webserver i.e. tomcat and addes to context.
    enables auto configuratioan and beans ready.
    
    Spring bean life cycle.
https://www.youtube.com/watch?v=J_kTukE7hr8
https://www.youtube.com/watch?v=zp2tqnpZiAU

java
@Component services

========================================
@Component: Marks as bean, component scan machanism adds to context.
@Service: same as Component, it just specifies  intent of class better like busincess logic will write here.
@Repository: dao layer, unchecked exceptions thrown 
@Configuration : class contains one or more beans defined inside it.
@Bean:is used explicitly  declare a single bean, rather than spring doing it for us.
@Controller: marks a class spring mvc web controler
@RestController: @Controller+@ResponseBody, eleminates the need to annotate every request handling method of that call with @ResponseBody annotation.

@Autowired 



Design patterns.

Single ton class:


public class SingletonClass {
    private static SingletonClass SINGLE_INSTANCE = null;
    private SingletonClass() {}
    public static SingletonClass getInstance() {
        if (SINGLE_INSTANCE == null) {
            synchronized (SingletonClass.class) {
                if (SINGLE_INSTANCE == null) {
                    SINGLE_INSTANCE = new SingletonClass();
                }
            }
        }
        return SINGLE_INSTANCE;
    }
}

=============

Types of Design patterns

Creational: when we want to create an object

SingelTon
Factory
Abstract Factory
Prototype
Builder


Structural: compose object, if objet wants to use an other obejct.(insdie an object)
Composite
Proxy
Aadapter
Facade
Bridge


Behaviour: make communitionation to other object.
Observer
Template Method
Visitor
Strategy

Difference between Bean and Component 

No comments:

Post a Comment