spring---aop(通过自定义类的切面来实现)


通过自定义类,切面来实现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"?>
"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">


    "userService" class="com.kuang.service.UserServiceImpl">
    "log" class="com.kuang.log.Log">
    "afterLog" class="com.kuang.log.AfterLog">



    "diyPoint" class="com.kuang.diy.DiyPoint">
    

        ref="diyPoint">

            "point" expression="execution
        (* com.kuang.service.UserServiceImpl.*(..))"/>

            "before" pointcut-ref="point"/>
            "after" pointcut-ref="point"/>