spring Aop 配置文件方式+JoinPoint获取参数
2015-11-24 20:50
519 查看
转载自: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);
}
<?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);
}
相关文章推荐
- Spring AOP 简单demo
- Spring3.0 AOP 详解
- Spring Aop 注解
- Java集合概述
- java集合类
- 解决spring使用动态代理类型转换失败的问题--java.lang.ClassCastException: com.sun.proxy.$Proxy$ cannot be cast to ...
- JDK的配置及出现问题的解决方案
- 大家来学javaBean
- 核心java系列——线程(二)
- JAVA中的基础语句
- Java基础语法
- Java反射机制简介
- java开发环境搭建
- java回调机制的理解
- java缓冲区
- 使用java实现定时器定时删除系统文件
- Java中equals和==的区别
- Java 十六进制转十进制
- JavaMail1.5.4 源码
- java HashCode()方法理解