I want to change a method body and not to change existing source code running on web app.
Does spring offer that magical function?
I want to change a method body and not to change existing source code running on web app.
Does spring offer that magical function?
Hello amiladomingo:
What I mean by change is not to replace totally but do some small changes. It's like below:
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?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(); ........
If it can't be possible, we had to change source code ,that would be the last way we wanna choose.
Thanks.
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
The aspect method will execute, when one of the insert methods (i.e. insertStudent) returns.Code:@After("execution(public * com.dao.*.insert*(..))") public void aspectMethod() { // Your new logic }
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
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:
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.Code:TransactionManager manager = new TransactionManager(); try { manager.start(); myplayedGameDAO.mergeGameList(map); manager.commit(); } catch (Exception e) { log.error(" error : " + e.getMessage()); } finally { manager.end(); }
If it is just the successful return you want to handle, you can you use "AfterReturn" adviceThere are other advices you can use (i.e. Before, AfterReturn, AfterThrowing)![]()
Amila Domingo