execution的具体使用

/ Spring / 没有评论 / 358浏览

上一篇中我们了解了@Aspect注解的详细使用说明,那么在接下来的文章中,我们将通过具体的测试用例来验证上一篇中我们学习到的知识。在这一篇中我们首先看一下execution的使用。

@Aspect
public class UserServiceAspect {

@Before("execution(* register(..))")
public void beforeRegister() {
TransactionManager.beginTransaction();
}
}
public interface IUserService {

public void login(String username, String password);

public void register(String username, String nickName, String password);

}
public class UserServiceImpl implements IUserService {

public void login(String username, String password) {
System.out.println(String.format("用户登陆: username: %s password: %s", username, password));
}

public void register(String username, String nickName, String password) {
System.out.println(String.format("用户注册: username: %s nickName: %s password: %s", username, nickName, password));
}

}
<bean id="userService" class="com.jilinwula.spring.aop.UserServiceImpl"/>
<bean class="com.jilinwula.spring.annotation.UserServiceAspect"/>
<aop:aspectj-autoproxy/>
@Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
IUserService userService = applicationContext.getBean("userService", IUserService.class);
userService.register("admin", "吉林乌拉", "jilinwula");
userService.login("admin", "jilinwula");
}
开启事物
用户注册: username: admin nickName: 吉林乌拉 password: jilinwula
用户登陆: username: admin password: jilinwula