target

/ Spring / 没有评论 / 336浏览
@Aspect
public class UserServiceAspect {
@Before("target(com.jilinwula.spring.aop.IUserService)")
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));
}

}
public class UserService{

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

@Login
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 id="user" 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");
IUserService userService = applicationContext.getBean("userService", IUserService.class);
UserService user = applicationContext.getBean("user", UserService.class);
userService.register("admin", "吉林乌拉", "jilinwula");
userService.login("admin", "jilinwula");

user.register("admin", "吉林乌拉", "jilinwula");
user.login("admin", "jilinwula");
}
需要登陆

用户注册: username: admin nickName: 吉林乌拉 password: jilinwula 需要登陆 用户登陆: username: admin password: jilinwula 用户注册: username: admin nickName: 吉林乌拉 password: jilinwula 用户登陆: username: admin password: jilinwula