AOP的两种实现(注解&XML)(二)

AOP的两种实现方式:

<1>.注解方式:简化配置文件,加如注解扫描即可。。   

  1.注入maven依赖包:

  

  2.定义一个StuServiceIml类,有切入点(被增强的方法);

package com.spring.service;

import org.springframework.stereotype.Repository;

@Repository("stuServiceIml")
public class StuServiceIml implements StuService {
    
    public void aopTest()
    {
        System.out.println("我是内部test方法");
    }
}

  3.定义一个切面类,里面有功能性代码(增强的方法),将功能加入到切入点---切面方法;

@Component
@Aspect
public class AopTest1 {

    @Pointcut("execution(* com.spring.service.*.aop*(..))")
    public void pointCut() {}

    //在切入点之前执行
    @Before("pointCut()")
    public void before()
    {
        System.out.println("我是切入方法前");
    }

    @Around("pointCut()")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {

        //Method[] methods=joinPoint.getTarget().getClass().getDeclaredMethods();

        //org.aspectj.lang.Signature signature=joinPoint.getSignature();


        System.out.println("我是方法环绕的前切入");
        joinPoint.proceed();//执行拦截的方法(环绕时需要知道方法执行的时间节点) 环绕时会将方法执行的权限给切面 方法返回值
        System.out.println("我是方法环绕的后切入");
    }

    @After("pointCut()")
    public void after()
    {
        System.out.println("我是切入方法后");
    }

}

  4.配置文件:

<?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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"

       xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
       default-lazy-init="false">

    <description>Spring公共配置</description>
    <!-- 开启注解扫描-->
    <context:component-scan base-package="com.spring"></context:component-scan>
    <!-- 开启注解方式-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>

 

  5.测试类:

public class Test {

    public static void main(String[] args) {
        ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");

        StuServiceIml stu= (StuServiceIml) app.getBean("stuServiceIml");
        stu.aopTest();
    }
}

 6.运行结果:

 

 

<2>.xml方式实现: 代码简单,需要在配置文件配置切入点,通知(切面方法)等。

                              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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
    <!-- 声明一个业务类 -->
    <bean id="stuServiceIml" class="com.spring.service.StuServiceIml">
        
    </bean>  
    
     <!-- 声明通知类 -->
    <bean id="aspectTest" class="com.spring.aopTest.AopTest1" />

    <aop:config>
     <aop:aspect ref="aspectTest">
        <aop:pointcut id="pointcut" expression="execution(* com.spring.service.*.aop*(..))"/>
        
        <aop:before method="before" pointcut-ref="pointcut"/> 
        <!--aop:after-returning method="doAfterReturning" pointcut-ref="pointcut" returning="result"/>-->
        <aop:after method="after" pointcut-ref="pointcut" /> 
        <aop:around method="around" pointcut-ref="pointcut"/> 
        <!--aop:after-throwing method="doAfterThrowing" pointcut-ref="pointcut" throwing="ex"/>-->
      </aop:aspect>
   </aop:config>
</beans>

   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值