spring-aop-aspectj(Schema)-case

阅读更多
基于Schema 配置切面:
1、切点定义的语言依然是AspectJ表达式语言
2、把不用AspectJ相关的注解定义切面,而是通过xml配置,相当于注解的xml表达的aop定义形式。

定义advice增强
public class LoginMonitorAspectj {
	public static final ThreadLocal time = new ThreadLocal();

	public void before(JoinPoint pjp) {
		time.set(System.currentTimeMillis());
		System.out.println("invoke before --------------------------------");
		System.out.println(pjp.toLongString());
		System.out.println(Arrays.toString(pjp.getArgs()));
	}
	public void afterReturning(JoinPoint pjp,User user) {
		System.out.println("invoke afterReturning ---------------------------------");
		System.out.println(user.toString());
		System.out.println("times : "+(System.currentTimeMillis() - time.get()));
	}
	public void _exception(JoinPoint pjp,Exception ex) {
		System.out.println("invoke _exception ---------------------------------");
		System.out.println("times : "+(System.currentTimeMillis() - time.get()));
	}
}



业务方法定义
@Service
public class LoginService implements ILoginService {

	public User login(String username, String password) {
		User user = new User();
		System.out.println("in login method");
		user.setPassword("123456");
		user.setUsername("admin");
		return user;
	}

	@MethodExecuteTimes
	public void regist(String username, String password, String email, int age) {
		   System.out.println(Arrays.toString(new Object[]{username,password,age}));
	}

}


配置applicationContext.xml


		
		
	
	 
	
	
	
	
	   
	        
	        
	        
	   
	
  


详细见附件
  • spring-aop-config.rar (391.5 KB)
  • 下载次数: 1

你可能感兴趣的:(spring,aop,config)