步骤:
一,先建立dao,daoImpl与service,。
//dao 层
public interface UserDao {
public void add();
}
//impl层
public class UserDaoImpl implements UserDao {
@Override
public void add() {
System.out.println ("增加成功");
//throw new RuntimeException ("出错了");
}
}
//service层
public interface UserService {
public void add();
}
//service impl层
public class UserServiceImpl implements UserService {
protected UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void add() {
userDao.add ();
}
}
二,设置aop
先导包
四种类型:
1,当目标方法抛出异常时调用
public void afterError(JoinPoint joinPoint, RuntimeException e){
//joinPoint.getSignature () 这个方法是拿到你的动态代理的签名文件
System.out.println (joinPoint.getSignature ().getName ()+"这个方法异常信息"+e.getMessage ());
}
2.环绕增强,前后都有
//环绕增强,传递的是其之类,环绕特有
//ProceedingJoinPoint 环绕独有
public void around(ProceedingJoinPoint proceedingJoinPoint){
//分割线以上,相当于before
System.out.println (proceedingJoinPoint.getTarget ()+Arrays.toString (proceedingJoinPoint.getArgs ()));
try {
//执行调用此方法执行目标对象相应的方法 这个方法相当于一个分割线,也就是 前置增强和后置增强的分界线
proceedingJoinPoint.proceed ();
} catch (Throwable throwable) {
throwable.printStackTrace ();
}
//分割线以下的 都为after
System.out.println (proceedingJoinPoint.getSignature ().getName ());
}
3.after在目标方法后增强,before类似。
public void after(JoinPoint joinPoint ){
System.out.println (joinPoint.getSignature ().getName ());
}
三,配置xml文件。
<!--配置dao-->
<bean id="userDao" class="com.offcn.dao.impl.UserDaoImpl"></bean>
<!--配置service层-->
<bean id="userService" class="com.offcn.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<!--配置增强的bean-->
//aopError 为aop里对应的
<bean id="aopError" class="com.offcn.aop.AopError"></bean>
四,设置切面
<!--设置切面-->
<aop:config>
<!--配置切点,,对哪一个方法,或者哪个类进行植入,expression指对哪个类来进行植入-->
<!--execution:service所有的方法-->
<aop:pointcut id="pointcut" expression="execution(* com.offcn.service.UserService.*(..))"></aop:pointcut>
<!--引入增强bean-->
<aop:aspect ref="aopError">
<!-- <!–在这个里面就可以配置你的异常增强,异常后置增强,这个方法必须引入增强bean的方法,在这里设置切点–>
//处理异常的引入
<aop:after-throwing method="afterError" pointcut-ref="pointcut" throwing="e" />
//after的引入
<aop:after method="after" pointcut-ref="pointcut"></aop:after>
<!--还有前置增强 <aop:before method=""-->
<!--环绕增强,相当于一个前置 后置-->
//环绕增强的引入
<aop:around method="around" pointcut-ref="pointcut"></aop:around>
</aop:aspect>
//三种引入 method 都对应的是 aopError里的方法
</aop:config>
text类:
@Test
public void test(){
//加载xml文件
ApplicationContext app=new ClassPathXmlApplicationContext ("applicationContext.xml");
UserService userService= (UserService) app.getBean ("userService");
userService.add ();
}
其结果就会在
<aop:pointcut id="pointcut" expression="execution(* com.offcn.service.UserService.*(..))"></aop:pointcut>
里找到的对应方法设置对应的增强。
通过注解,不设置xml 来进行增强
导包
首先在xml设置以下:
<context:component-scan base-package="com.offcn.service,com.offcn.dao" />
<bean class="com.offcn.ErrorAop.AopError"></bean>
<!--开启spring注解-->
<aop:aspectj-autoproxy/>
在对应的aopError中通过注解进行代理、增强
//设置切面 用注解
@Aspect
public class AopError {}
用after直接在前面用注解
@After (“execution(* com.offcn.service.UserService.*(…))”)
@After ("execution(* com.offcn.service.UserService.*(..))")
public void after(JoinPoint joinPoint ){
System.out.println (joinPoint.getSignature ().getName ());
}
异常的增强,需要在注解中加入pointcut
//异常增强,要加pointcut
@AfterThrowing(pointcut = "execution(* com.offcn.service.UserService.*(..))",throwing = "e")
public void afterError(JoinPoint joinPoint,RuntimeException e){
System.out.println (joinPoint.getSignature ().getName ());
}