spring aspectj小试2

阅读更多
/**
 * @author zhuc
 * @version 2012-8-21 下午1:38:39
 */
@Aspect
@Component
public class Aspect2 {

	/**
	 * @param joinPoint
	 * @param log
	 */
	@After(value = "@annotation(log)")
	public void doAfter(JoinPoint joinPoint, Log log) {
		for (Object obj : joinPoint.getArgs()) {
			System.out.println("参数: " + obj);
		}
		System.out.println(log.function() + "," + log.desc());
		System.out.println("doAfter......");
	}
}

 

/**
 * @author zhuc
 * @version 2012-8-21 下午1:42:07
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {
	String function() default "";
	String desc() default "";
}

 

/**
 * @author zhuc
 * 
 */
public class Service {

	/**
	 * @param name
	 * @return
	 */
	@Log(function = "日志管理", desc = "添加Log")
	public String addLog(String name) {
		System.out.println(name);
		return "hello:" + name;
	}
}

 



	
	
	
	
	

 

 

/**
 * @author zhuc
 * @version 2012-8-21 下午1:45:19
 */
public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext ac = new FileSystemXmlApplicationContext(
				"src/test/java/org/zhuc/maven/aspectj/aspectj.xml");
		Service s = (Service) ac.getBean("service");
		String str = s.addLog("zhuc");
		System.out.println(str);
	}

}

 

运行结果:

zhuc
参数: zhuc
日志管理,添加Log
doAfter......
hello:zhuc

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