@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDemo3 {
@Resource(name="orderDao")
private OrderDao orderDao;
@Test
public void demo1(){
orderDao.save();
orderDao.update();
orderDao.delete();
orderDao.find();
}
}
7.通知类型和切入点
通知类型:
前置通知:在目标方法执行之前执行;
后置通知:在目标方法执行之后执行,若目标方法异常则不执行;
环绕通知:在目标方法执行之前和之后执行,需要手动执行目标类的方法;
最终通知:无论目标方法是否出现异常,最终都会执行;
异常抛出通知:在目标方法出现异常时执行;
切入点表达式:
execution表达式:[方法访问修饰符] 返回值 包名.类名.方法名(参数)
<!-- 配置切面类 -->
<bean></bean>
<!-- 进行aop的配置 -->
<aop:config>
<!-- 配置切入点表达式:哪些类的哪些方法需要进行增强 -->
<aop:pointcut expression="execution(* cn.itcast.spring.demo3.*Dao.save(..))"/>
<aop:pointcut expression="execution(* cn.itcast.spring.demo3.*Dao.delete(..))"/>
<aop:pointcut expression="execution(* cn.itcast.spring.demo3.*Dao.update(..))"/>
<aop:pointcut expression="execution(* cn.itcast.spring.demo3.*Dao.find(..))"/>
<!-- 配置切面 -->
<aop:aspect ref="myAspectXml">
<aop:before method="before" pointcut-ref="pointcut1"/>
<aop:after-returning method="afterReturing" pointcut-ref="pointcut2"/>
<aop:around method="around" pointcut-ref="pointcut3"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut4"/>
<aop:after method="after" pointcut-ref="pointcut4"/>
</aop:aspect>
</aop:config>
f.Spring使用AspectJ进行AOP开发,注解方式
1.导入jar
* spring的传统AOP的开发的包
spring-aop-4.2.4.RELEASE.jar
com.springsource.org.aopalliance-1.0.0.jar
* aspectJ的开发包:
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aspects-4.2.4.RELEASE.jar
2.配置文件
引入AOP约束:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
3.目标类和切面类
public class ProductDao {
public void save(){
System.out.println("保存商品...");
}
public void update(){
System.out.println("修改商品...");
}
public void delete(){
System.out.println("删除商品...");
}
public void find(){
System.out.println("查询商品...");
}
}
@Aspect
public class MyAspectAnno {
@Before("MyAspectAnno.pointcut1()")
public void before(){
System.out.println("前置通知===========");
}
@Pointcut("execution(* cn.itcast.spring.demo4.ProductDao.save(..))")
private void pointcut1(){}
}
4.配置注解自动代理
<!-- 开启aop注解的自动代理 -->
<aop:aspectj-autoproxy/>
<!-- 目标类============ -->
<bean></bean>
<!-- 配置切面类 -->
<bean></bean>
5.其他通知的注解
@Aspect
public class MyAspectAnno {
@Before("MyAspectAnno.pointcut1()")
public void before(){
System.out.println("前置通知===========");
}
@AfterReturning("MyAspectAnno.pointcut2()")
public void afterReturning(){
System.out.println("后置通知===========");
}
@Around("MyAspectAnno.pointcut3()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("环绕前通知==========");
Object obj = joinPoint.proceed();
System.out.println("环绕后通知==========");
return obj;
}
@AfterThrowing("MyAspectAnno.pointcut4()")
public void afterThrowing(){
System.out.println("异常抛出通知========");
}
@After("MyAspectAnno.pointcut4()")
public void after(){
System.out.println("最终通知==========");
}
//自定义切入点
@Pointcut("execution(* cn.itcast.spring.demo4.ProductDao.save(..))")
private void pointcut1(){}
@Pointcut("execution(* cn.itcast.spring.demo4.ProductDao.update(..))")
private void pointcut2(){}
@Pointcut("execution(* cn.itcast.spring.demo4.ProductDao.delete(..))")
private void pointcut3(){}
@Pointcut("execution(* cn.itcast.spring.demo4.ProductDao.find(..))")
private void pointcut4(){}
}
@Aspect
publicclass MyAspectAnno {
@Before("MyAspectAnno.pointcut1()")
publicvoid before(){