商业网站建设大纲,ip做网站地址,网络营销优秀案例,下载app下载安卓免费文章目录 一、基于注解的AOP1、配置Spring环境2、在beans.xml文件中定义AOP约束3、定义记录日志的类【切面】4、定义Bean5、在主配置文件中配置扫描的包6、在主配置文件中去开启AOP的注解支持7、测试8、优化改进9、总结 一、基于注解的AOP
1、配置Spring环境
dependencie… 文章目录 一、基于注解的AOP1、配置Spring环境2、在beans.xml文件中定义AOP约束3、定义记录日志的类【切面】4、定义Bean5、在主配置文件中配置扫描的包6、在主配置文件中去开启AOP的注解支持7、测试8、优化改进9、总结 一、基于注解的AOP
1、配置Spring环境
dependencies!-- 导入Spring的jar包--dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.2.1.RELEASE/version/dependencydependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactIdversion1.9.4/version/dependency
/dependencies2、在beans.xml文件中定义AOP约束
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd
/beans3、定义记录日志的类【切面】
Component(logger)
Aspect // 表示的是一个切面
public class Logger {// 目的在调用业务方法之前进行增强【前置通知】Before(execution(* cn.bdqn.service.impl.*.*(..)))public void beforePrintLog(){System.out.println(前置通知----beforePrintLog---开始打印日志啦);}// 后置通知AfterReturning(execution(* cn.bdqn.service.impl.*.*(..)))public void afterReturningPrintLog(){System.out.println(后置通知----afterReturningPrintLog);}
} 注意该类的两个细节
a、Component注解向容器中注册一个Bean。
b、Aspect注解表示这个是一个切面类。
c、Before注解表示的是这个是前置增强/前置通知。
4、定义Bean
package cn.bdqn.domain;public class User {}package cn.bdqn.service;
public interface UserService {// 保存用户public void save(User user);
}package cn.bdqn.service.impl;
Service(userService) // 向容器中注册Bean
public class UserServiceImpl implements UserService {Overridepublic void save(User user) {System.out.println(保存用户啦);}
} 注意对于业务Bean我们也需要通过Service注解来向容器中注册。
5、在主配置文件中配置扫描的包
beanscontext:component-scan base-packagecn.bdqn/
/beans6、在主配置文件中去开启AOP的注解支持
beansaop:aspectj-autoproxy/
/beans7、测试
public class UserServiceTest {Testpublic void testUserService() throws Exception{ApplicationContext ac new ClassPathXmlApplicationContext(beans.xml);UserService userService (UserService) ac.getBean(userService);userService.queryAll();}
}8、优化改进
问题我们看到对于切面类中定义的通知有一个共性问题是切入点表达式是相同的 那我们在想能否也像xml配置的那样把切入点表达式给抽取出来呢答案是可以的改造如下
Component(logger)
Aspect // 表示的是一个切面
public class Logger {Pointcut(execution(* cn.bdqn.service.impl.*.*(..)))private void pt(){}// 目的在调用业务方法之前进行增强【前置通知】Before(pt())public void beforePrintLog(){System.out.println(前置通知----beforePrintLog---开始打印日志啦);}// 演示的后置通知AfterReturning(pt())public void afterReturningPrintLog(){System.out.println(后置通知----afterReturningPrintLog);}
}9、总结 配置业务Bean Service(userService)
public class UserServiceImpl implements UserService{}配置切面Bean 需要在切面类上定义Aspect // 表示的是一个切面 Component(logger)
Aspect // 表示的是一个切面
public class Logger {}在切面类中的通知方法上定义相应的通知 Before 前置通知
AfterReturning后置通知
AfterThrowing: 异常通知
After最终通知
Around: 环绕通知定义切入点表达式 Before(execution(* cn.bdqn.service.impl.*.*(..)))
public void beforePrintLog(){System.out.println(前置通知----beforePrintLog---开始打印日志啦);
}在主配置文件中去开启AOP的注解 aop:aspectj-autoproxy/