spring学习(三):AOP

一、Service类

package service;

public class MyService {
	public void doMyService() {
		System.out.println("This is my service");
	}
}

二、切面类

为上面的service类的前后增加内容

package aspect;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyAspect {
	public Object function(ProceedingJoinPoint joinPoint) throws Throwable{
		System.out.println("start: "+joinPoint.getSignature().getName());
		Object object = joinPoint.proceed();
		System.out.println("end: "+joinPoint.getSignature().getName());
		return object;
	}
}

三、配置文件

两个bean,和一个配置aop



 
 	 
 	
 	
 		
 		
 			
 		
 	
 
图片为上述配置文件几个东西的关系 spring学习(三):AOP_第1张图片

四、测试运行

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.MyService;

public class MyTest {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
		MyService ms = (MyService) context.getBean("myService");
		ms.doMyService();
	}
}


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