哈尔滨网站关键词优化排名,wordpress 无法登陆,灰色项目网站代做,如何开发自己的小程序现在#xff0c;如果您有兴趣创建一个以Aspectj的Aspect和Before批注的形式使用面向方面编程#xff08;AOP#xff09;的Spring MVC Webapp来审核用户对屏幕的访问#xff0c;那么这是您想要阅读的博客。 正如我在上一个博客中所说的那样#xff0c;审核用户对屏幕的访问… 现在如果您有兴趣创建一个以Aspectj的Aspect和Before批注的形式使用面向方面编程AOP的Spring MVC Webapp来审核用户对屏幕的访问那么这是您想要阅读的博客。 正如我在上一个博客中所说的那样审核用户对屏幕的访问是面向方面编程AOP很好解决的少数几个交叉问题之一。 就我的演示代码而言其想法是您将注释添加到适当的控制器并且每次用户访问页面时都会记录该访问。 使用此技术您可以构建最流行的屏幕的图片从而构建应用程序中最流行的功能块。 知道了这些细节之后就可以更轻松地决定将开发目标放在何处因为开发几乎没有人使用过的那些应用程序块是没有用的。 对于演示代码我创建了一个简单的Spring MVC应用程序该应用程序具有两个屏幕主页和帮助页面。 在此之上我创建了一个简单的批注 Audit 用于将控制器标记为需要审计的控制器并非所有控制器都需要尤其是如果您选择审计功能点而不是单个屏幕时并且告诉建议对象的屏幕ID。 我已经在下面的代码片段中演示了这一点 Audit(Home) RequestMapping(value /, method RequestMethod.GET) public String home(Locale locale, Model model) { 在停留在AspectJ方面之前首先要做的是使用为工作设计的Spring模板创建标准的Spring MVC Web应用程序 下一步要做的就是对POM文件进行一堆更改如我之前的博客中所述 。 这些并不是一切必不可少的它们对于一切正常工作都是必不可少的。 但是请确保您添加了aspectJwearver依赖项并删除了AspectJ插件定义。 该应用程序具有两个控制器和两个简单的JSP。 第一个控制器是从Spring MVC应用程序中获取的HomeController 第二个控制器是旨在在应用程序的任何页面上显示帮助的HelpController 。 我在下面包括了HelpController的showHelp(…)方法但这只是为了完整性。 在这种情况下只要有几个要审核的控制器实际上没有关系。 Controller()
public class HelpController { Audit(Help) // User has visited the help page RequestMapping(value /help, method RequestMethod.GET) public String showHelp(RequestParam int pageId, Model model) { String help getHelpPage(pageId); model.addAttribute(helpText, help); return help; } 从上面的代码中您可以看到我的两个RequestMapping方法都使用Audit注释进行了注释因此下一步是其定义 Retention(RetentionPolicy.RUNTIME)
Target(ElementType.METHOD)
public interface Audit { String value();
} 此代码的关键是保留策略和目标。 保留策略必须设置为RetentionPolicy.RUNTIME 这意味着编译器不会丢弃注释并确保在运行时将注释保存在JVM中。 Target定义可以在何处应用注释。 在这种情况下我只希望将其应用于方法因此目标是ElementType.METHOD 。 注释必须包含一个值在这种情况下该值用于保存用户当前正在访问的屏幕的名称。 下一个关键抽象是AuditAdvice类如下所示 Aspect
public class AuditAdvice { Autowired private AuditService auditService; /** * Advice for auditing a users visit to a page. The rule is that the Before annotation * applies to any method in any class in the com.captaindebug.audit.controller package * where the class name ends in Controller and the method is annotated by Audit. * * param auditAnnotation * Audit annotation holds the name of the screen were auditing. */ Before(execution(public String com.captaindebug.audit.controller.*Controller.*(..)) annotation(auditAnnotation) ) public void myBeforeLogger(Audit auditAnnotation) { auditService.audit(auditAnnotation.value()); } } 这有两个AspectJ注释 Aspect和Before 。 Aspect批注将AuditAdvice类标记为一个方面 而Before批注意味着在定义与作为Before批注的参数的表达式匹配的任何方法之前将调用auditScreen(…)方法。 这种表达是很酷的想法。 我已经在Spring应用程序中使用AspectJ的AfterThrowing建议中的博客中介绍了execution表达式的构造 但是总而言之我将对所有具有公共可见性返回String且位于com.captaindebug.audit.controller程序包中并且将单词Controller包含Before方法都应用Before注释方法。班级名称。 换句话说我很难将此执行表达式应用于除我的应用程序的控制器以外的任何对象并且这些控制器必须由Audit注释进行注释如annotation(auditAnnotation)表达式和auditScreen(…)方法的Audit auditAnnotation参数。 这意味着我不能无意中将Audit注释应用于除控制器之外的任何东西 AuditAdvice类将实际审核的职责委托给AuditService 。 这是一项虚拟服务因此与其执行诸如将审核事件存储在数据库中之类的有用操作不如将其简单地添加到日志文件中。 Service
public class AuditService { private static Logger logger LoggerFactory.getLogger(AuditService.class); /** * Audit this screen against the current user name * * Its more useful to put this info into a database so that that you can count visits to * pages and figure out how often theyre used. That way, you can focus your design on the * popular parts of your application. The logger is just for demo purposes. */ public void audit(String screenName) { String userName getCurrentUser(); logger.info(Audit: {} - {}, userName, screenName); } /** * Get the current logged on user name by whatever mechanism available */ private String getCurrentUser() { return Fred; } } 因此这就是本文所涵盖的代码现在剩下要做的就是整理Spring配置文件在这里没有太多要做。 首先与任何AOP应用程序一样您需要添加以下AOP启用行 aop:aspectj-autoproxy/ …连同其架构详细信息 xmlns:aophttp://www.springframework.org/schema/aop
xsi:schemaLocationhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 其次您需要通过更新context:components-scan元素将建议类告知Spring上下文 context:component-scan base-packagecom.captaindebug.auditcontext:include-filter typeaspectjexpressioncom.captaindebug.audit.aspectj.AuditAdvice //context:component-scan 您还可以选择从架构位置URI的末尾删除版本号。 例如 http://www.springframework.org/schema/context/spring-context.3.0.xsd 变成 http://www.springframework.org/schema/context/spring-context.xsd 这样做的原因是由于没有任何版本号的架构URI似乎指向该架构的最新版本因此它在将来的某个时刻简化了Spring版本的升级。 为了完整起见我的配置文件如下所示 ?xml version1.0 encodingUTF-8?
beans:beans xmlnshttp://www.springframework.org/schema/mvcxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:beanshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd!-- DispatcherServlet Context: defines this servlets request-processing infrastructure --!-- Enables the Spring MVC Controller programming model --annotation-driven /!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --resources mapping/resources/** location/resources/ /!-- Resolves views selected for rendering by Controllers to .jsp resources in the /WEB-INF/views directory --beans:beanclassorg.springframework.web.servlet.view.InternalResourceViewResolverbeans:property nameprefix value/WEB-INF/views/ /beans:property namesuffix value.jsp //beans:beanaop:aspectj-autoproxy/context:component-scan base-packagecom.captaindebug.auditcontext:include-filter typeaspectjexpressioncom.captaindebug.audit.aspectj.AuditAdvice //context:component-scan/beans:beans 最后当您运行该应用程序时将记录用户对主页的访问。 当用户单击帮助链接时也会记录对帮助页面的访问。 日志文件中的输出如下所示 INFO : com.captaindebug.audit.service.AuditService - Audit: Fred - Home
INFO : com.captaindebug.audit.controller.HomeController - Welcome home! the client locale is en_US
INFO : com.captaindebug.audit.service.AuditService - Audit: Fred - Help 有关此代码和下一个博客的代码请访问github https //github.com/roghughe/captaindebug/tree/master/audit-aspectj 参考 使用AspectJ审计Spring MVC Webapp。 Captain Debug的Blog博客中的JCG合作伙伴 Roger Hughes的第2部分 。 翻译自: https://www.javacodegeeks.com/2013/07/auditing-a-spring-mvc-webapp-with-aspectj-part-2.html