Can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Example On Spring AOP Before Advice, After Advice

Spring » On Jul 25, 2012 | { 3 Comments }

Hi friends…, will see one example on spring Before Advice, and After Advice.

Files Required

  • MyAfterAdvice.java
  • MyImplClass.java
  • MyInterFace.java
  • MyWelcomeAdvice.java
  • OurLogic.java
  • spconfig.xml

Directory Structure

MyAfterAdvice.java

MyImplClass.java

MyInterFace.java

MyWelcomeAdvice.java

OurLogic.java

spconfig.xml

Output:

Explanation:

Actually in spring frame work, while creating a spring bean we need to follow POJI-POJO model.  It means a spring bean is a combination of an interface and implementation class. In AOP always we need to create a spring bean in the form of interface and implementation class only, because the IOC container internally creates proxy class by implementing that interface with the help of ProxyFactoryBean.

In spring configuration file, we need to configure a predefined bean class called ProxyFactoryBean, this class is used to configure our bean class and its required advices as a group.

In the client application, we are passing id of ProxyFactoryBean (id4) to get the object, because we need proxyed object, but not target one hope you understand.  In client application we cannot typecast the object into our bean class type, because internally the object is created for proxy class but not for our bean class right :-)   so we need to typecast into interface type because the proxy class will also implements the same interface internally remember…!!

In OurLogic.java if we typecast into our class type, then it will generates ClassCastException exception.

Hope you are clear, if not refer previous concepts of AOP.

 

 

 

What you are thinkig....

3 Responses to “Example On Spring AOP Before Advice, After Advice”
  1. Abhishek says:

    Hi,

    Thanks for this post. But i have confusion in OurLogic.java,line no.15, it should be MyImplClass inter =(MyImplClass)factory.getBean(“id4″); rather we have used MyInterFace inter(MyInterFace)factory.getBean(“id4″); because MyInterFace is an interface and we can;t instansiate interface. Hope you got my problem.please help

  2. Java4s says:

    @Abhishek

    We are not creating any object to that interface, we just taking object into interface reference, like how we are using List and ArrayList :-)

    Hope you are clear.

  3. Arin says:

    Thanks a lot for providing such a wonderfull tutorial with easy to understand words….I would request you to write books. It will a block buster…Trust me.

If you want a pic to show with your comment, go get a gravatar !
Please post your questions on Java4s Answers forum

Name*
Ask a Question ?
or
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links Current & UpComing Tutorials Java4s.com
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets


UpComing :
Jsps,Web Services
Java4s is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.


© 2014 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by Team Java4s.