@annotation

/ Spring / 没有评论 / 318浏览
@Aspect
public class UserServiceAspect {
@Before("@annotation(com.jilinwula.spring.annotation.Login)")
public void beforeLogin() {
System.out.println("需要登陆");
}
}
@Retention(RetentionPolicy.RUNTIME) // 生命注解的保留期限
@Target(ElementType.METHOD) // 生命注解可以使用的类型
public @interface Login {
boolean role() default false; // 生命注解成员
}
public interface IUserService {

public void login(String username, String password);

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

}
public class UserServiceImpl implements IUserService {

@Login
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));
}

}
@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