Sunday, 12 December 2021

Spring and core java mock interview learnings

   Authority to spring to create objects

  @Component  -- for DAO layer works fine but readability/visibility wise its bad

  @Service --  Service layer

  @Controller -- Business Logic

  @Repository -- we use this annotation to DAO layer -- Benifits this helps to handle persistance level exceptions that spring provides

  DataAccessException is the root  class which spring provides for handling exceptions


@PostConstruct

  Called first during initialisation(Creating data base connections)

  @Predestroy  

  called B4 the container is destroyed and bean is out of container(Close all  the DB connections/clean up work)

  


Rest client

  ---------------------

  Rest template --- Client to consume the data (make a call to get , put,post)

  getForEntity and getForObject -- methods belong to resttemplate

  getForObject -- returns object

  getForEntity         -- returns header,body , what ever we want

Exchange method - same purpose belongs to rest template



While wrting an API , what are the advantages of returning entity??

We can send http status and body and headers


For delete API, what kinld of status code you will return

204 /200


Time out when ever we are going for rest template?

Basically when connecting to server connections are made with threads.

Thread is blocked if the connection is not establised and then another user tries and another thread is blocked .

So just to avoid that time out is needed so that thread is released


Can we have many threads inside the server?

yes


Server time out -- connection not established

Read time out --  connection established but not able to read some data


Versioning in rest

---------------------------

Adding a new functionality to the API and user should use the new one

Can be defined using request parameters and can be mentioned in headers

URi also it can be defined


How authentication in rest web services and how we make rest web servives secure?

Security configuration class which will be extending securityconfigurationAdapter class

Hitting a request goes to authentication filter which will give us authentication object and this object is paased to authentication builder which will provide proper authentication provider which will validate this object.

Some of the authentication providers are DAO Authentication Provider, we can define even our own


Authentication provider internally passes back to builder which will pass to security holder So next time when user logs in object will be checked by the security holder and user will be allowed to sign in



Header we pass the tokens , like jwt tokens which will be used for authentication and server will validate it.These are stateless i.e server wont store any inf and client will alwyas send relavant information via header.

Bearer is written in jwt token??Means syou are the owner .JWT used for authorisation .Once we give uname and pwd we get authorisation token which weill again be sending to the server which will in turn verify the token

  

In basic authentication where we give username and pwd where we can use base64 encoded


Content Negotiation?


What kinld of content API should accept/produce.It can be achieved using MediaType


Cross Cutting Concerns in spring?And do you implement that?

Belongs to spring AOP used for logging purpose .We want to log after the particular method or so..

Monitoring , secutity.after one method we have to perform security then it can be done


In micro services suppose we want to implement logging,tracing??Where will be implementing these?

At gateway level we ill be doing


Custom validators in spring can be created by implementing validator class

There is a method over ride and write the logic


Core Java

------------------

We have define ArrayList as Size 2 and then add 3 elements ..Will third elemt be added?

Yes it will be added as the arraylist dynamicallu grows in size


1 million string objects to array list added with out specifying size and in a for loop you are adding then it takes total og 613 ms to complete else if you specify size then it will run in 193 ms.


Internally it regrows and calls Arrays.Copyof which will create a new Array and then copy all the elements, so suppose if we have lot of elements and if its size is specified then it allocates so much and then no copy of array or new array creation is needed


Fail fast and fail safe collections

----------------------------------------

Iterators 

fail fast used by arraylist/ linked list --During itration if we try to add /modify it gives concurrent modification exception 

Fail safe : Thread safe ..CopyOnWriteArrayList , Concurrent hash map ...Modification during iteration it works perfectly fine


Fail safe is going to consume more memory , modification operations will be perfomred on a copy of collections not on the orinal collections



Arrays.asList will give us an unmodifyable list , which we cannoot alter 



SingleTon Design pattern

--------------------------

Object for the class can be created only one(For the entire run time)

Scope Single ton in spring means one bean per container(one per application context)


Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...