Aspectj是一种面向切面的框架,用它可以实现面向切面编程。在eclipse如果要使用Aspectj需要安装ADJT插件,安装方法如下
-
下载安装包,下载地址
下载最新的ADJT,此处以2.2.3.为例
-
在eclipse中安装



之后稍等片刻,喝点水....

- 写一个小Demo
首先写一个普通的类Login
package com.service;
import org.springframework.stereotype.Component;
@Component
public class Login {
public void userLogin(String name) {
System.out.println(name+"用户登录");
}
}
再写一个用aspect打开的文件Test(我也不懂这样说对不对,可以用这样的方法创建它)

package com.aspect;
public aspect Test {
/**
* 此处的ececution中的参数中的com.service.Login是指运行
* com.service.Login类运行前
* 第一个*号是指返回值不限,第二个*号是指方法名不限
* 括号只是任意个数类型不限的形参
*/
before() : execution(* com.service.Login.*(..)) {
System.out.println("登录前的检查");
}
after() : execution(* com.service.Login.*(..)) {
System.out.println("登录后的检查");
}
}
之后测试一下
@Test
public void testAspectJ() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Login login = applicationContext.getBean("login", Login.class);
login.userLogin("Slence");
}

可以看到运行userLogin方法之前和之后自动加上了Test类中的代码
网友评论