日期:2014-05-17 浏览次数:20913 次
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <aop:aspectj-autoproxy /> <bean id="person" class="quickstart.aspectj.Person" scope="prototype"> <property name="name" value="Lucy" /> <property name="age" value="12" /> </bean> <bean id="organ" class="quickstart.aspectj.Organization" /> </beans>
package quickstart.aspectj; public class Person { private String name; private String age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String login(String loginInfo) { System.out.println("Person login() " + "LoginInfo:" + loginInfo + this); return this.name + " logining..."; } public void loginThrow() throws Exception { throw new Exception(); } }
package quickstart.aspectj; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public class Organization { // 创建切入点 (以下切点注释方法均可以使用) // @Pointcut("within(quickstart.aspectj.Person)") // @Pointcut("execution(* quickstart.aspectj.Person.*(..))") @Pointcut("execution(* quickstart.aspectj.Person.login(..))") public void logPerson() { } // 后通知 (After (finally) advice) 使用切入点 logPer