Square CSS

Monday, May 25, 2020

Java 8 : Static Method In Interface

Introduction


Java 8 comes with use of static method in an interface. Prior to Java 8 static method comes only on class level.

Refer The Video For More Detail



Uses of Static Method


We can create a method with static keyword and the method will be referred from the interface.


  public interface InterfaceWithStaticMethod {

public static void getStaticData() {
System.out.println("Data from static method of interface !!!");
}

}






  public class MainClass {

InterfaceWithStaticMethod.getStaticData();

}











The static method can be called in the class just by calling with interface having static method.











Conclusion


The static method can be created inside the interface since Java 8. It can be used by calling the Interface with its static method.


@EnableScheduling : Create Scheduler In Spring

Introduction


We can create scheduler in spring using @EnableScheduling and @Scheduler in Spring based application.


Refer Video Tutorial For Detail :





@EnableScheduling


@EnableScheduling is annotated on class level to configure class for scheduling jobs in Spring.
This annotation is used along with @configuration for configuring scheduler inside the class as method. 


  @Configuration
  @EnableScheduling //to enable scheduling in class
public class MyScheduler{
        //Custom Code
  



@Scheduler


@Scheduler is used on the method level to run method at scheduled time. The annotation takes parameter to configure time for scheduling. This can be done using cron expression or using delay functions provided in spring.



  //annotate with scheduler
  @Scheduled(cron="*/2 * * * * *")//for every 2 seconds  
public void jobForScheduler() {
        //Custom Code to execute every 2 seconds
  


Conclusion


To enable scheduling in Spring we need to annotate class with @EnableScheduling and @configuration. The method to be considered for job to be annotated with @Scheduled

@EnableCaching : Maintain Cache In Spring


Introduction : 

We can enable cache in spring by the use of @EnableCaching and @Cachable.


Refer Video Tutorial For Detail :




@EnableCaching


The annotation @EnableCaching should be used on the class level to configure the class for enabling cache. This annotation should be used to make it configurable for cache maintenance.



  @EnableCaching //provided annotation on class level
  public class CachingDemo{
         //Custom Code
  }


@Cachable

The annotation @Cachable should be used above the methods which needs to maintain cache.
Once the request is complete the response is stored in cache for same request coming in future.
The response is mapped to the key for further requests. 


@Cacheable("userInfo") //provided cacheable annotation along with userInfo as key
public String checkCache() { //Custom Code }


Conclusion


Major annotation to be used for caching in spring is @EnableCaching and @Cachable. The key should be provide where the cached is used so that the cache data should be mapped to the key.

Sunday, May 24, 2020

@Lookup : Annotation In Spring




Introduction :


@Lookup tells spring to return the instance of the return type of method.
In other words whatever be the return type of method, it's instance will be returned.

Use case :


In situations where a prototype bean needs to be injected in a singleton @Autowired won't work.
In such situations @Lookup is helpful.

Example :


Suppose we have a prototype bean shown below.



@Component @Scope("Prototype")
public class CheckPrototype{
//Some Code }
   




If we want above bean in some singleton class then instead of @Autowired the below code will work.



@Lookup//to handle prototype in singleton public CheckPrototype getCheckPrototype() { // return new Instance }
     

Limitations :


1. @Lookup won't work for static, final and private fields
2. @Lookup wont work if the bean class is final.

Tuesday, January 22, 2019

Hibernate Tutorial : Introduction

Hibernate is an open source lightweight, Object Relational Mapping framework to interact Java classes with databases. It implements the specifications of JPA as a persistence layer.

Benefits 


Following are the benefits of using hibernate:

  • It is a lightweight framework which increases the efficiency of a project.
  • It helps in the interaction of java model class with the table of a database.
  • It maintains the first-level cache thus increasing the efficiency.
  • It implements the specifications of JPA thus helps to avoid the use of database-specific language.
  • It helps in writing queries both in native and hibernate query language.

Object Relational Mapping


Object Relational Mapping is a technique through which an object can interact with the database. 
The java code creates an object and the fields of this object get mapped with the column of the table in the database.


The different field of an object will be mapped with the different columns of a table. Internally this process is achieved with the help of JDBC (Java Database Connectivity).

Conclusion


In this tutorial, information is provided about the overview of Hibernate. In the next tutorial, the setup regarding hibernate will be elaborated.



 ☛ Next >> Hibernate Tutorial: Setting Up Environment

                    This series of tutorials is for Hibernate. The tutorial will cover ... 


Some Algorithms

Algorithm: Tower of Hanoi

Tower of Hanoi consists of three towers called as pegs with n number of rings. Rings are of different size.  Conditions to be fulfill...

Popular Posts