I am learning AOP, in the spring project, I have the following piece of code.
MainDemoApp.java
public class MainDemoApp {
public static void main(String[] args) {
// read spring config java class
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(DemoConfig.class);
// DemoConfig.class is a simple class with just component scanning enabled
// and with @EnableAspectJAutoProxy
// get the bean from spring container
AccountDAO accountDAO = context.getBean("accountDAO", AccountDAO.class);
System.out.println(accountDAO.toString()); // line to be modified
// call the business method
accountDAO.addAccount();
// close the context
context.close();
}
}
AccountDAO.java
@Component
public class AccountDAO {
public void addAccount() {
System.out.println(this.toString()); // line to be modified
}
}
and my aspect class
MyDemoLoggingAspect.java
@Aspect
@Component
public class MyDemoLoggingAspect {
// this is where we add all our related advices for logging
// let's start with a @Before advice
@Before("execution(public void addAccount())")
public void beforeAddAccountAdvice() {
System.out.println("\n=======>>> Executing @Before Advice");
}
}
Now when I run main
method in MainDemoApp
, I get the following output-
Output-
com.luv2code.aopdemo.dao.AccountDAO@3e0e1046
=======>>> Executing @Before Advice
com.luv2code.aopdemo.dao.AccountDAO@3e0e1046
As you can see the lines
System.out.println(accountDAO.toString());
And
System.out.println(this.toString());
give the same result, before advice is also executed successfully.
By default toString()
is defined as-
this.getClass().getName() + "@" + Integer.toHexString(this.hashCode())
But when I explicitly print out getClass().getName() the results are different
System.out.println(accountDAO.getClass().getName());
And
System.out.println(this.getClass().getName());
Output-
com.luv2code.aopdemo.dao.AccountDAO$$EnhancerBySpringCGLIB$$88eb61b6
=======>>> Executing @Before Advice
com.luv2code.aopdemo.dao.AccountDAO
Both the class names differ, I don't know which type of class name is that.
Also when I explicitly print hashCode()
the results are different.
I am confused about getting different results with-
toString()
, whentoString()
includesgetClass().getName()
andhashCode()
getClass().getName()
hashCode()
Why are these differences there, I did not override any of these methods (At my end), does this has something related to AOP proxy?
Kindly help me clarify these doubts.
Edit-
I also have a very strange observation that when I modify
MainDemoApp.java
AccountDAO accountDAO = context.getBean("accountDAO", AccountDAO.class);
System.out.println(accountDAO.hashCode());
// call the business method
accountDAO.addAccount(accountDAO);
And AccountDAO.java
is modified as-
public void addAccount(AccountDAO obj) {
System.out.println(this.hashCode() + " " + obj.hashCode());
}
NOW the result of hashCode()
is the same but if I don't change the method signature and call hashCode()
on the previous piece of code, the result is different
Can anyone explain this also?