preprocessing parameters of a controller using spring AOP

Source: Internet
Author: User

Actual programming, there may be a situation, the foreground pass the parameters, we need certain processing to use, for example, there is such aController

@ Controllerpublic class matchoddscontroller { @Autowired private Matchoddsservcie Matchoddsservice;  @RequestMapping (value =  "/listodds", method = Requestmethod.get, produces = {mediatype.application_json_value})  @ResponseBody public list<oddsmodel> listodds (@RequestParam Date startDate, @ Requestparam Date endDate) {return Matchoddsservice.listodds (StartDate, endDate);}}  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

The reception of the startDate and endDate is the two date, the actual use we need to convert it to two date corresponding to the day 11 points, if only such a class, we can directly in the method of processing on the front can be

However, there are two other classes with the same business logic

@ Controllerpublic class matchproductcontroller { @Autowired  Private Matchproductservice Matchproductservice;  @RequestMapping (value =  "/listproduct", method = Requestmethod.get, produces = {mediatype.application_json_value})  @ResponseBody public list<productmodel> listproduct (@RequestParam Date StartDate, @RequestParam Date endDate) {return matchproductservice.listmatchproduct ( StartDate, endDate); }}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
@ Controllerpublic class matchcontroller { @Autowired private Matchservice Matchservice;  @RequestMapping (value =  "/listmatch", method = Requestmethod.get, produces = {mediatype.application_json_value})  @ResponseBody public list<matchmodel> listmatch (@RequestParam Date startDate , @RequestParam Date endDate) {return matchservice.listmatch (StartDate, endDate);}}  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

Of course, you can also write two util methods, respectively processing startDate and endDate , but in order to make Controller it look cleaner, we still use AOP to do it, by the way for more complex AOP application to do the groundwork

The configuration class is used in this application, and the main configuration classes are as follows:

@SpringBootApplication@EnableAspectJAutoProxy(proxyTargetClass = true) //开启AspectJ代理,并将proxyTargetClass置为true,表示启用cglib对Class也进行代理public class Application extends SpringBootServletInitializer { ...}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5

Create a new aspect class below with the following code

@Aspect1@Configuration2PublicClassSearchdateaspect {@Pointcut ("Execution (* com.ronnie.controller.*.list* (java.util.date,java.util.date)) && args (startdate,enddate)")3PrivatevoidSearchdatepointcut (date startdate, date endDate) {4}@Around (value ="Searchdatepointcut (Startdate,enddate)", ArgNames ="Startdate,enddate")5Public ObjectDealsearchdate (Proceedingjoinpoint joinpoint, date startdate, date endDate)throws throwable {//6 object[] args = Joinpoint.getargs (); Span class= "hljs-comment" >//7 if (Args[0] = = null) {args[0] = Calendars.gettodayeleven (); args[ 1] = Dateutils.add (new Date (), 7, Timeunit.days); //default shows all odds for today and later} else {args[0] = dateutils.addhours (startDate, 11); Args[1] = dateutils.addhours (endDate, 11);} return joinpoint.proceed (args); //8}}             
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24

Explain the meaning of each place above, the label is consistent with the comment after the statement

    1. Indicates that this is a slice class
    2. Indicates that the class is a configuration class that ApplicationContext loads the configuration at startup and scans the class to
    3. Defines a pointcut that execution(* com.ronnie.controller.*.list*(java.util.Date,java.util.Date)) represents any return value, a com.ronnie.controller method that begins with a list of any class under the package, with a parameter of two date types that args(startDate,endDate) indicates that spring is required to pass in both parameters
    4. Define the name of the Pointcut
    5. Configure Surround Notifications
    6. ProceedingJoinPointis automatically passed in to handle the actual call
    7. Get the parameter, the following code is the Modify parameter
    8. Calling the target class with modified parameters

For more information, refer to

    • Http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html
    • http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/

http://blog.csdn.net/ro_wsy/article/details/50858810

preprocessing parameters of a controller using spring AOP

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.