【AOP】三种方式

方式一

1.导入pom中

<dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
    </dependencies>

2.导入aop

快捷键:<aop:config+enter键

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

<bean id="userService" class="com.zy.pojo.service.UserServiceImpl"></bean>
<bean id="beforLogo" class="com.zy.pojo.logo.BeforLogo"></bean>
<bean id="afterLogo"   class="com.zy.pojo.logo.AfterLogo"></bean>


    <aop:config>
        <aop:pointcut id="pointCut" expression="execution(* com.zy.pojo.service.UserServiceImpl.*(..))"/>
        <aop:advisor advice-ref="beforLogo" pointcut-ref="pointCut"></aop:advisor>
        <aop:advisor advice-ref="afterLogo" pointcut-ref="pointCut"></aop:advisor>
    </aop:config>


</beans>

test

public class MyTest {
 @Test
 public void testAop(){
  ApplicationContext context = new ClassPathXmlApplicationContext("aop.xml");
  UserService userService = (UserService) context.getBean("userService");
  userService.add();

 }
}

方式二

1.自定义类

public class DiyPoint {
    public void before(){
        System.out.println("....方法执行前....");
    }

    public void after(){
        System.out.println("....方法执行后....");
    }
}

2.bean

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

<bean id="userService" class="com.zy.pojo.service.UserServiceImpl"></bean>
<bean id="beforLogo" class="com.zy.pojo.logo.BeforLogo"></bean>
<bean id="afterLogo"   class="com.zy.pojo.logo.AfterLogo"></bean>
<!--    方式二:自定义类-->
    <bean id="diy" class="com.zy.pojo.diy.DiyPoint"></bean>
<aop:config>
    <aop:aspect ref="diy">
        <aop:pointcut id="point" expression="execution(* com.zy.pojo.service.UserServiceImpl.*(..))"/>
        <aop:before method="before" pointcut-ref="point"></aop:before>
        <aop:after method="after" pointcut-ref="point"></aop:after>
    </aop:aspect>
</aop:config>



</beans>

方式三

1.自定义类

@Aspect
public class AnnotationPointCut {
    @Before("execution(* com.zy.pojo.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("....方法执行前....");
    }
@After("execution(* com.zy.pojo.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("....方法执行后....");
    }
}

2.beans

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

<bean id="userService" class="com.zy.pojo.service.UserServiceImpl"></bean>
<bean id="beforLogo" class="com.zy.pojo.logo.BeforLogo"></bean>
<bean id="afterLogo"   class="com.zy.pojo.logo.AfterLogo"></bean>

<!--    方式三    -->

    <bean id="annotationPointCut" class="com.zy.pojo.diy.AnnotationPointCut"></bean>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值