SpringAop log management

Source: Internet
Author: User

SpringAop log management

Spring AOP

1.Understanding of AOP-the idea of division of labor for each part and integration during runtime


2.Understanding process-oriented, object-oriented, and Aspect-oriented ideas

1)Process-oriented: when the room is decorated, prepare a lamp and pull a wire to connect the lamp.


2)Object-oriented: design the locations in the room where the wire interface needs to be used, and then set the wire interface at the corresponding location for future use.


3)Face-to-Face: When decorating a house, first design the areas where the wire interface needs to be installed. Then, set the wire interface and do not open the interface. This is the connection point, when the electrical cutting is required, open the interface and the plug-in is the starting point.


Aspect: function (LOGIN log)

Objectives: Major Aspects (LOGIN)

Aspect: Integration of entry point implant notification

Connection point: where secondary functions (logs) can be inserted

Entry Point: To insert a secondary function

Notification: encapsulate objects of sub-functions

Insert: insert notification into the entry point


3.Implement login and log management (using Spring AOP)


1)LoginService LogService TestMain

2)Use Spring to manage LoginService and LogService objects

3)Determine which connection points are the starting point. In the configuration file

4)Encapsulate LogService as notification

5)Implant notifications to the entry point

6)Client call Target






  1. Aspect id = "dd" ref = "logService">


  2. Execution (** cn.com. spring. service. impl .*.*(..))

    1) * All Modifiers

    2) * all return types

    3) * all class names

    4) * All method names

    5) *. All parameter names


    1. ILoginService. java


    1. Package cn.com. spring. service;

    2. Public interface ILoginService {
    3. Public boolean login (String userName, String password );
    4. } 2. LoginServiceImpl. java


      1. Package cn.com. spring. service. impl;

      2. Import cn.com. spring. service. ILoginService;

      3. Public class LoginServiceImpl implements ILoginService {

      4. Public boolean login (String userName, String password ){
      5. System. out. println ("login:" + userName + "," + password );
      6. Return true;
      7. }

      8. }

        3. ILogService. java


        1. Package cn.com. spring. service;

        2. Import org. aspectj. lang. JoinPoint;

        3. Public interface ILogService {
        4. // LOG method without Parameters
        5. Public void log ();
        6. // LOG method with Parameters
        7. Public void logArg (JoinPoint point );
        8. // Method with parameters and returned values
        9. Public void logArgAndReturn (JoinPoint point, Object returnObj );
        10. }
          4. LogServiceImpl. java


          1. Package cn.com. spring. service. impl;

          2. Import org. aspectj. lang. JoinPoint;

          3. Import cn.com. spring. service. ILogService;

          4. Public class LogServiceImpl implements ILogService {

          5. @ Override
          6. Public void log (){
          7. System. out. println ("*************** Log *******************");
          8. }

          9. // Method with parameters without return values
          10. Public void logArg (JoinPoint point ){
          11. // This method returns an array containing the request, ActionCofig, and other class objects.
          12. Object [] args = point. getArgs ();
          13. System. out. println ("target parameter list :");
          14. If (args! = Null ){
          15. For (Object obj: args ){
          16. System. out. println (obj + ",");
          17. }
          18. System. out. println ();
          19. }
          20. }

          21. // Method with parameters and return values
          22. Public void logArgAndReturn (JoinPoint point, Object returnObj ){
          23. // This method returns an array containing the request, ActionCofig, and other class objects.
          24. Object [] args = point. getArgs ();
          25. System. out. println ("target parameter list :");
          26. If (args! = Null ){
          27. For (Object obj: args ){
          28. System. out. println (obj + ",");
          29. }
          30. System. out. println ();
          31. System. out. println ("execution result:" + returnObj );
          32. }
          33. }
          34. } 5. applicationContext. java




            1. Xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
            2. Xmlns: p = "http://www.springframework.org/schema/p"
            3. Xsi: schemaLocation = "http://www.springframework.org/schema/beans
            4. Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            5. Http://www.springframework.org/schema/aop
            6. Http://www.springframework.org/schema/aop/spring-aop-2.5.xsd>






            7. Expression = "execution (* cn.com. spring. service. impl. LoginServiceImpl .*(..))"
            8. Id = "myPointcut"/>






            9. 6. TestMain. java


              1. Public class TestMain {
              2. Public static void testSpringAOP (){
              3. ApplicationContext ctx = new ClassPathXmlApplicationContext ("app *. xml ");

              4. ILoginService loginService = (ILoginService) ctx. getBean ("loginService ");
              5. LoginService. login ("zhangsan", "12344 ");
              6. }
              7. Public static void main (String [] args ){
              8. TestSpringAOP ();
              9. }
              10. } 7. output result:
                1. Log in: zhangsan, 12344
                2. Target parameter list:
                3. Zhangsan,
                4. 12344,

                5. The execution result is: true resolution: 1. the login () method System. out. println ("login:" + userName + "," + password) is called first );

                  2. Then, the logArgAndReturn () method is called to output logs, and whether the login () method is successful is returned.


                  1. System. out. println ("target parameter list :");
                  2. If (args! = Null ){
                  3. For (Object obj: args ){
                  4. System. out. println (obj + ",");
                  5. }
                  6. System. out. println ();
                  7. System. out. println ("execution result:" + returnObj );
                  8. }

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.