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 { ...}
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
- Indicates that this is a slice class
- Indicates that the class is a configuration class that
ApplicationContext loads the configuration at startup and scans the class to
- 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
- Define the name of the Pointcut
- Configure Surround Notifications
ProceedingJoinPointis automatically passed in to handle the actual call
- Get the parameter, the following code is the Modify parameter
- 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