Spring基础配置(AOP切面编程)

一、AOP切面编程的简要说明

AOP:面向切面编程,相对于OOP面向对象编程
Spring的AOP的存在目的就是为了解耦。AOP可以让一组类共享相同行为。在OOP中只能
通过继承类和实现接口,来使代码的耦合度增强,且类继承只能为单继承,阻碍更多行为添加到一组类 上,AOP弥补了OOP的不足
Spring 支持AspectJ的注解式切面编程
1、使用@Aspect声明是一个切面
2、使用@After、@Before、@Around定义建言(advice),可直接将拦截规则(切点)作为参数
3、其中@After、@Before、@Around参数的拦截规则作为切点(PointCut),为了使切点复用,可使
@PointCut专门定义拦截规则,然后在@After、@Before、@Around的参数中调用。
4、其中符合条件的每一个被拦截处为连接点(JoinPoint)

二、示例说明

package service.aop;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
/*
 * 拦截规则(切点)的注解
 */
public @interface Action {
    String name();
}

//编写使用注解的被拦截类
package service.aop;
import org.springframework.stereotype.Service;
@Service
public class DemoAnnotationService {
    @Action(name="注解拦截式add()")
    public void add(){}
}

//编写使用方法的被拦截类
package service.aop;
import org.springframework.stereotype.Service;
@Service
public class DemoMethodService {
    public void add(){}
}


package service.aop;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
/*
 * AOP:面向切面编程,相对于OOP面向对象编程
 * Spring的AOP的存在目的就是为了解耦。AOP可以让一组类共享相同行为。在OOP中只能
 * 通过继承类和实现接口,来使代码的耦合度增强,且类继承只能为单继承,阻碍更多行为添加到一组类上,AOP弥补了OOP的不足
 * Spring 支持AspectJ的注解式切面编程
 * 1、使用@Aspect声明是一个切面
 * 2、使用@After、@Before、@Around定义建言(advice),可直接将拦截规则(切点)作为参数
 * 3、其中@After、@Before、@Around参数的拦截规则作为切点(PointCut),为了使切点复用,可使
 *    @PointCut专门定义拦截规则,然后在@After、@Before、@Around的参数中调用。
 * 4、其中符合条件的每一个被拦截处为连接点(JoinPoint)
 */
//使用@Aspect声明是一个切面
@Aspect
//通过@Component让此切面成为Spring容器管理的Bean
@Component
public class LogAspect {
    //@PointCut专门定义拦截规则,然后在@After、@Before、@Around的参数中调用。
    @Pointcut("@annotation(service.aop.Action)")
    public void annotationPointCut(){};
    //使用@After、@Before、@Around定义建言(advice),可直接将拦截规则(切点)作为参数
    @After("annotationPointCut()")
    public void after(JoinPoint joinPoint){
        MethodSignature signature=(MethodSignature) joinPoint.getSignature();
        Method method=signature.getMethod();
        Action action=method.getAnnotation(Action.class);
        System.out.println("注解式拦截"+action.name());
    }
    //通过@Before注解声明一个建言,此建言直接使用拦截规则作为参数
    @Before("execution(* service.aop.DemoMethodService.*(..))")
    public void before(JoinPoint joinPoint){
        MethodSignature signature=(MethodSignature) joinPoint.getSignature();
        Method method=signature.getMethod();
        System.out.println("方法式拦截,"+method.getName());

    }
}


package service.aop;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan("service.aop")
//使用@EnableAspectJAutoProxy注解开启Spring对AspectJ代理的支持
@EnableAspectJAutoProxy
public class AopConfig {

}

package service.aop;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(AopConfig.class);
        DemoAnnotationService demoAnnotationService=context.getBean(DemoAnnotationService.class);
        DemoMethodService demoMethodService=context.getBean(DemoMethodService.class);
        demoAnnotationService.add();
        demoMethodService.add();
        context.close();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为你写诗_xue

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值