After going through spring core interview questions, I have covered Top Spring AOP Interview Question And Answers which you may expect in your technical interview of Spring topic.
Q1. What are the common services required when you are developing an enterprise application?
Ans: The three commonly services required are as follows:
- Low-level services.
- Middle-level services
- High-level services
Q2. What are the Low-level services?
Ans: Some of the low-level services are IO. Threading, Networking, etc. Which will be supplied by servers freely.
Q3. What are the Middle-level services?
Ans: Some of the middle-level services are a transaction, security, logging, messaging, etc which has to be implemented by you.
Normally, when you implement business operation you need to write the code for business logic and middle-level services.
Q4. What do you understand by Spring AOP?
- AOP stands for Aspect-oriented programming.
- AOP is a new kind of programming technique which is mainly used to separate the commonly required middle-level services logic from the core business logic of the application.
- The transaction, security, logging, Messaging, etc are the middle-level services which are also called as a cross-cutting concern.
Q5. What are the various AOP frameworks available?
- Spring AOP
- JBoss AOP
Q6. What are the Terminology used in Spring?
Ans: The Terminologies are as follows:
Q7. What is Aspect in Spring AOP?
- Commonly required middle-level services which you are implementing for your enterprise application are called as Aspects
- Security, Transactions, Logging, etc are aspects.
Q8. What is Advice in Spring AOP?
- implementation of a middle-level service is called as advice
- implementation of an aspect is called as advice.
- For Example; Advice is class which contains code for aspects like security, Txs, logging, etc.class TxService(…)
Q9. What is Joint Point?
- Joint point is a point in the program execution where you want to apply the advice.
- Joint point is a point in the program execution where you want to run middle-level services code.
txs.begin(); before business operations
txs.deposit(); Business operation
txs.commit(); After business opeartion returns the control successfully
txs.rollback(); After business operation throws exception.
Q10.What are JointPoints supported by Spring AOP?
- MethodBefore Before invoking method
- MethodReturning When a method returns the control successfully
- MethodThrowing When a method throws an Exception
- MethodAfter When a method returns control anyway.
Q11.What is Pointcut in Spring AOP?
- Collection of joinpoint is called as pointCut.
- By default, advice will be applied for all the business operations of all the business services.
- When you want to apply the advice for some specified business operations of specified business services then you must define pointcut with the required Aspect() expression.
Q12. What is Advisor?
Ans: The advisor is a combination of Advice and points cut.
Defining the Advisors: To specify the required Advisors, you nedd to configure the bean in the spring configuration document with default Pointcut Advisor class as follows:
<bean name=”MBA advisor” class=”org.springframework.aop.support.DefaultPointcutAdvisor”> <property name=”advice” ref=”mba”/> <property name=”pointcut” ref=”MyPC1”/> <bean> Now you have to refer advisor in the proxy factory bean instead of advice. <bean id=”baseProxy” class=”org.springframework.aop.framework.proxyFactoryBean” abstract=”true”> <property name=”interceptorName”> <list> <value>MBAdvisor</value> </list> </property> <bean>
- InterceptorNames are a list of advice or advisors.
- When you use a list of advice then advice will be applied to all the methods of all the business services.
- When you use a list of advisors then advice will be applied to only for the methods which are matching with the given pointcut expression.
Q13. What is Target?
Ans: A target is an object of your business service before applying the Advice or advisors.
Q14 What is Proxy?
Ans: A proxy is an Object of your business service before applying the Advice or advisors.
Q15 What is Weaving in Spring AOP?
Ans: Weaving is the process of applying the advice or advice or advisors to the target objects at given pointcuts to get the proxy objects.
Q16 Before writing any code, What are the Spring AOP dependencies you need to import into your project.
Ans: The spring AOP dependencies you need to import in your project are as follows by Maven
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.11</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.11</version> </dependency>
Please feel free to write down the new questions which are not part of this post so I can include them to benefit more audience.