spring—aop(通过自定义类的切面来实现)(Spring — AOP (implemented by customizing the aspect of the class))

通过自定义类,切面来实现aop

要切入的方法类

package com.kuang.diy;
public class DiyPoint {
    public void before(){
        System.out.println("==============再方法之前执行了=============");
    }

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

}

接口

package com.kuang.service;

public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void select();
}

实现类

package com.kuang.service;
public class UserServiceImpl implements UserService {
    public void add() {
        System.out.println("实现了增加方法");
    }
    public void delete() {
        System.out.println("实现了删除方法");
    }
    public void update() {
        System.out.println("实现了修改方法");

    }
    public void select() {
        System.out.println("实现了查询方法");
    }
}

applicationContext.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:context="http://www.springframework.org/schema/context"
       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/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

<!--    注册bean-->
    <bean id="userService" class="com.kuang.service.UserServiceImpl"></bean>
    <bean id="log" class="com.kuang.log.Log"></bean>
    <bean id="afterLog" class="com.kuang.log.AfterLog"></bean>

<!--    配置aop需要导入aop的约束-->
<!--自定义实现aop-->
    <bean id="diyPoint" class="com.kuang.diy.DiyPoint"></bean>
    <aop:config>
<!--        自定义的切面,ref:要引用的类-->
        <aop:aspect ref="diyPoint">
<!--            切入点-->
            <aop:pointcut id="point" expression="execution
        (* com.kuang.service.UserServiceImpl.*(..))"/>
<!--            通知;也就是什么时候切入
                before:什么时候切入
                method:要切入的方法
                pointcut-ref:在那个点切入
-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>
</beans>
————————

AOP is implemented through custom classes and facets

Method class to cut into

package com.kuang.diy;
public class DiyPoint {
    public void before(){
        System.out.println("==============再方法之前执行了=============");
    }

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

}

Interface

package com.kuang.service;

public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void select();
}

Implementation class

package com.kuang.service;
public class UserServiceImpl implements UserService {
    public void add() {
        System.out.println("实现了增加方法");
    }
    public void delete() {
        System.out.println("实现了删除方法");
    }
    public void update() {
        System.out.println("实现了修改方法");

    }
    public void select() {
        System.out.println("实现了查询方法");
    }
}

applicationContext.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:context="http://www.springframework.org/schema/context"
       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/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

<!--    注册bean-->
    <bean id="userService" class="com.kuang.service.UserServiceImpl"></bean>
    <bean id="log" class="com.kuang.log.Log"></bean>
    <bean id="afterLog" class="com.kuang.log.AfterLog"></bean>

<!--    配置aop需要导入aop的约束-->
<!--自定义实现aop-->
    <bean id="diyPoint" class="com.kuang.diy.DiyPoint"></bean>
    <aop:config>
<!--        自定义的切面,ref:要引用的类-->
        <aop:aspect ref="diyPoint">
<!--            切入点-->
            <aop:pointcut id="point" expression="execution
        (* com.kuang.service.UserServiceImpl.*(..))"/>
<!--            通知;也就是什么时候切入
                before:什么时候切入
                method:要切入的方法
                pointcut-ref:在那个点切入
-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>
</beans>