Results 1 to 7 of 7

Thread: Do we have chance to change a method?

  1. #1
    Join Date
    May 2010
    Posts
    17

    Default Do we have chance to change a method?

    I want to change a method body and not to change existing source code running on web app.
    Does spring offer that magical function?

  2. #2
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    287

  3. #3
    Join Date
    May 2010
    Posts
    17

    Smile

    Hello amiladomingo:
    What I mean by change is not to replace totally but do some small changes. It's like below:

    Code:
            StopWatch stopWatch = new StopWatch();
            stopWatch.start("perfTest");
    
            for (int x = 0; x < 1000000; x++) {
                String out = target.formatMessage("foo");
            }
    
            //insert my new logic
            stopWatch.stop();
           ........
    The scenario is we want to inspect a large of projects ,when new records are inserted or update in these projects running on tomcat , I hope all these data also be synchronized to my inspection database so that we can summarize latest data that need to be checked by administrator. So I need to do some extra work to the existing code, but cause the number of projects is pretty big , adding logic for every single class method is not reasonable, so I just want to add synchronizing code in a relative easy and common way. Basically we have got all the methods we need to handle and enough materials Does it sound crazy?
    If it can't be possible, we had to change source code ,that would be the last way we wanna choose.
    Thanks.

  4. #4
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    287

    Default

    You can use AOP to achieve this.

    http://static.springsource.org/sprin.../html/aop.html

    Lets say you want to execute a piece code after all the inserts to the DB

    Code:
    @After("execution(public * com.dao.*.insert*(..))")
    public void aspectMethod()
    {
          // Your new logic
    }
    The aspect method will execute, when one of the insert methods (i.e. insertStudent) returns.

    Note - method marked with @after should be prepared to handle both normal return and exception.

    There are other advices you can use (i.e. Before, AfterReturn, AfterThrowing)
    Amila Domingo

  5. #5
    Join Date
    May 2010
    Posts
    17

    Default

    Quote Originally Posted by amiladomingo View Post
    You can use AOP to achieve this.

    http://static.springsource.org/sprin.../html/aop.html

    Lets say you want to execute a piece code after all the inserts to the DB

    Code:
    @After("execution(public * com.dao.*.insert*(..))")
    public void aspectMethod()
    {
          // Your new logic
    }
    The aspect method will execute, when one of the insert methods (i.e. insertStudent) returns.

    Note - method marked with @after should be prepared to handle both normal return and exception.

    There are other advices you can use (i.e. Before, AfterReturn, AfterThrowing)
    Hi,amiladomingo:
    I know what you mean , after interceptor is the first idea I can think of , but is abandoned right now, because the method to be intercept doesn't throw a exception,if we use after interceptor or around interceptor, we can't know whether the transaction is successful or not. the existing code is like below:
    Code:
    TransactionManager manager = new TransactionManager();
    			try {
    				manager.start();
    				myplayedGameDAO.mergeGameList(map);
    				manager.commit();
    			} catch (Exception e) {
    				log.error(" error : " + e.getMessage());
    			} finally {
    				manager.end();
    			}
    So pure after interceptor can't handle that. data must be synchronized only when inspected data updating is successful. we need insert code into some certain point of existing code and the new logic can access the rest of the method.

  6. #6
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    287

    Default

    There are other advices you can use (i.e. Before, AfterReturn, AfterThrowing)
    If it is just the successful return you want to handle, you can you use "AfterReturn" advice
    Amila Domingo

  7. #7
    Join Date
    May 2010
    Posts
    17

    Default

    Quote Originally Posted by amiladomingo View Post
    If it is just the successful return you want to handle, you can you use "AfterReturn" advice
    Hello amiladomingo:
    There's no guarantee that the method will have a return value.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •