Aspect-Oriented Software Development (AOSD) Tutorial #6

4 downloads 3029 Views 284KB Size Report
AOP Concepts. • Aspect: modularization of crosscutting concern. – xml: schema- based approach. – Annotations: @AspectJ approach. • Join point: in Spring AOP  ...
Aspect-Oriented Software Development (AOSD) Tutorial #6 Spring AOP

Aspect-Oriented Software Development (236606)

1

AOP Concepts • Aspect: modularization of crosscutting concern – xml: schema-based approach – Annotations: @AspectJ approach

• Join point: in Spring AOP, always a method execution • Advice, pointcut: as in AspectJ • Introduction (Intertype declarations) Aspect-Oriented Software Development (236606)

2

AOP Concepts • Target object: object being advised • AOP proxy: an object created by the AOP framework • Weaving: linking aspects with types or objects to create an advised object (runtime weaving in Spring AOP)

Aspect-Oriented Software Development (236606)

3

Supported Pointcut Designators • execution • within • this: proxy object • target: original bean object • args • @target(executing object has annotation of type) • @args(arg has annotation of type) • @within(type has annotation of type) • @annotation(method has annotation of type) Spring: • bean(idOrNameOfBean) : particular spring bean (or set) Aspect-Oriented Software Development (236606)

4

Pointcut Designators Examples: target(com.xyz.service.AccountService) args(java.io.Serializable) @within(org.springframework.annotation.Trans actional) @args(com.xyz.security.Classified) bean(*Service) Aspect-Oriented Software Development (236606)

5

Example (@AspectJ style) • Do the necessary checks when we want to access an object in the DAO layer. @Aspect public class SystemArchitecture { @Pointcut(“execution(com.xyz.someapp.dao.*)”) public void inDataAccessLayer() {} … } Aspect-Oriented Software Development (236606)

6

Example (@AspectJ style) Or @Before(“execution(….)”)

@Aspect public class AfterReturningExample { @Before(“com.xyz.myapp.SystemArchitecture.d ataAccessOperation()”) public void doAccessCheck(); }

Aspect-Oriented Software Development (236606)

7

Example 2 • Do recovery actions when a data access operation throws an exception: @AfterThrowing( pointcut=“com.xyz.myapp.SystemArchitecture.dataAccessOperation()”, throwing=“ex”) public void doRecoveryActions(DataAccessException ex){ //… }

Aspect-Oriented Software Development (236606)

8

Access to the current JoinPoint and passing arguments • Declaring the first parameter of type org.aspectJ.lang.JoinPoint. Except for around advice where the first parameter must be of type ProceedingJoinPoint. Parameter names are not available Example: through Java reflection! @Before( argNames available at runtime. If argNames is not value=“com.xyz.lib.Pointcuts.anyPublicMethod() &&available SpringAOP tries to biend parameters (throws target(bean) && @annotation(auditable)”, exception on ambiguous or illegal argNames=“bean,auditable”) binding) public void audit(JoinPoint jp, Object bean, Auditable auditable){ // use jp, bean, auditable.. } Aspect-Oriented Software Development (236606)

9

Comparison @Aspect public class SystemArchitecture { @Pointcut(“execution(com.xyz.som eapp.dao.*)”) Public void inDataAccessLayer() {} … } @Aspect public class AfterReturningExample { @Before(“com.xyz.myapp.SystemA rchitecture.dataAccessOperatio n()”) Public void doAccessCheck(); }



Aspect-Oriented Software Development (236606)

10

Proxy objects From the outside, the object is accessed through its proxy, thus advices are executed

Examples and graphics from: Chapter 6: Aspect Oriented Programming with Spring. The Spring Framework Reference Documentation

A call to foo() from inside the object does not go through the proxy Aspect-Oriented Software Development (236606)

11