1.项目初始化
1.1 pom.xml
<dependencies>
<!-- spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- spring事务依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!-- mybatis和spring集成依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<!-- 阿里的连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!-- aspectj-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.bundles</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8_2</version>
</dependency>
</dependencies>
1.2UserService对象
package com.nq.service;
import org.springframework.stereotype.Component;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/29 23:04
*/
@Component
public class UserService {
public void test(){
System.out.println("test方法。");
}
public void a(){
System.out.println("a方法。");
}
}
2.cjlib动态代理的使用
2.1所有的方法都是使用动态代理
package com.nq.service;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/29 23:29
*/
public class CglibTest {
public static void main(String[] args) {
UserService target =new UserService();
Enhancer enhancer=new Enhancer();
enhancer.setSuperclass(UserService.class);
enhancer.setCallbacks(new Callback[]{new MethodInterceptor() {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("before...");
//被代理的对象,目标方法
//Object result=methodProxy.invoke(target,objects);
//Object result=method.invoke(target,objects);
Object result=methodProxy.invokeSuper(o,objects);
System.out.println("After...");
return result;
}
}
});
UserService userService = (UserService) enhancer.create();
userService.test();
}
}
2.2不同的方法使用不同的代理
package com.nq.service;
import org.springframework.cglib.proxy.*;
import java.lang.reflect.Method;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/29 23:29
*/
public class CglibTest {
public static void main(String[] args) {
UserService target =new UserService();
Enhancer enhancer=new Enhancer();
enhancer.setSuperclass(UserService.class);
enhancer.setCallbacks(new Callback[]{new MethodInterceptor() {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("before...");
//被代理的对象,目标方法
//Object result=methodProxy.invoke(target,objects);
//Object result=method.invoke(target,objects);
Object result=methodProxy.invokeSuper(o,objects);
System.out.println("After...");
return result;
}
}
, NoOp.INSTANCE});
enhancer.setCallbackFilter(new CallbackFilter() {
@Override
public int accept(Method method) {
if("test".equals(method.getName())){//表示test方法执行第一个代理方法,其他方法执行第二个方法
return 0;
}else{
return 1;
}
}
});
UserService userService = (UserService) enhancer.create();
userService.a();
}
}
3.jdk动态代理的使用
3.1UserInterface创建接口
package com.nq.service;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/29 23:58
*/
public interface UserInterface {
void test();
void a();
}
3.2UserService实现接口
package com.nq.service;
import org.springframework.stereotype.Component;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/29 23:04
*/
@Component
public class UserService implements UserInterface{
public void test(){
System.out.println("test方法。");
}
public void a(){
System.out.println("a方法。");
}
}
3.3JdkTest代理的main方法
package com.nq.service;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/29 23:56
*/
public class JdkTest {
public static void main(String[] args) {
UserInterface target=new UserService();
UserInterface userInterface =(UserInterface) Proxy.newProxyInstance(JdkTest.class.getClassLoader(), new Class[]{UserInterface.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Jdk动态代理被执行了,方法之前");
method.invoke(target,args);
System.out.println("Jdk动态代理被执行了,方法之后");
return null;
}
});
userInterface.test();
}
}
4.spring中动态代理的使用
4.1proxyFactory 动态代理的使用方法
NickelBeforeAdvice.java方法执行之前
package com.nq.advice;
import org.springframework.aop.BeforeAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/30 0:59
*/
public class NickelBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("方法执行前被执行了");
}
}
NickelAfterReturningAdvice.java方法执行之后
package com.nq.advice;
import org.aspectj.lang.annotation.AfterReturning;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/30 0:59
*/
public class NickelAfterReturningAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("方法returning被执行了");
}
}
NickelAroundadvice.java环绕方法
package com.nq.advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/30 0:59
*/
public class NickelAroundadvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("方法执行round之前");
Object proceed = invocation.proceed();
System.out.println("方法执行round之后");
return proceed;
}
}
NickelThrowAdvice.java抛异常方法
package com.nq.advice;
import org.springframework.aop.ThrowsAdvice;
import java.lang.reflect.Method;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/30 1:00
*/
public class NickelThrowAdvice implements ThrowsAdvice {
public void afterThrowing(Method method, Object[] args, Object target, NullPointerException ex) {
System.out.println("方法抛异常执行了");
}
}
UserService.java抛异常方法
package com.nq.service;
import org.springframework.stereotype.Component;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/29 23:04
*/
@Component
public class UserService implements UserInterface{
public void test(){
System.out.println("test方法。");
}
public void a(){
System.out.println("a方法。");
throw new NullPointerException();
}
}
SpringProxtFactoryTest.java测试方法
package com.nq.service;
import com.nq.advice.NickelAfterReturningAdvice;
import com.nq.advice.NickelAroundadvice;
import com.nq.advice.NickelBeforeAdvice;
import com.nq.advice.NickelThrowAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import java.lang.reflect.Method;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/30 0:49
*/
public class SpringProxtFactoryTest {
public static void main(String[] args) {
UserService target=new UserService();
ProxyFactory proxyFactory=new ProxyFactory();
proxyFactory.setTarget(target);
//proxyFactory.setInterfaces(UserInterface.class);这里可以指定为接口类型
proxyFactory.addAdvice(new NickelAroundadvice());
proxyFactory.addAdvice(new NickelBeforeAdvice());
UserService userService = (UserService) proxyFactory.getProxy();
userService.test();
}
}
4.2proxyFactory 对方法名字进行限制
NickelStaticMethodMatherPointcut.java方法匹配的方法
package com.nq.advice;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
import java.lang.reflect.Method;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/30 1:39
*/
public class NickelStaticMethodMatherPointcut extends StaticMethodMatcherPointcut {
@Override
public boolean matches(Method method, Class<?> aClass) {
return method.getName().equals("test");
}
}
NickelPointcutAdvisor.java切面方法
package com.nq.advice;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
import java.lang.reflect.Method;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/30 1:34
*/
public class NickelPointcutAdvisor implements PointcutAdvisor {
@Override
public Pointcut getPointcut() {
return new NickelStaticMethodMatherPointcut();
}
@Override
public Advice getAdvice() {
return new NickelBeforeAdvice();
}
@Override
public boolean isPerInstance() {
return false;
}
}
SpringProxtFactoryTest.java测试的方法
package com.nq.service;
import com.nq.advice.*;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import java.lang.reflect.Method;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/30 0:49
*/
public class SpringProxtFactoryTest {
public static void main(String[] args) {
UserService target=new UserService();
ProxyFactory proxyFactory=new ProxyFactory();
proxyFactory.setTarget(target);
//proxyFactory.setInterfaces(UserInterface.class);这里可以指定为接口类型
//proxyFactory.addAdvice(new NickelAroundadvice());
//proxyFactory.addAdvice(new NickelBeforeAdvice());
proxyFactory.addAdvisor(new NickelPointcutAdvisor());
UserService userService = (UserService) proxyFactory.getProxy();
userService.test();
}
}
说明: proxyFactory.setOptimize(true), proxyFactory.setProxyTargetClass(true)设置这两个值,将会使用cglib动态代理的方式,其他默认形式使用jdk动态代理的方式。proxyFactory.setExposeProxy(true)设置这个方法,查看当前代理对象是否生效。
说明:Object proxy = AopContext.currentProxy();在对应的方法书写这个方法,查看代理的方法。
4.3spring中的动态代理方式
4.3.1spring执行方法时候启动逻辑
UserService.java修改
package com.nq.service;
import org.springframework.stereotype.Component;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/29 23:04
*/
public class UserService{
public void test(){
System.out.println("test方法。");
}
public void a(){
System.out.println("a方法。");
//throw new NullPointerException();
}
}
说明:这里需要去掉Component注解
AppConfig.java配置
package com.nq.service;
import com.nq.advice.NickelAfterReturningAdvice;
import com.nq.advice.NickelBeforeAdvice;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/28 14:59
*/
@ComponentScan("com.nq")
public class AppConfig {
@Bean
public ProxyFactoryBean userService(){
UserService userService=new UserService();
ProxyFactoryBean proxyFactoryBean=new ProxyFactoryBean();
proxyFactoryBean.addAdvice(new NickelAfterReturningAdvice());
proxyFactoryBean.setTarget(userService);
return proxyFactoryBean;
}
}
Test.java配置
package com.nq.service;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/29 23:00
*/
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();
context.register(AppConfig.class);
context.refresh();
UserService userService = (UserService) context.getBean("userService");
userService.test();
}
}
启动Test.java显示结果
4.3.2不去掉注解,spring执行方法的时候执行对应的逻辑
修改NickelAfterReturningAdvice.Java,增加注解
package com.nq.advice;
import org.aspectj.lang.annotation.AfterReturning;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/30 0:59
*/
@Component
public class NickelAfterReturningAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("方法returning被执行了");
}
}
修改UserService.Java,增加注解
package com.nq.service;
import org.springframework.stereotype.Component;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/29 23:04
*/
@Component
public class UserService{
public void test(){
System.out.println("test方法。");
}
public void a(){
System.out.println("a方法。");
//throw new NullPointerException();
}
}
修改AppConfig.Java配置文件
package com.nq.service;
import com.nq.advice.NickelAfterReturningAdvice;
import com.nq.advice.NickelBeforeAdvice;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/28 14:59
*/
@ComponentScan("com.nq")
public class AppConfig {
@Bean
public BeanNameAutoProxyCreator beanNameAutoProxyCreator(){
BeanNameAutoProxyCreator beanNameAutoProxyCreator=new BeanNameAutoProxyCreator();
beanNameAutoProxyCreator.setBeanNames("userSe*");
beanNameAutoProxyCreator.setInterceptorNames("nickelAfterReturningAdvice");
beanNameAutoProxyCreator.setProxyTargetClass(true);
return beanNameAutoProxyCreator;
}
}
Test.Java配置文件
package com.nq.service;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/29 23:00
*/
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();
context.register(AppConfig.class);
context.refresh();
UserService userService = (UserService) context.getBean("userService");
userService.test();
}
}
Test测试结果
4.3.3AppConfig另外实现动态代理
package com.nq.service;
import com.nq.advice.NickelAfterReturningAdvice;
import com.nq.advice.NickelBeforeAdvice;
import org.springframework.aop.framework.DefaultAdvisorChainFactory;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/28 14:59
*/
@ComponentScan("com.nq")
public class AppConfig {
@Bean
public DefaultPointcutAdvisor defaultPointcutAdvisor(){
NameMatchMethodPointcut pointcut=new NameMatchMethodPointcut();
pointcut.addMethodName("test");
DefaultPointcutAdvisor defaultPointcutAdvisor=new DefaultPointcutAdvisor();
defaultPointcutAdvisor.setPointcut(pointcut);
defaultPointcutAdvisor.setAdvice(new NickelAfterReturningAdvice());
return defaultPointcutAdvisor;
}
@Bean
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator=new DefaultAdvisorAutoProxyCreator();
return defaultAdvisorAutoProxyCreator;
}
}
测试结果
其中DefaultAdvisorAutoProxyCreator通过导入的方式
package com.nq.service;
import com.nq.advice.NickelAfterReturningAdvice;
import com.nq.advice.NickelBeforeAdvice;
import org.springframework.aop.framework.DefaultAdvisorChainFactory;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/28 14:59
*/
@ComponentScan("com.nq")
@Import(DefaultAdvisorAutoProxyCreator.class)
public class AppConfig {
@Bean
public DefaultPointcutAdvisor defaultPointcutAdvisor(){
NameMatchMethodPointcut pointcut=new NameMatchMethodPointcut();
pointcut.addMethodName("test");
DefaultPointcutAdvisor defaultPointcutAdvisor=new DefaultPointcutAdvisor();
defaultPointcutAdvisor.setPointcut(pointcut);
defaultPointcutAdvisor.setAdvice(new NickelAfterReturningAdvice());
return defaultPointcutAdvisor;
}
}
4.4spring中Aspect的使用
创建NickelAspect.java
package com.nq.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/30 2:37
*/
@Aspect
@Component
public class NickelAspect {
@Before("execution(public void com.nq.service.UserService.test())")
public void nickelBefore(JoinPoint joinPoint){
System.out.println("NickelAspect执行了");
}
}
修改AppConfig.java扫描类
package com.nq.service;
import com.nq.advice.NickelAfterReturningAdvice;
import com.nq.advice.NickelBeforeAdvice;
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.aop.framework.DefaultAdvisorChainFactory;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/28 14:59
*/
@ComponentScan("com.nq")
@Import(AnnotationAwareAspectJAutoProxyCreator.class)
//@EnableAspectJAutoProxy
public class AppConfig {
}
启动测试类测试
5.TargetSource方法的使用
package com.nq.service;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.ProxyFactory;
/**
* @author Nickel
* @version 1.0
* @date 2023/8/1 0:58
*/
public class TargetSourceTest {
public static void main(String[] args) {
UserService target=new UserService();
ProxyFactory proxyFactory=new ProxyFactory();
proxyFactory.setTargetSource(new TargetSource() {
@Override
public Class<?> getTargetClass() {
return target.getClass();
}
@Override
public boolean isStatic() {
return false;
}
@Override
public Object getTarget() throws Exception {
System.out.println("getTarget被执行了");
return target;
}
@Override
public void releaseTarget(Object o) throws Exception {
}
});
UserService service = (UserService) proxyFactory.getProxy();
service.test();
}
}
说明:可以在getTarget方法里面写一定的逻辑,让getTarget对象被执行。
6.Introduction(给未实现接口的方法赋值接口)
https://www.cnblogs.com/powerwu/articles/5170861.html
AppConfig
package com.nq.service;
import com.nq.advice.NickelAfterReturningAdvice;
import com.nq.advice.NickelBeforeAdvice;
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.aop.framework.DefaultAdvisorChainFactory;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
/**
* @author Nickel
* @version 1.0
* @date 2023/7/28 14:59
*/
@ComponentScan("com.nq")
@EnableAspectJAutoProxy
public class AppConfig {
}
OrderService.java
package com.nq.service;
import org.springframework.stereotype.Component;
/**
* @author Nickel
* @version 1.0
* @date 2023/8/1 1:20
*/
@Component
public class OrderService {
}
OrderInterface.java
package com.nq.interfac;
/**
* @author Nickel
* @version 1.0
* @date 2023/8/1 1:18
*/
public interface OrderInterface {
void test();
}
OrderInterfaceImpl.java
package com.nq.service.impl;
import com.nq.interfac.OrderInterface;
/**
* @author Nickel
* @version 1.0
* @date 2023/8/1 1:19
*/
public class OrderInterfaceImpl implements OrderInterface {
@Override
public void test() {
System.out.println("order 里面Introduction被实现了");
}
}
OrderAspect.java
package com.nq.aspect;
import com.nq.interfac.OrderInterface;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
import org.springframework.stereotype.Component;
/**
* @author Nickel
* @version 1.0
* @date 2023/8/1 1:22
*/
@Component
@Aspect
public class OrderAspect {
@DeclareParents(value = "com.nq.service.OrderService",defaultImpl = com.nq.service.impl.OrderInterfaceImpl.class)
public OrderInterface orderInterface;
}
IntroductionTest.java
package com.nq.service;
import com.nq.interfac.OrderInterface;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Nickel
* @version 1.0
* @date 2023/8/1 1:26
*/
public class IntroductionTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();
context.register(AppConfig.class);
context.refresh();
OrderInterface orderService = (OrderInterface) context.getBean("orderService");
orderService.test();
}
}
说明:我们可以看到,OrderService并为实现OrderInterface接口,但是在spring容器中却可以转化为OrderInterface类型,这就是通过Aspect实现OrderService转化为OrderInterface的方式。
7.advisor方法使用
1.OrderAdvisor
OrderAdvisor.java
package com.nq.advisor;
import com.nq.advice.NickelAfterReturningAdvice;
import com.nq.service.OrderService;
import org.aopalliance.aop.Advice;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.stereotype.Component;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
/**
* @author Nickel
* @version 1.0
* @date 2023/8/1 1:47
*/
@Component
public class OrderAdvisor implements PointcutAdvisor {
///实现切面的方法
@Override
public Advice getAdvice() {
return new NickelAfterReturningAdvice();
}
@Override
public Pointcut getPointcut() {
return new Pointcut() {
@Override
public ClassFilter getClassFilter() {
return new ClassFilter() {
@Override
public boolean matches(Class<?> aClass) {
return aClass.equals(OrderService.class);
}
};
}
@Override
public MethodMatcher getMethodMatcher() {
return new MethodMatcher() {
@Override
public boolean matches(Method method, Class<?> aClass) {
return method.getName().equals("advisorTest");
}
@Override
public boolean isRuntime() {
return false;
}
@Override
public boolean matches(Method method, Class<?> aClass, Object... objects) {
return false;
}
};
}
};
}
@Override
public boolean isPerInstance() {
return false;
}
}
OrderService.java
package com.nq.service;
import org.springframework.stereotype.Component;
/**
* @author Nickel
* @version 1.0
* @date 2023/8/1 1:20
*/
@Component
public class OrderService {
public void advisorTest(){
System.out.println("OrderService对象的advisorTest发现了");
}
}
OrderTest.java文章来源:https://www.toymoban.com/news/detail-619023.html
package com.nq.service;
import com.nq.advice.NickelPointcutAdvisor;
import com.nq.advisor.OrderAdvisor;
import org.springframework.aop.framework.ProxyFactory;
/**
* @author Nickel
* @version 1.0
* @date 2023/8/1 2:08
*/
public class OrderTest {
public static void main(String[] args) {
OrderService target=new OrderService();
ProxyFactory proxyFactory=new ProxyFactory();
proxyFactory.setTarget(target);
proxyFactory.addAdvisor(new OrderAdvisor());
OrderService orderService = (OrderService) proxyFactory.getProxy();
orderService.advisorTest();
}
}
文章来源地址https://www.toymoban.com/news/detail-619023.html
到了这里,关于cglib动态代理、jdk动态代理及spring动态代理使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!