SpringAOP快速使用
来源:互联网 发布:php微信创建菜单 编辑:程序博客网 时间:2024/05/16 04:47
1.项目结构:
2.代码:
1.PersonService.java
package com.aop.service;public interface PersonService {public void save(String name);public void update(String name,Integer id);public String getPersonName(Integer id);}
2.PersonServiceImpl.java
package com.aop.service.impl;import com.aop.service.PersonService;public class PersonServiceImpl implements PersonService{@Overridepublic void save(String name) {System.out.println("save---------");}@Overridepublic void update(String name, Integer id) {System.out.println("update----------");}@Overridepublic String getPersonName(Integer id) {System.out.println("getName");return "name------------";}}
3.拦截方法一:MyInterceptor.java
package com.aop.aop;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 MyInterceptor {//切入点声明@Pointcut("execution(* com.aop.service.impl.PersonServiceImpl.*(..))")public void anyMethod(){}//切入点方法,切入参数为string的方法 @Before("anyMethod() && args(name)")public void doAccessCheck(String name){System.out.println("before--------------" +name);} //获取返回值为string的方法返回值 @AfterReturning(pointcut="anyMethod()", returning="result") public void doAfterReturn(String result){System.out.println("doAfterReturn--------------"+result);} @After("anyMethod()") public void doAfter(){ System.out.println("doAfter---------------"); } @AfterThrowing("anyMethod()") public void doThrowing(){ System.out.println("doThrow---------------"); } //环绕通知 @Around("anyMethod()") public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { /** * 环绕通知内部一定要确保执行该方法,如果不执行该方法,业务bean中被拦截的方法就不会被执行。 * 当执行该方法,如果后面还有切面的话,它的执行顺序应该是这样的:先执行后面的切面,如果后面没有切面了, * 再执行最终的目标对象的业务方法。若不执行该方法,则后面的切面,业务bean的方法都不会被执行。 */ // if () { // 判断用户是否有权限, System.out.println("进入方法"); //执行后面的切面 Object result = pjp.proceed(); System.out.println("退出方法"); // } return result; } }
4.拦截方法一beans.xml配置:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 声明代理aop --><aop:aspectj-autoproxy/><!-- 切入类 --><bean id="myInterceptor" class="com.aop.aop.MyInterceptor"></bean><!-- 服务类 --><bean id = "personService" class="com.aop.service.impl.PersonServiceImpl"></bean></beans>
5.SpringAOPTest.java
package com.junit.test;import static org.junit.Assert.*;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.aop.service.PersonService;public class SpringAOPTest {@Testpublic void test() {ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");PersonService ps = (PersonService) cxt.getBean("personService");ps.save("aaa");;}}
结果:
6.拦截方法二:MyInterceptorXML.java
package com.aop.aop;import org.aspectj.lang.ProceedingJoinPoint;public class MyInterceptorXML { public void doAccessCheck() { System.out.println("前置通知111111"); } public void doAfterReturning() { System.out.println("后置通知11111"); } public void doAfter() { System.out.println("最终通知111111"); } public void doAfterThrowing() { System.out.println("异常通知11111"); } public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { System.out.println("进入方法111111"); Object result = pjp.proceed(); System.out.println("退出方法111111"); return result; }}
7.拦截方法二:beans.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 服务类 --><bean id = "personService" class="com.aop.service.impl.PersonServiceImpl"></bean><bean id="aspectbean" class="com.aop.aop.MyInterceptorXML"></bean><aop:config><aop:aspect id="asp" ref="aspectbean"><aop:pointcut expression="execution(* com.aop.service.impl.PersonServiceImpl.*(..))" id="mycut"/><aop:after method="doAccessCheck" pointcut-ref="mycut"/><aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/> <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/> <aop:after pointcut-ref="mycut" method="doAfter"/> <aop:around pointcut-ref="mycut" method="doBasicProfiling"/></aop:aspect></aop:config></beans>
package com.aop.aop;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 MyInterceptor {//切入点声明@Pointcut("execution(* com.aop.service.impl.PersonServiceImpl.*(..))")public void anyMethod(){}//切入点方法,切入参数为string的方法 @Before("anyMethod() && args(name)")public void doAccessCheck(String name){System.out.println("before--------------" +name);} //获取返回值为string的方法返回值 @AfterReturning(pointcut="anyMethod()", returning="result") public void doAfterReturn(String result){System.out.println("doAfterReturn--------------"+result);} @After("anyMethod()") public void doAfter(){ System.out.println("doAfter---------------"); } @AfterThrowing("anyMethod()") public void doThrowing(){ System.out.println("doThrow---------------"); } //环绕通知 @Around("anyMethod()") public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { /** * 环绕通知内部一定要确保执行该方法,如果不执行该方法,业务bean中被拦截的方法就不会被执行。 * 当执行该方法,如果后面还有切面的话,它的执行顺序应该是这样的:先执行后面的切面,如果后面没有切面了, * 再执行最终的目标对象的业务方法。若不执行该方法,则后面的切面,业务bean的方法都不会被执行。 */ // if () { // 判断用户是否有权限, System.out.println("进入方法"); //执行后面的切面 Object result = pjp.proceed(); System.out.println("退出方法"); // } return result; } }
4.拦截方法一beans.xml配置:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 声明代理aop --><aop:aspectj-autoproxy/><!-- 切入类 --><bean id="myInterceptor" class="com.aop.aop.MyInterceptor"></bean><!-- 服务类 --><bean id = "personService" class="com.aop.service.impl.PersonServiceImpl"></bean></beans>
5.SpringAOPTest.java
package com.junit.test;import static org.junit.Assert.*;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.aop.service.PersonService;public class SpringAOPTest {@Testpublic void test() {ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");PersonService ps = (PersonService) cxt.getBean("personService");ps.save("aaa");;}}
结果:
package com.junit.test;import static org.junit.Assert.*;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.aop.service.PersonService;public class SpringAOPTest {@Testpublic void test() {ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");PersonService ps = (PersonService) cxt.getBean("personService");ps.save("aaa");;}}结果:
6.拦截方法二:MyInterceptorXML.java
package com.aop.aop;import org.aspectj.lang.ProceedingJoinPoint;public class MyInterceptorXML { public void doAccessCheck() { System.out.println("前置通知111111"); } public void doAfterReturning() { System.out.println("后置通知11111"); } public void doAfter() { System.out.println("最终通知111111"); } public void doAfterThrowing() { System.out.println("异常通知11111"); } public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { System.out.println("进入方法111111"); Object result = pjp.proceed(); System.out.println("退出方法111111"); return result; }}
7.拦截方法二:beans.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 服务类 --><bean id = "personService" class="com.aop.service.impl.PersonServiceImpl"></bean><bean id="aspectbean" class="com.aop.aop.MyInterceptorXML"></bean><aop:config><aop:aspect id="asp" ref="aspectbean"><aop:pointcut expression="execution(* com.aop.service.impl.PersonServiceImpl.*(..))" id="mycut"/><aop:after method="doAccessCheck" pointcut-ref="mycut"/><aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/> <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/> <aop:after pointcut-ref="mycut" method="doAfter"/> <aop:around pointcut-ref="mycut" method="doBasicProfiling"/></aop:aspect></aop:config></beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 服务类 --><bean id = "personService" class="com.aop.service.impl.PersonServiceImpl"></bean><bean id="aspectbean" class="com.aop.aop.MyInterceptorXML"></bean><aop:config><aop:aspect id="asp" ref="aspectbean"><aop:pointcut expression="execution(* com.aop.service.impl.PersonServiceImpl.*(..))" id="mycut"/><aop:after method="doAccessCheck" pointcut-ref="mycut"/><aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/> <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/> <aop:after pointcut-ref="mycut" method="doAfter"/> <aop:around pointcut-ref="mycut" method="doBasicProfiling"/></aop:aspect></aop:config></beans>
结果:
3.总结:
方式一:1.切面类去注解配置:切面,切入点,切入方法(各种选择,获取值)
2.beans配置:注解驱动,切面类bean
方式二:1.普通aop类
2.beans配置:普通aop类bean,aop-config配置。
0 0
- SpringAOP快速使用
- SpringAop的使用
- 使用SpringAOP 处理异常
- springAOP简单使用
- SpringAOP概念及其使用
- SpringAOP使用注意事项
- SpringAOP
- SpringAOP
- SpringAop
- SpringAOP
- springAOP
- springAOP
- SpringAOP
- SpringAop
- springAOP
- springAOP
- SpringAop
- SpringAOP
- 一起学WEB(三) CSS到底是干啥的
- (c++)输入三个字符串,按由小到大的顺序输出
- Socket的监听的三种方式
- javascript
- 支付宝 Android 版使用的开源组件
- SpringAOP快速使用
- hrbust 哈理工 1013
- PHP中@的用法
- 【Dongle】【Java】规范之EJB
- jstl表达式分页 Mvc后台
- Mysql: Lock wait timeout exceeded; try restarting transaction异常处理
- work record 1
- 算法提高 夺宝奇兵
- Linux Shell的 & 、&& 、 ||