0°

within

@Aspect
public class UserServiceAspect {
  @Before("within(com.jilinwula.spring.aop.*)")
  public void beforeArgs() {
    System.out.println("需要登陆");
  }
}
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
未分类
未分类
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论