简而言之:
a)spring.xml
<aop:aspectj-autoproxy />
<bean id="circle" class="org.tutorial.spring.model.Circle">
<property name="name" value="Circle name" />
</bean>
<bean id="triangle" class="org.tutorial.spring.model.Triangle">
<property name="name" value="Triangle name" />
</bean>
<bean id="shapeService" class="org.tutorial.spring.service.ShapeService" autowire="byName" />
<bean class=" org.tutorial.spring.aspect.LoggingAspect" />
b)ShapeService类
package org.tutorial.spring.service;
import org.tutorial.spring.model.Circle;
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;
}
}
c)圆圈类
package org.tutorial.spring.model;
public class Circle {
private String name;
public String getName() {
System.out.println("Circle getName");
return name;
}
public void setName(String name) {
System.out.println("Circle setName");
this.name = name;
}
}
d)LoggingAspect类
@Aspect
public class LoggingAspect {
@Before("allCircleMethods()")
public void securityAdvice() {
System.out.println("Security Advice is executed!");
}
@Pointcut("within(org.tutorial.spring.model.Circle)")
public void allCircleMethods() {
}
}
e)ShapeAOP类(要运行的主类)
public class ShapeAOP {
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 setName
执行安全建议!
圆圈getName
圈子名称
注意在Circle bean实例化过程中'Circle setName'之前没有'执行安全建议'的输出。
为什么Circle类中的切入点没有应用于Circle setName方法?
答案 0 :(得分:0)
用户pap在评论部分回答了我的问题