0

简而言之:

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
安全提示已执行!
Circle getName
圈子名称

请注意,在 Circle bean 实例化期间,在“Circle setName”之前没有“Security Advice is executed”的输出。

为什么 Circle 类中的切入点没有应用于 Circle setName 方法?

4

1 回答 1

0

用户pap在评论区回答了我的问题

于 2013-02-19T19:58:17.043 回答