您的位置:首页 > 编程语言 > ASP

AspectJ之Advice定义之Before Advice

2016-12-08 09:26 239 查看
1、Before Advice:



示例步骤如下:

1、配置文件如下:

<context:component-scan base-package="com.wuyonghu" />
//这里的aop:aspectj-autoproxy标签必须有,否则无效,相当于是aspectj的自动代理
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>


2、定义切面类:

package com.wuyonghu.aspectj;

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

@Component("aspect")
@Aspect
public class MoocAspect {

@Before("execution(* com.wuyonghu.aspectj.AspectjBiz.*(..))")
public void before(){
System.out.println("切面类中的before方法执行了。。。");
}
}


3、定义业务类:

package com.wuyonghu.aspectj;

import org.springframework.stereotype.Service;

@Service
public class AspectjBiz {

public String save(String arg){
System.out.println("AspectBiz中的save方法执行了");
return "success";
}
}


4、测试类:

package com.wuyonghu.aspectj;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@RunWith(BlockJUnit4ClassRunner.class)
public class AspectjTest {
@Test
public void test1() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
AspectjBiz biz=(AspectjBiz) context.getBean("aspectjBiz");
biz.save("海賊王");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  aop