Xml method --- spring AOP intercepts user operations

Source: Internet
Author: User

Logging is required to record all web operations to the database and use aop to record logs. Of course, this is a good choice, there are multiple methods for integrating aop logging in spring (multiple configuration methods for aop, annotations, and xml configuration). Here is only an example of xml configuration. 1. Write logging 2. Configure aop 1. Write Java code for logging @ SuppressWarnings ("unchecked") public class LogService {public void logAll (JoinPoint jp) {System. out. println ("in LogService, method logAll invoked. "); System. out. println ("================"); StringBuilder sb = new StringBuilder (); sb. append ("method:" + jp. getSignature (). getName (); HttpServletRequest request = ServletActionContext. getRequest (); Iterator <Entry <String, Object> iter = request. getParameterMap (). entrySet (). iterator (); boolean isFirst = true; sb. append ("paras:"); while (iter. hasNext () {Entry <String, Object> entry = iter. next (); if (isFirst) {isFirst = false;} else {sb. append ("#");} sb. append (entry. getKey () + "="); Object [] allValue = (Object []) entry. getValue (); for (int I = 0; I <allValue. length; I ++) {if (I! = 0) {sb. append (",");} sb. append (allValue [I]. toString () ;}} System. out. println (sb. toString (); System. out. println ("==================") ;}2. Configure aop in applicationContext. configure xml code in Xml </aop: config> <bean id = "logService" class = "com. cjnetwork. cms. service. logService "> </bean> <aop: config> www.2cto.com <aop: aspect id =" myAspect "ref =" logService "> <aop: pointcut expression =" execution (* com. cjnetwork. c Ms. action. *. *(..)) "id =" logPointCut "/> <aop: before method =" logAll "pointcut-ref =" logPointCut "/> </aop: aspect> </aop: config> <bean id = "logService" class = "com. cjnetwork. cms. service. logService "> </bean> <aop: config proxy-target-class =" true "/> the basic configuration is as follows: <aop: config proxy-target-class = "true"/>, indicates that the cglib proxy is forcibly used instead of the java proxy. This is very important. If you use the proxy that comes with java, an exception is thrown, prompting that the proxy class cannot be converted to our own class. This is because the default attribute is false. In this proxy mode, the interface must be implemented. The class returned by the proxy can be converted to the corresponding interface class, but cannot be directly converted to the class implementation. This method is not recommended.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.