CodeByAkram

What do you mean by Aspect, Join Point, Advice?


What’s the difference between @Component, @Controller, @Repository & @Service annotations in Spring?

What do you mean by Aspect, Join Point, Advice?

Pointcut: Pointcut are expressions that is matched with join points to determine whether advice needs to be executed or not. Pointcut uses different kinds of expressions that are matched with the join points and Spring framework uses the AspectJ pointcut expression language.
AOP Advice Types
Before Advice: These advices run before the execution of join point methods. Use @Before annotation to mark an advice type as Before advice.
After (finally) Advice: An advice that gets executed after the join point method finishes executing, whether normally or by throwing an exception. We can create after advice using @After annotation.
After Returning Advice: Sometimes we want advice methods to execute only if the join point method executes normally. We can use @AfterReturning annotation to mark a method as after returning advice.
After Throwing Advice: This advice gets executed only when join point method throws exception, we can use it to rollback the transaction declaratively. We use @AfterThrowing annotation for this type of advice.
Around Advice: This is the most important and powerful advice. This advice surrounds the join point method and we can also choose whether to execute the join point method or not. We can write advice code that gets executed before and after the execution of the join point method. It is the responsibility of around advice to invoke the join point method and return values if the method is returning something. We use @Around annotation to create around advice methods.

What’s the difference between @Component, @Controller, @Repository & @Service annotations in Spring?


Spring Annotations, Codebyakram


Spring 2.5 introduces further stereotype annotations: @Component@Service, and @Controller@Component is a generic stereotype for any Spring-managed component. @Repository@Service, and @Controller are specializations of @Component for more specific use cases, for example, in the persistence, service, and presentation layers, respectively.

You may also like What do you mean by Aspect, Join Point, Advice?

Therefore, you can annotate your component classes with @Component, but by annotating them with @Repository@Service, or @Controller instead, your classes are more properly suited for processing by tools or associating with aspects. For example, these stereotype annotations make ideal targets for pointcuts.

Thus, if you are choosing between using @Component or @Service for your service layer, @Service is clearly the better choice. Similarly, as stated above, @Repository is already supported as a marker for automatic exception translation in your persistence layer.



Differentiate between constructor injection and setter injection

Partial Dependency

In Setter Injection, partial dependency is possible, means if we have 4 dependencies as mentioned below,

Differentiate between constructor injection and setter injection, codebyakram


Then it is not necessary to inject all values if we are using setter injection.

But in Constructor Injection, partial dependency is not possible because we are calling the constructor of that class.

Overriding

Constructor Injection can not override the setter injected properties but Setter injection can override the constructor injected properties.

Changes

Setter injection makes bean class object as mutable but constructor injection makes bean class object as immutable. 

Number of dependencies

If we have dependencies for example 17 in our bean class then, in this case setter injection is not recommended as we need to write almost 17 setters right, bean length will increase.

In this case, Constructor injection is highly recommended, as we can inject all the dependencies with in 3 to 4 lines by calling one constructor.


Java 8 Lambda Comparator

Lets se how we can sort the object by using java 8 lambda comparator. For this let take a Employee class.

Java 8 Lambda Comparator, codebyakram

public class Employee {
    private String name;
    private int salary;
 
    // standard constructors, getters/setters, equals and hashcode
}

1. Classic sort (without lambda)

Comparator< Employee > bySalary = new Comparator< Employee >() {
  @Override
  public int compare(Employee o1, Employee o2) {
   return o1.getSalary().compareTo(o2.getSalary());
  }
 };
2. Lambda expression equivalent.

 Comparator< Employee > bySalary = 
  (Employee o1, Employee o2)->o1.getSalary().compareTo(o2.getSalary());

You may also like to read What do you mean by Aspect, Join Point, Advice?

How to get keys and values from Map in Java?

As we know, map is based on key-value pair associations, so interviewer can ask you this question if you are a beginner or having less than 3 years of experience.

So lets see how we can the key and values from a Map?

How to get keys and values from Map, codebyakram


In Java we have and Map.Entry method that returns a collection-view of map.


Map< string string=""> map = new HashMap<>();

 // Get keys and values
 for (Map.Entry< string string=""> entry : map.entrySet()) {
  String k = entry.getKey();
  String v = entry.getValue();
  System.out.println("Key: " + k + ", Value: " + v);
 }

 // In Java 8 we can use for each
 map.forEach((k, v) -> {
  System.out.println("Key: " + k + ", Value: " + v);
 });