SpringAOP简单实例(基于注解)

首先,在ApplicationContext.xml中配置如下:



    

    
    

    


    

    


 然后编写AOP切面类

        SpringAOP简单实例(基于注解)_第1张图片

package com.day10_24.AOPs;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class Aop {

     
    @Pointcut("execution(* com.day10_24.AOPs.IUserDaoImpl.sys())")
    public void testPoint(){
    }

    @Before("testPoint()")
    public void testBefore()
    {
        System.out.println("testPoint() Before Success!");
    }

    @After("testPoint()")
    public void testAfter()
    {
        System.out.println("testPoint() After Success!");
    }

}

 接下来编写目标对象方法

package com.day10_24.AOPs;

public interface IUserDao {
    public void sys();

}

 

package com.day10_24.AOPs;

import org.springframework.stereotype.Component;

@Component("iUserDaoImpl")
public class IUserDaoImpl implements IUserDao {
    @Override
    public void sys() {
        System.out.println("hello World!");
    }
}

 编写测试类

public class Test3 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext=
                new ClassPathXmlApplicationContext("applicationContext.xml");
        IUserDao userDao=applicationContext.getBean("iUserDaoImpl", IUserDaoImpl.class);
        userDao.sys();
    }
}

 打印结果:

             SpringAOP简单实例(基于注解)_第2张图片

 

 

你可能感兴趣的:(Spring)