开发者

AspectJ - Why put transaction control in a seperate place?

开发者 https://www.devze.com 2023-03-03 13:13 出处:网络
Ok, I haven\'t fully understood the philosophy why AOP AspectJ is good for. I have now implemented a Logging and transaction control for when withdrawing money from a bankaccount. Alright, why is it g

Ok, I haven't fully understood the philosophy why AOP AspectJ is good for. I have now implemented a Logging and transaction control for when withdrawing money from a bankaccount. Alright, why is it good in doing that? I could likewise implement the control in the same class file where I've also stored all my banking methods (withdraw, deposit, balance... etc). And the logging I could've create a new class for it, and thereafter make an instance of it in the BankAccount class.

So why do I need to use AOP, AspectJ for it? I haven't fully understood the idea...

Here's my aspect file

public aspect SafeWithdrawal {                                                                                  

pointcut checking(BankAccount bk, float x): execution(* BankAccount.withdraw(float)) && target(bk) && args(x);                                                                                                   

public static void BankAccount.LogChange(String str){                                                    
    System.out.println(str);                                                                             
}                              开发者_JS百科                                                                          


before(BankAccount b, float x) : checking(b, x) {
        if(b.getBalance() >= x) {
            BankAccount.LogChange("Account changing. $" + x + " withdrawn...");
        } else {               
            BankAccount.LogChange("Account does not have. $" + x + " to withdrawn...");
        }

}                                                                                                        

}  


The idea is that your domain methods like withdraw can remain laser-focused on your business processes and secondary concerns like logging, transactions, profiling, etc. don't get in the way.

0

精彩评论

暂无评论...
验证码 换一张
取 消