In order to access method to only specific class then you
need to give package name
@Before(“execution(public
String org.yash.watertechsol.model.circle.getName())”)
Now this aspect only
run for Circle class.
If you want to apply aspect for all the method which starts
with get and irrespective of return type.
To do that we need to use wildcard.
@Before(“execution(public
* get*())”)
@Before(“execution( * get*())”)
Let say if you want to pass argument
@Before(“execution( * get*(**))”) -> only one argument
@Before(“execution( * get*(..))”) -> two dots means you
can have zero or more parameter
@Before(“execution( * get*())”) -> Zero arguments
@Before(“execution( *
org.yash.watertechsol.model.*.get*())”) -> it applies to all classes
available in Model
If you want to run two aspects.
@Before("execution(public * get*())")
public void secondAdvice(){
System.out.println("Second
advice execute");
}
PointCuts: it is a AOP terminology again for all the points
in the execution of your code where you want the advice method to cutin eg:
execution( * get*()) is a point cut before execution of your actual method.
//@Before("execution(public
String getName())")
//@Before("execution(public * get*())")
@Before("allGetters()")
public void
LoggingAdvice(){
System.out.println("Advice
run. Get method called");
}
//@Before("execution(public * get*())")
@Before("allGetters()")
public void secondAdvice(){
System.out.println("Second
advice execute");
}
@Pointcut("execution(*
get*())")
public
void allGetters(){} ->this dummy method
can be used for all the advice.
AopMain.java
package org.yash.watertechsol;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import org.yash.watertechsol.service.ShapeService;
public class AopMain {
public static void main(String
args[]){
ApplicationContext
ctx = new ClassPathXmlApplicationContext("spring.xml");
ShapeService
shapeService = ctx.getBean("shapeService", ShapeService.class);
System.out.println(shapeService.getCircle().getName());
}
}
Circle.java
package org.yash.watertechsol.model;
public class Circle {
private String name;
public void setName(String
name) {
this.name = name;
}
public String
getName() {
return name;
}
}
Triangle.java
package org.yash.watertechsol.model;
public class Triangle {
private String name;
public void setName(String
name) {
this.name = name;
}
public String
getName() {
return name;
}
}
ShapeService.java
package org.yash.watertechsol.service;
import org.yash.watertechsol.model.Circle;
import org.yash.watertechsol.model.Triangle;
public class ShapeService {
private
Triangle triangle;
private
Circle circle;
public
Triangle getTriangle() {
return
triangle;
}
public
void setTriangle(Triangle triangle) {
this.triangle
= triangle;
}
public
Circle getCircle() {
return
circle;
}
public
void setCircle(Circle circle) {
this.circle
= circle;
}
}
LoggingAspect.java
package org.yash.watertechsol.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class LoggingAspect {
//@Before("execution(public
String getName())")
//@Before("execution(public * get*())")
@Before("allGetters()")
public
void LoggingAdvice(){
System.out.println("Advice
run. Get method called");
}
//@Before("execution(public * get*())")
@Before("allGetters()")
public
void secondAdvice(){
System.out.println("Second
advice execute");
}
@Pointcut("execution(*
get*())")
public
void allGetters(){}
}
spring.xml
<?xml
version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<aop:aspectj-autoproxy />
<bean id="triangle"
class="org.yash.watertechsol.model.Triangle">
<property name="name"
value="Triangle Name"></property>
</bean>
<bean id="circle"
class="org.yash.watertechsol.model.Circle">
<property name="name"
value="Circle Name"></property>
</bean>
<bean id="shapeService"
class="org.yash.watertechsol.service.ShapeService"
autowire="byName"/>
<bean id="loggingAspect"
class="org.yash.watertechsol.aspect.LoggingAspect"/>
</beans>
No comments:
Post a Comment