Springaop 091027050407 Phpapp02
Springaop 091027050407 Phpapp02
AGENDA
Introduction To AOP
AOP Concepts
○ Aspect
○ Joinpoint
○ Advice
○ Pointcuts
○ Target Object
○ Weaving
concerns :
A concern is a particular issue, concept, or area of
interest for an application: typically, a goal the
application must meet.
Security
Logging
Cross cutting
concerns
Withdraw method Deposit method
•Transaction Management •Transaction Management
• Logging • Logging
•Checking for the Privileged •Checking for the Privileged User
User
•Actual Deposit Logic
•Actual Withdraw Logic comes here
comes here
Adlux Consultancy Services Pvt Ltd
Public class Account
{
public void deposit() {
// Transaction Management
// Logging
// Checking for the Privileged User
// Actual Deposit Logic comes here
}
public void withdraw() {
// Transaction Management
// Logging
// Checking for the Privileged User
// Actual Withdraw Logic comes here
}
}
AOP calls this kind of logic that cross-cuts the existing business
logic as Cross-Cutting Concerns .
concerns
Easier to understand
Efficient to implement
More efficient
Advice
Action taken at a particular joinpoint is called Advice.
PointCut
A declarative condition that identifies for which joinpoint, the
advices should fire.
Aspect:
An aspect is a modularization of a crosscutting concern; the
gathering together of code that might otherwise have been
scattered. It is termed as the combination of the point-cut and the
advice.
Adlux Consultancy Services Pvt Ltd
AOP vs OOP
Throws Advice :
some kind of exception happens during the execution of a
method, then to handle the exception properly.
PointcutAdvisor=Pointcut + Advice
Static Pointcuts
NameMatchMethod Pointcut
Regular Expression Pointcut
NameMatchMethod Pointcut :
Here the name of the methods that are to be given
advices can be directly mentioned in the
Configuration File.
Adlux Consultancy Services Pvt Ltd
Pointcuts
MyInterface.java
public interface MyInterface
{ public void setMessage(String msg);
public void method1();
public void method2();
public void getMethod1();
public void getMethod2();
}
MyClass.java
public class MyClass implements MyInterface
{
private String message;
public void setMessage(String msg)
{
this.message=msg;
}
public void method1(){}
public void method2(){}
public void getMethod1() {}
public void getMethod2(){}
}
Adlux Consultancy Services Pvt Ltd
Pointcuts
ApplicationContext.xml
<beans>
<bean id=“getMethodsAdvisor" class="org.springframework. aop.
support.NameMatchMethodPointcutAdvisor">
<property name="mappedName">
<value>get*</value>
</property>
(OR)
<property name=“mappedNames”>
<list> <value>get*</value>
<value>set*</value>
<value>method1</value>
</list>
</property>
<property name="advice">
<ref bean=“sampleAdvice "/>
</property>
</bean>
<bean id=“proxy“
class=“org.springframework.aop.framework.ProxyFactoryBean“>
<property name="proxyInterfaces“>
<value>MyInterface</value>
</property>
<property name="target">
<ref bean=“myTargetClass "/>
</property>
<property name="interceptorNames">
<value>getMethodsAdvisor </value>
</property>
</bean>
</beans>
Adlux Consultancy Services Pvt Ltd
Pointcuts
<property name=“patterns”>
<list>
<value>m.*1</value>
<value>.*method.*</value>
</list>
</property>
</bean>
<property name=“interceptorNames”>
<value> regExpAdvisor </value>
</property>
</bean>
Symbol Description
Dynamic Pointcuts :
However, there may be some cases where your pointcuts
will need to evaluate runtime attributes.
Spring provides one built-in dynamic pointcut:
ControlFlowPointcut.
This pointcut matches based on information about the
current thread’s call stack.
That is, it can be configured to return true only if a
particular method or class is found in the current thread’s
stack of execution.
Adlux Consultancy Services Pvt Ltd
Pointcuts
Dynamic Pointcut Configuration
<property name=“interceptorNames”>
<value>dynamicAdvisor</value>
</property>
</bean>
Example
<bean id=“myTargetClass” class=“sample.MyClass”>
<property name=“message”>
<value>Welcome to Adlux</value>
</property>
</bean>
· Runtime
1. target
2. proxyInterfaces
3. interceptorNames
<property name="interceptorNames">
<value>sampleAdvice</value>
</property>
</bean>
<bean id=“sampleTarget" class=“MySampleTarget“/>
<bean id=“sampleAdvice" class=“MySampleAdvice“/>
· DefaultAdvisorAutoProxyCreator
· BeanNameAutoProxyCreator
framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>