当前位置: 首页 > news >正文

域名批量查询网站广州白云区新闻头条最新消息今天

域名批量查询网站,广州白云区新闻头条最新消息今天,建立网站 用英语,中山森斯网站建设公司1.引言 面向方面的编程的主要目标是将跨领域的关注点分离。 当我们谈论跨领域的关注时#xff0c;我们指的是在我们的系统或应用程序中的多个地方使用的通用功能。 这些概念包括#xff1a; 记录中 交易管理 错误处理 监控方式 安全 实现这种分离的方法是将这些概念模块… 1.引言 面向方面的编程的主要目标是将跨领域的关注点分离。 当我们谈论跨领域的关注时我们指的是在我们的系统或应用程序中的多个地方使用的通用功能。 这些概念包括 记录中 交易管理 错误处理 监控方式 安全 实现这种分离的方法是将这些概念模块化。 这将使我们保持业务逻辑类整洁仅包含设计该类的代码。 如果我们不对这些问题进行模块化则将导致代码纠结该类包含不同的问题和代码分散相同的问题将散布在整个系统中。 在此示例中我们有一个Spring MVC应用程序该应用程序访问所请求的数据客户和订单并显示一个包含其信息的页面。 我们可以看一下不同的层 在上图中我们可以理解功能分散在不同的类中在每个服务中实现监视并且某些类包含不同的关注点例如ClientController类包含日志记录和异常处理。 为了解决这个问题我们将写一些方面来实现我们的跨领域关注点。 目标是实现以下模型 每个类仅包含与业务逻辑相关的代码而各方面将负责拦截代码以注入跨领域的关注点。 让我们看一个例子。 源代码可以在github上找到。 2.检查控制器代码 ClientController Controller public class ClientController {Autowiredprivate ClientService clientService;private static Logger mainLogger LoggerFactory.getLogger(generic);private static Logger errorLogger LoggerFactory.getLogger(errors);RequestMapping(/getClients)public String getClients(Model model, RequestParam(id) int id) {mainLogger.debug(Executing getClients request);try {Client client clientService.getClient(id);model.addAttribute(client, client);} catch (DataAccessException e) {errorLogger.error(error in ClientController, e);NotificationUtils.sendNotification(e);return errorPage;}return showClient;} } 该控制器的目的在于检索客户端并返回一个显示其信息的视图但是正如您所看到的此代码包含其他逻辑。 一方面它处理服务可能引发的异常并将其重定向到错误页面。 另一方面如果发生错误它会生成日志记录信息和通知发送。 所有这些代码对于该应用程序中的所有控制器可能还有其他类都是通用的。 的确我们本可以使用ControllerAdvice批注来集中处理异常但是本文的目标是了解如何使用Spring AOP完成它。 订单控制器也会发生同样的情况。 我不会在这里包括它因为我不想让帖子过长。 如果您想检查一下可以获取上一个链接中包含的源代码。 3.检查服务代码 客户服务 Service(clientService) public class ClientServiceImpl implements ClientService {Autowiredprivate ClientRepository clientRepository;private static Logger mainLogger LoggerFactory.getLogger(generic);private static Logger monitorLogger LoggerFactory.getLogger(monitoring);OverrideTransactional(readOnly true)public Client getClient(int id) {mainLogger.debug(Accessing client service);long startTime System.currentTimeMillis();Client client clientRepository.getClient(id);long totalTime System.currentTimeMillis() - startTime;monitorLogger.info(Invocation time {}ms , totalTime);return client;} } 除了服务调用之外它还包含日志记录的生成以及每个调用中执行时间的监视。 如果需要使用程序化事务管理我们还可以使用方面来使事务管理模块化但是在本示例中并非如此。 4.数据访问层 ClientRepositoryImpl Repository public class ClientRepositoryImpl implements ClientRepository {private JdbcTemplate template;private RowMapperClient rowMapper new ClientRowMapper();private static final String SEARCH select * from clients where clientId ?;private static final String COLUMN_ID clientId;private static final String COLUMN_NAME name;public ClientRepositoryImpl() {}public ClientRepositoryImpl(DataSource dataSource) {this.template new JdbcTemplate(dataSource);}public Client getClient(int id) {return template.queryForObject(SEARCH, rowMapper, id);}private class ClientRowMapper implements RowMapperClient {public Client mapRow(ResultSet rs, int i) throws SQLException {Client client new Client();client.setClientId(rs.getInt(COLUMN_ID));client.setName(rs.getString(COLUMN_NAME));return client;}} } 该代码不包含任何横切关注点但我将其包括在内以显示所有示例应用程序层。 5激活AOP 要配置AOP必须导入以下依赖项 dependencygroupIdorg.springframework/groupIdartifactIdspring-aop/artifactIdversion3.2.1.RELEASE/version /dependency dependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactIdversion1.6.8/version /dependency 在Spring配置文件中我们需要添加以下标签 context:component-scan base-packagexpadro.spring.mvc.aop/ aop:aspectj-autoproxy/ component-scan标签将在基本包中搜索以找到我们的方面。 要使用自动扫描您不仅需要使用Aspect注释定义方面类而且还需要包含Component注释。 如果不包括Component则需要在xml配置文件中定义方面。 6集中错误处理 我们将使用Around建议来编写方面。 该建议将截获所有使用RequestMapping注释进行注释的方法并将负责调用该方法以捕获服务引发的异常。 Component Aspect public class CentralExceptionHandler {private static Logger errorLogger LoggerFactory.getLogger(errors);Around(annotation(org.springframework.web.bind.annotation.RequestMapping) target(controller))public String handleException(ProceedingJoinPoint jp, Object controller) throws Throwable {String view null;try {view (String) jp.proceed();} catch (DataAccessException e) {errorLogger.error(error in {}, controller.getClass().getSimpleName(), e);NotificationUtils.sendNotification(e);return errorPage;}return view;} } Target批注允许我们引用被拦截的类。 现在我们有了方面处理的异常处理因此我们可以在控制器中摆脱这种逻辑。 Controller public class ClientController {Autowiredprivate ClientService clientService;private static Logger mainLogger LoggerFactory.getLogger(generic);//private static Logger errorLogger LoggerFactory.getLogger(errors);RequestMapping(/getClients)public String getClients(Model model, RequestParam(id) int id) {mainLogger.debug(Executing getClients request);//try {Client client clientService.getClient(id);model.addAttribute(client, client);//} catch (DataAccessException e) {//errorLogger.error(error in ClientController, e);//NotificationUtils.sendNotification(e);//return errorPage;//}return showClient;} } 仅需注意您可以通过以下建议拦截控制器抛出的异常 AfterThrowing(pointcutannotation(org.springframework.web.bind.annotation.RequestMapping), throwinge) 但是请注意此建议不会阻止异常的传播。 7集中日志 日志记录方面有两个建议一个关于控制器日志另一个关于服务日志 Aspect Component public class CentralLoggingHandler {private static Logger mainLogger LoggerFactory.getLogger(generic);Before(annotation(org.springframework.web.bind.annotation.RequestMapping) annotation(mapping))public void logControllerAccess(RequestMapping mapping) {mainLogger.debug(Executing {} request, mapping.value()[0]);}Before(execution(* xpadro.spring.mvc.*..*Service.*(..)) target(service))public void logServiceAccess(Object service) {mainLogger.debug(Accessing {}, service.getClass().getSimpleName());} }8.最后监控问题 我们将写另一个方面来监视问题。 建议如下 Aspect Component public class CentralMonitoringHandler {private static Logger monitorLogger LoggerFactory.getLogger(monitoring);Around(execution(* xpadro.spring.mvc.*..*Service.*(..)) target(service))public Object logServiceAccess(ProceedingJoinPoint jp, Object service) throws Throwable {long startTime System.currentTimeMillis();Object result jp.proceed();long totalTime System.currentTimeMillis() - startTime;monitorLogger.info({}|Invocation time {}ms , service.getClass().getSimpleName(), totalTime);return result;} }9检查最终代码 在将所有交叉问题模块化后我们的控制器和服务仅包含业务逻辑 Controller public class ClientController {Autowiredprivate ClientService clientService;RequestMapping(/getClients)public String getClients(Model model, RequestParam(id) int id) {Client client clientService.getClient(id);model.addAttribute(client, client);return showClient;} }Service(clientService) public class ClientServiceImpl implements ClientService {Autowiredprivate ClientRepository clientRepository;OverrideTransactional(readOnly true)public Client getClient(int id) {return clientRepository.getClient(id);} }10结论 我们已经看到了如何应用面向方面的编程来保持我们的代码整洁并专注于针对其设计的逻辑。 在使用AOP之前只需考虑其已知限制。 参考在XavierPadró的Blog博客上从我们的JCG合作伙伴 Xavier Padro 应用面向方面的编程 。 翻译自: https://www.javacodegeeks.com/2014/02/applying-aspect-oriented-programming.html
http://www.zqtcl.cn/news/424510/

