• spring的面向切面实现的两种方式


    面向切面:主要应用在日志记录方面。实现业务与日志记录分离开发。

    spring面向切面有两种实现方式:1、注解 2、xml配置。

    1、注解实现如下:

    (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:context="http://www.springframework.org/schema/context" 
            xsi:schemaLocation="http://www.springframework.org/schema/beans 
                                http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
                                http://www.springframework.org/schema/aop 
                                http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                                http://www.springframework.org/schema/context 
                                http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
       <context:annotation-config/>
       <aop:aspectj-autoproxy/>   
       <bean id="Psv" class="com.test.spring.AOP.Personservice"/>
       <bean id="testAop" class="com.test.spring.AOP.testAop"/>
       </beans>

    注意:红色部分的配置

    (2)切入实现的代码如下:

    @Aspect
    public class testAop {
    
    //指明所要代理的类或方法
    @Pointcut("execution(* com.test.spring.AOP.Personservice.*(..))")
        public void pointCut(){}
        
        @After("pointCut()")
        public void after(){
            System.out.println("after");
        }
        
        @Before("pointCut()")
        public void before(){
            //JoinPoint joinPoint
            System.out.println("before");
        }
        
        @Around("pointCut()")
        public Object around(ProceedingJoinPoint joinpoint){
    //joinpoint.getArgs()能够得到传入到方法的参数
    Object valu
    = null try { System.out.println("aaaaaaaa"); valu = joinpoint.proceed(); //返回所代理的方法(有返回值的方法)返回值
    System.out.println(valu); }
    catch (Throwable e) { e.printStackTrace(); } return valu; } @AfterReturning("pointCut()") public void afteReturning(){ System.out.println("afteReturning"); } @AfterThrowing("pointCut()") public void afterThrowing(){ System.out.println("afterThrowing"); } }

    2、xml配置实现方式

    <?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:context="http://www.springframework.org/schema/context" 
            xsi:schemaLocation="http://www.springframework.org/schema/beans 
                                http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
                                http://www.springframework.org/schema/aop 
                                http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                                http://www.springframework.org/schema/context 
                                http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
       <context:annotation-config/>
      
       <bean id="Psv" class="com.test.spring.AOP.Personservice"/>
       <bean id="testAop" class="com.test.spring.AOP.testAop"/>
    <aop:config>
            <aop:aspect id="Pservice" ref="testAop">
                <!-- 配置指定切入的对象 -->
                <aop:pointcut id="point_cut" expression="execution(* execution(* frame.com.Action.login.*(..)))" />
                <!-- 只匹配add方法作为切入点
                <aop:pointcut id="except_add" expression="execution(* com.test.spring.AOP.*.addPerson(..))" />
                 -->
                <!-- 前置通知 -->
                <aop:before method="before" pointcut-ref="point_cut" />
                <!-- 后置通知 returning指定返回参数 -->
                <aop:after-returning method="after" pointcut-ref="point_cut" returning="result" />
                <!-- 环绕通知 -->

            <aop:around method="around" pointcut-ref="point_cut"/> <aop:after-throwing method="doThrow" pointcut-ref="point_cut" throwing="e"/> </aop:aspect> </aop:config> </beans>
  • 相关阅读:
    DOCK-SWARM
    springMVC上传和下载文件
    生成任意内容任意类型的文件
    配置监听(系统启动和关闭时运行的程序)
    配置过滤器
    读取文件内容(TXT之类的文件)
    读取properties文件中的内容
    时间戳转时间字符串和时间
    启动Service的时候报错unable to install breakpoint in
    java后台调用接口并获取返回值
  • 原文地址:https://www.cnblogs.com/wbjgogogo/p/5161170.html
Copyright © 2020-2023  润新知