MyAdvice 填充方法(在原有方法上添加方法)

//applicationContext.xml配置文件 

/UserServiceImp继承于UserService接口

<!-- 1 配置目标对象-->
    <bean name="userService" class="cn.jy.service.UserServiceImp"> </bean>
    <!-- 2 配置通知对象-->
    <bean name="myAdvice" class="cn.jy.aop.MyAdvice"></bean>
    <!-- 3 配置将通知织入目标对象-->
    <aop:config>
           <!-- 配置切入点 -->
             <aop:pointcut expression="execution(* cn.jy.service.UserServiceImp.save())" id="jy"/>
             <aop:aspect ref="myAdvice">
                   <aop:before method="before"  pointcut-ref="jy"/>
             </aop:aspect>
    </aop:config>

//UserServiceImp

package cn.jy.service;

public class UserServiceImp implements UserService {

    public void save() {
        System.out.println("baocun");
    }
    public void delete() {
        System.out.println("delete");
    }
}
//UserService接口

package cn.jy.service;

public  interface UserService {
void save();
void delete();
}

//测试类

package cn.jy.aop;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.jy.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
    @Resource(name="userService")
    private UserService u;
    @Test
    public void fun1(){
        System.out.println(u);
        u.save();
    }
}

原文地址:https://www.cnblogs.com/Fisherman13/p/10564851.html