1. 首页
  2. 乌拉客

@within

@Aspect
public class UserServiceAspect {
  @Before("@within(com.jilinwula.spring.annotation.Login)")
  public void before() {
    System.out.println("需要登陆");
  }
}
@Retention(RetentionPolicy.RUNTIME) // 生命注解的保留期限
@Target({ElementType.METHOD, ElementType.TYPE}) // 生命注解可以使用的类型
public @interface Login {
  boolean role() default false; // 生命注解成员
}
@Login
public class UserService{

  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.UserService"/>
<bean class="com.jilinwula.spring.annotation.UserServiceAspect"/>
<aop:aspectj-autoproxy/>
@Test
public void test() {
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
  UserService userService = applicationContext.getBean("userService", UserService.class);
  userService.register("admin", "吉林乌拉", "jilinwula");
  userService.login("admin", "jilinwula");
}
需要登陆
用户注册: username: admin nickName: 吉林乌拉 password: jilinwula
需要登陆
用户登陆: username: admin password: jilinwula

原创文章,作者:二十四分之七倍根号六,如若转载,请注明出处:http://jilinwula.com/363.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注

联系我们

邮件:admin@jilinwula.com

QQ:923505769

 

QR code