相关文章:

  • 仓库改造类网站怎么做手机创建网站的软件
  • 成都平台网站建设公司邯郸网络科技公司
  • 热门课程自己做网站大型购物网站建站
  • apache 创建网站重庆建站模板
  • 手机怎么制作网站叶县建设局网站
  • 怎么做后台网站一键更新搜索引擎优化的主要策略
  • 站酷网免费素材图库官网国外域名购买
  • 石家庄个人谁做网站天体摄影
  • 徐州集团网站建设网站排名快速提升工具
  • 自己创业做原公司一样的网站做网站要学会什么语言
  • 宿州高端网站建设公司网络业务
  • 做多语言版本网站网站收录低
  • 鼎湖网站建设江门网站优化快速排名
  • 做交通事故的网站北京物联网app开发公司
  • 网站建设中 页面网络培训注册会计师
  • app网站如何做推广安装wordpress错误
  • 八零婚纱摄影工作室网站南昌微信营销公司
  • 海南网站开发公司百度网站建设费用怎么做账
  • 做网站的研究生专业微信公众号推广的方法
  • sql网站开发数据库连接失败wordpress改全屏
  • 做外贸怎么上国外网站网上注册公司流程图文
  • 网站开发 慕课厦门建设局网站技司学校
  • 中山自助建站系统多个网站能否统一做等保
  • 做网站怎么别人搜不出来电商购物网站开发需求分析
  • 教育网站设计用模板做的网站不好优化
  • php网站api接口写法中国做铁塔的公司网站
  • 做博客网站如何盈利wordpress百家号
  • 读经典做临床报名网站cnzz 网站跳出率查询
  • 青岛网站建设网站广东手机网站制作公司
  • 个人免费建站软件影视传媒网站源码