Tuesday, October 29, 2019

Spring Interview Questions - Basic To Advance - Part 4

What is Spring AOP?
Aspect oriented Programming is programming paradigm which is analogous to object oriented programming. Key unit of object oriented programming is class, similarly key unit for AOP is Aspect. Aspect enable modularisation of concerns such as transaction management, it cut across multiple classes and types. It also refers as a crosscutting concerns.


What is Aspect, Advice, Join point and pointcut in Spring AOP?
Aspect: An Aspect is a class that implements concerns that cut across different classes such as logging. It is just a name.

Joint Point : It is a point in execution of program such as execution of method. In Spring AOP, a join point always represents a method execution.

Advice : Action taken by  aspect at particular join point. For example: Before execution of getEmployeeName() method, put logging. So here, we are using before advice.

Pointcut : Pointcut is an expression that decides execution of advice at matched joint point. Spring uses the AspectJ pointcut expression language by default.

What is @Qualifier annotation in Spring?

You can have more than one bean of same type in your XML configuration but you want to autowire only one of them ,so @Qualifier removes confusion created by @Autowired by declaring exactly which bean is to autowired.

What is @Required annotation in Spring?
This annotation simply indicates that the affected bean property must be populated at configuration time: either through an explicit property value in a bean definition or through autowiring. The container will throw an exception if the affected bean property has not been populated; this allows for eager and explicit failure, avoiding NullPointerExceptions or the like later on.

Suppose you have very large application and you get NullPointerExceptions because required dependency has not been injected then it is very hard to find out what goes wrong.So this annotation helps us in debugging the code.

Circular dependencies in Spring
For example: Class A requires an instance of Class B and Class B requires an instance of Class A.
As you can see, we are getting BeanCurrentlyInCreationException.Requested bean is currently in creation: Is there an unresolvable circular reference?

Solution

Setter injection
You use @Lazy with @Autowired to solve circular dependencies in Spring.
public class A {
 B b;
 @Autowired
 public A(@Lazy B b) {
  super();
  this.b = b;
 }
}

You use @PostConstruct with @Autowired to solve circular dependencies in Spring.
@PostConstruct
 public void init()
 {
  b.setA(this);
 }

What happens when you inject prototype bean into singleton bean? Can you explain the behavior?
When you inject prototype bean to singleton bean, prototype bean still behave like a singleton bean.

Solution

There are many ways to solve this problem.

Using Method injection
@Lookup

 public Y getY() {
  return y;
 }

You can use method injection to solve this problem.Simply use @Lookup with getY() method, it will return new instance each time.

Spring will use CGLIB to generate dynamically a subclass that overrides the method getY(). It then registers the bean into the application context. Whenever we request for getY() method, it will return new instance of Class Y.
There are two options here, either create dummy method or mark method as abstract. When you mark method as abstract, then you will have to declare class abstract as well, hence we have used dummy method in this scenario.

Using ObjectFactory

You can use ObjectFactory to get new object each time getY() method is called.
@Autowired
 private ObjectFactory<Y> prototypeBeanObjectFactory;
 Y y;
 public Y getY() {
  return prototypeBeanObjectFactory.getObject();
 }

Using ApplicationContextAware
You can return new bean using application context wheneven getY() method is getting called.
public class X implements ApplicationContextAware{
 private ApplicationContext applicationContext;
 Y y;
 public Y getY() {
  return applicationContext.getBean(Y.class);
 }
 public void setY(Y y) {
  this.y = y;
 }
 @Override
 public void setApplicationContext(ApplicationContext arg0) throws BeansException {
  this.applicationContext=arg0;
 }
}
Link : https://java2blog.com/spring-interview-questions-and-answers/

ContextLoaderListener vs DispatcherServlet vs Application Context vs Web Application Context - Decrypting the myth
https://siddharthnawani.blogspot.com/2019/10/contextloaderlistener-vs.html

Scope of a Spring-Controller and its instance-variables
https://stackoverflow.com/questions/11139571/scope-of-a-spring-controller-and-its-instance-variables

https://www.stacktips.com/tutorials/spring/how-spring-controller-request-mapping-works-in-spring-mvc


@Named annotation in Spring MVC
@Named works the same as @Component. However, the annotations @Controller, @Service, and @Repository are more specific.

From the Spring docs:

@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.

For example, these stereotype annotations make ideal targets for pointcuts. It is also possible that @Repository, @Service, and @Controller may carry additional semantics in future releases of the Spring Framework. 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.

This section explains the difference with @Named.

Many components, like Spring's DispatcherServlet (MVC configuration in WebApplicationContext) aren't looking for Component, they are looking for @Controller. So when it scans your class, it won't find it in @Named. In a similar fashion, transaction management with @Transactional looks for @Service and @Repository, not for the more generic @Component.

No comments:

Post a Comment