自己网站做问卷调查,怎么创建网站与网页,上海网站建设润滋,网站制作 优帮云今天没有偷懒#xff0c;只是忘了Mybatis#xff0c;所以去补课了~
┏━━━━━━━━━━━━━━━┓ NICE PIGGY PIG..
┗━━━━━━━△━━━━━━━┛ ヽ(#xff65;ω#xff65;)#xff89; | / UU
1.Aop实现方式一
1.1、什…今天没有偷懒只是忘了Mybatis所以去补课了~
┏━━━━━━━━━━━━━━━┓ NICE PIGGY PIG..
┗━━━━━━━△━━━━━━━┛ ヽ(´ω) | / UU
1.Aop实现方式一
1.1、什么是AOP 1.2、AOP在Spring的作用
3、使用Spring实现Aop 1.4、案例代码
//1.afterLog
package com.zhang.log;import org.springframework.aop.AfterAdvice;
import org.springframework.aop.AfterReturningAdvice;import java.lang.reflect.Method;public class AfterLog implements AfterReturningAdvice {Overridepublic void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {System.out.println(执行了method.getName()方法结果为returnValue);}
}
//2.Log
package com.zhang.log;import org.springframework.aop.MethodBeforeAdvice;import java.lang.reflect.Method;public class Log implements MethodBeforeAdvice {Overridepublic void before(Method method, Object[] args, Object target) throws Throwable {System.out.println(target.getClass().getName()的method.getName()被执行了);}
}//3.UserService
package com.zhang.service;public interface UserService {void add();void delete();void update();void query();
}//4.UserServicelmpl
package com.zhang.service;public class UserServicelmpl implements UserService {Overridepublic void add() {System.out.println(添加);}Overridepublic void delete() {System.out.println(删除);}Overridepublic void update() {System.out.println(更改);}Overridepublic void query() {System.out.println(查找);}
}//5.Test类
import com.zhang.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(applicationContext.xml);UserService userService context.getBean(UserService, UserService.class);userService.delete();}
}//6.pom.xml!--aop织入--dependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactIdversion1.9.4/version/dependency
//7.application.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd
!--注册bean--bean idlog classcom.zhang.log.Log/beanbean idafterLog classcom.zhang.log.AfterLog/beanbean idUserService classcom.zhang.service.UserServicelmpl/bean!--方式一执行原生Spring API接口--!--配置aop:需要导入aop的约束--aop:config!--切入点expression:表达式。execution(要执行的位置* * * *),括号里的两个点表示可以有任意的参数--aop:pointcut idpointout expressionexecution(* com.zhang.service.UserServicelmpl.*(..))/!--执行环绕增强--aop:advisor advice-reflog pointcut-refpointout/aop:advisor advice-refafterLog pointcut-refpointout//aop:config
/beans
2、Aop实现方式二 相关代码如下 !--方式二自定义--bean iddiy classcom.zhang.diy.Diy/aop:config!--自定义切面 ref表示要引用的类--aop:aspect refdiy!--切入点--aop:pointcut idpoint expressionexecution(* com.zhang.service.UserServicelmpl.*(..))/!--通知也就是方法--aop:before methodbefore pointcut-refpoint/aop:after methodafter pointcut-refpoint//aop:aspect/aop:config3、Aop实现方式三
注解实现IOP 方式三是通过注解来实现的这里多加了个环绕的知识点。 相关代码 package com.zhang.diy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
Aspect
public class AnnotationPoinCut {Before(execution(* com.zhang.service.UserServicelmpl.*(..)))public void before(){System.out.println(前);}After(execution(* com.zhang.service.UserServicelmpl.*(..)))public void after(){System.out.println(后);}Around(execution(* com.zhang.service.UserServicelmpl.*(..)))public void around(ProceedingJoinPoint jp) throws Throwable {System.out.println(环绕前);Object proceed jp.proceed();System.out.println(环绕后);}
} 总结 总言之aop的实现可能理解会有点麻烦但是只要再看一遍视频就好了。其实主要就是一些理念的理解比如说切面切入点这些。很简单的用心就可以了。加油哦