Spring Interview Questions

Explain the Regular expression pointcuts?
The RegexpMethodPointcut lets you leverage the power of regular expressions to define your pointcuts. It uses Perl-style regular expressions to define the pattern that should match your intended methods.
Common regular expression symbol used are (.) matches any single character, (+) Matches the preceding character one or more times, (*) matches the preceding character zero or more, (\) Escapes any regular expression symbol.
For Example:
If we want to match all the setXxx methods, we need to use the pattern .*set.* (the first wildcard will match any preceding class name.

What is Metadata autoproxying?

Spring also supprts autoproxying driven by metadata. Metadata autoproxy configuration is determined by source level attributes and keeps AOP metadata with the source code that is being advised. This lets your code and configuration metadata in one place.
The most common use of metadata autoproxying is for declarative transaction support.

Explain BeanNameAutoProxyCreator and DefaultAdvisorAutoProxyCreator classes?

BeanNameAutoProxyCreator generates proxies for beans that match a set of names. This name matching is similar to the NameMethodMatcherPointcut which allows for wildcard matching on both ends of the name. This is used to apply an aspect or a group of aspects uniformly across a set of beans that follow a similar naming conventions.
The DefaultAdvisorAutoProxyCreator is the more powerful autoproxy creator. Use this class to include it as a bean in your BeanFactory configuration. It implemnts the BeanPostProcessor interface. DefaultAdvisorAutoProxyCreator only works with advisors.

What is Autoproxying in Spring?

Spring has a facility of autoproxy that enables the container to generate proxies for us. We create autoproxy creator beans. Spring provides two classes to support this:
* BeanNameAutoProxyCreator
* DefaultAdvisorAutoProxyCreator

What is ProxyFactoryBean? Describe its properties?

ProxyFactoryBean creates proxied objects. Like other JavaBeans, it has properties that control its behaviour. ProxyFactoryBean properties are:
* target : The target bean of the proxy.
* proxyInterface : A list of interfaces that should be implemented by the proxy.
* interceptorNames : The bean names of the advice to be applied to the target.
* singleton : Whether the factory should return the same instance of the proxy for each getBean invocation.
* aopProxyFactory : The implemetation of the ProxyFactoryBean interface to be used.
* exposeProxy : Whether the target class should have access to the current proxy. This is done by calling AopContext.getCurrentProxy.
* frozen : Whether changes can be made to the proxy’s advice once the factory is created.
* optimize : Whether to aggressively optimize generated proxies.
* proxyTargetClass : Whether to proxy the target class, rather than implementing an interface.

Explain the Around advice?

The MethodInterceptor provides the ability to do both before advice and after advice in one advice object:
public interface MethodInterceptor{
Object invoke(MethodInvocation invocation) throws Throwable;
}
There are two important difference between MethodInterceptor and the both before and after advice. First, the method interceptor implementation controls the target method invocation, and this invocation done by calling MethodInvocation.proceed(). Second, MethodInterceptor gives you control over what object is returned.

Explain the After returning advice?

We want to make sure that we thank our patrons after they make their purchase. To do this, we implement AfterReturningAdvice:

public interface AfterReturningAdvice{
void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable
}
}

Like MethodBeforeAdvice, this advice gives you access to the method that was called, the arguments that were passed, and the target object.

Explian the Before advice?

Before the purchasing of our customer, we want to give them a warm greeting. For this, we need to add functionality before the method buySquishee() is executed. To accomplish this, we extend the MethodBeforeAdvice interface:
public interface MethodBeforeAdvice{
void before(Method m, Object[] args, Object target) throws Throwable
}
This interface provides access to the target method, the arguments passed to this method, and the target object of the method invocation.

Explain the interfaces that are used to create an Advice in Spring framework?

The interfaces that are used to create an Advice are:
* org.aopalliance.intercept.MethodInterceptor
* org.springframework.aop.BeforeAdvice
* org.springframework.aop.AfterReturningAdvice
* org.springframework.aop.ThrowsAdvice