转载自:http://blog.csdn.net/johnstrive/article/details/4766239
<?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:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- <aop:aspectj-autoproxy/> -->不需要这个
<bean id="securityHandler" class="annotationSecurityHandler.SecurityHandler" />
<bean id="securityHandler1" class="annotationSecurityHandler.SecurityHandler1" />
<bean id="userManager" class="managerImpl.UserManagerImpl" />
<!-- 配置aspect -->
<aop:config>
<!-- 定义切面 -->
<aop:aspect id="security" ref="securityHandler1">
<!-- 定义pointcut,并写表达式 -->
<aop:pointcut id="allMethod" expression="execution(* managerImpl.UserManagerImpl.add*(..))|| execution(* managerImpl.UserManagerImpl.del*(..))"/>
<!-- 定义advice,织入pointcut -->
<aop:before method="checkSecurity" pointcut-ref="allMethod"/>
</aop:aspect>
</aop:config>
</beans>
======================SecurityHandler1 .java==============================
package annotationSecurityHandler;
import org.aspectj.lang.JoinPoint;
public class SecurityHandler1 {
private void checkSecurity(JoinPoint j){//取得代理类传递的参数
Object obj[] = j.getArgs();
for(Object o :obj){
System.out.println(o);
}
System.out.println("========checkSecurity=="+j.getSignature().getName());//这个是获得方法名
}
}
===============test.java==================================
public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
UserManager userManager = (UserManager) beanFactory.getBean("userManager");
userManager.addUser("123", "aaa");
userManager.deleteUserById(1);
}