首页 > 代码库 > Spring_6_使用注解的方式实现AOP

Spring_6_使用注解的方式实现AOP

1)PersonService 接口类:

public interface PersonService {
	public void save(String naem);
	public void update(String name,Integer id);
	public String getPersonName(Integer id);
}

2)PersonServiceBean 实现类:

@Service
// 使用自动扫描的方式自动装配
public class PersonServiceBean implements PersonService {

	@Override
	public void save(String naem) {
		// throw new RuntimeException("异常");
		System.out.println("调用save()方法");
	}

	@Override
	public void update(String name, Integer id) {
		System.out.println("调用update()方法");
	}

	@Override
	public String getPersonName(Integer id) {
		System.out.println("调用getPersonName()方法");
		return "xoxo";
	}
}

3MyIntercerptor拦截器类:

/**
 * 切面
 */

@Aspect
//标志为切面
@Component
// 使用自动扫描的方式自动装配
public class MyInterceptor {
	// springDaoBean.PersonServiceBean为要拦截的类,
	// .*(..)表示拦截类中的所有方法
@Pointcut("execution(* springDaoBean.PersonServiceBean.*(..))")
	private void anyMethod() {// 声明一个切入点
	}

	// 前置通知的设定及参数的设置和获取
	@Before("anyMethod() && args(name)")
	public void doAccessCheck(String name) {
		System.out.println("前置通知:" + name);
	}

	// 后置通知的设定及参数的设置和获取
	@AfterReturning(pointcut = "anyMethod()", returning = "result")
	public void doAfterReturning(String result) {
		System.out.println("后置通知:" + result);
	}

	@After("anyMethod()")
	public void doAfter() {
		System.out.println("最后通知");
	}

	// 例外置通知的设定及参数的设置和获取
	@AfterThrowing(pointcut = "anyMethod()", throwing = "e")
	public void doAfterThrowing(RuntimeException e) {
		System.out.println("例外通知:" + e);
	}

	// 环绕通知的设定
	@Around("anyMethod()")
	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("进入方法");
		Object result = pjp.proceed();
		System.out.println("退出方法");
		return result;
	}
}

4)XML文件的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
   xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

	<!-- 添加的自动扫描组件 -->
	<context:component-scan base-package="springDaoBean" />
	
	<!-- 添加拦截器 -->
	<aop:aspectj-autoproxy />

	<!-- 使用注解的方式注入 -->
	<!-- 
<bean id="personServiceBean" 		
class="springDaoBean.PersonServiceBean" />
 	 -->
	<!-- 
<bean id="myInterceptor" 				
class="springDaoBean.MyInterceptor" /> 
-->
</beans>

5)springAopTest 测试类:

public class springAopTest {
	@Test
	public void instanceSpring() {

		AbstractApplicationContext ctx = new 		
  ClassPathXmlApplicationContext("springXml/beans.xml");

		PersonService personServiceBean = (PersonService) 
ctx.getBean("personServiceBean");
		// 前置通知测试及参数的打印
		personServiceBean.save("oooo");
		//后置通知的测试及参数的打印
		personServiceBean.getPersonName(0);
	}
}