Announcement Announcement Module
Collapse
No announcement yet.
AOP Method Before/After Advice Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AOP Method Before/After Advice

    Hi,

    I am testing AOP in Spring using Before & After method advisors. My code below executes just fine, but the tracing does not print anything... Could someone please help me find my problem?

    Thank you,
    ~ Sam

    ---------------------------------------------
    package example.business;

    import java.util.Iterator;
    import java.util.List;

    import example.Player;
    import example.persistence.common.PlayerDAO;
    import example.persistence.springHibernateDAO.PlayerDAOHi bernateWithSpring;
    import example.util.Util;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlAp plicationContext;
    import org.springframework.core.io.support.ResourcePatter nResolver;

    public class PlayerManagerImpl implements PlayerManager {

    private PlayerDAO playerDao;
    static Log log = LogFactory.getLog(PlayerManagerImpl.class.getName( ));

    public void setPlayerDao(PlayerDAO playerDao) {
    this.playerDao = playerDao;
    }


    // Method to add and save a player and return ID
    public Long savePlayer(Player player){

    try {
    player = this.playerDao.savePlayer(player);

    if (player.getId() == null) {
    System.out.println("There was an error with saving this player: "+player.getFirstName());
    return null;
    }
    else {
    System.out.println("Player object saved successfully: "+player.getFirstName());
    return player.getId();
    }
    }
    catch (Exception e) {
    System.out.println("There was an error with saving this player: "+player.getFirstName());
    System.out.println(e.getMessage());
    return null;
    }
    }


    //main method
    public static void main(String[] args) {

    // Spring + Hibernate DAO Test
    String[] paths = {"applicationContext.xml"};
    ApplicationContext ctx = new ClassPathXmlApplicationContext(paths);

    PlayerManager pm = (PlayerManager)ctx.getBean("myPlayerServiceTarget" );

    Player player = new Player();
    player.setFirstName("James");
    player.setLastName("Gosling");
    player.setJerseyNumber(99);
    player.setDraftDate(Util.parseDate("2003-Feb-28"));
    player.setAnnualSalary(200000f);

    try {
    Long id = pm.savePlayer(player);

    if (id == null) {
    System.out.println("The Player object was not saved.");
    }
    else {
    System.out.println("Player object saved successfully: "+id.toString());
    }
    }
    catch (Exception e) {
    System.out.println(e.getMessage());
    }
    }
    }


    ---------------------------------------------------
    package example.aop.advice;

    import java.lang.reflect.Method;
    import org.springframework.aop. MethodBeforeAdvice;

    public class TracingBeforeAdvice
    implements MethodBeforeAdvice
    {
    public void before(Method m,
    Object[] args,
    Object target)
    throws Throwable
    {
    System.out.println(
    "Hello world! (by " +
    this.getClass().getName() +
    ")");
    }
    }

    ------------------------------------------------
    package example.aop.advice;

    import java.lang.reflect.Method;
    import org.springframework.aop.AfterReturningAdvice;

    public class TracingAfterAdvice
    implements AfterReturningAdvice
    {
    public void afterReturning(Object object,
    Method m,
    Object[] args,
    Object target)
    throws Throwable
    {
    System.out.println(
    "Hello world! (by " +
    this.getClass().getName() +
    ")");
    }
    }

    -----------------------------------------------------------------

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName"><value>com.mysql.jdbc.Drive r</value></property>
    <property name="url"><value>jdbc:mysql://localhost/hibernate?relaxAutoCommit=true</value></property>
    <property name="username"><value>sam</value></property>
    <property name="password"><value>sam</value></property>
    </bean>

    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="mappingResources">
    <list>
    <value>Player.hbm.xml</value>
    <value>Team.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">net.sf.hibernate.dialect.M ySQLDialect</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    </props>
    </property>
    </bean>


    <!-- DAO Objects -->
    <bean id="playerDAO" class="example.persistence.springHibernateDAO.Play erDAOHibernateWithSpring">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    <bean id="teamDAO" class="example.persistence.springHibernateDAO.Team DAOHibernateWithSpring">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>

    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->

    <bean id="myTransactionManager" class="org.springframework.orm.hibernate.Hibernate TransactionManager">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>

    <bean id="myPlayerServiceTarget" class="example.business.PlayerManagerImpl">
    <property name="playerDao">
    <ref bean="playerDAO"/>
    </property>
    </bean>

    <bean id="myPlayerService" class="org.springframework.aop.framework.ProxyFact oryBean">
    <property name="proxyInterfaces">
    <value>example.business.PlayerManager</value>
    </property>
    <property name="target">
    <ref local="myPlayerServiceTarget"/>
    </property>
    <property name="interceptorNames">
    <list>
    <value>myTransactionInterceptor</value>
    <value>theTracingBeforeAdvisor</value>
    <value>theTracingAfterAdvisor</value>
    </list>
    </property>
    </bean>

    <!-- Interceptor for Transactions -->

    <bean id="myTransactionInterceptor" class="org.springframework.transaction.interceptor .TransactionInterceptor">
    <property name="transactionManager">
    <ref bean="myTransactionManager"/>
    </property>
    <property name="transactionAttributeSource">
    <value>
    example.business.PlayerManager.save*=PROPAGATION_R EQUIRED
    example.business.PlayerManager.selectPlayers=PROPA GATION_MANDATORY
    </value>
    </property>
    </bean>

    <!-- Advisor pointcut definition for before advice -->
    <bean id="theTracingBeforeAdvisor"
    class="org.springframework.aop.support.RegexpMetho dPointcutAdvisor">
    <property name="advice">
    <ref local="theTracingBeforeAdvice"/>
    </property>
    <property name="pattern">
    <value>.*</value>
    </property>
    </bean>

    <!-- Advisor pointcut definition for after advice -->
    <bean id="theTracingAfterAdvisor"
    class="org.springframework.aop.support.RegexpMetho dPointcutAdvisor">
    <property name="advice">
    <ref local="theTracingAfterAdvice"/>
    </property>
    <property name="pattern">
    <value>.*</value>
    </property>
    </bean>

    <!-- Advice classes -->
    <bean id="theTracingBeforeAdvice"
    class="example.aop.advice.TracingBeforeAdvice"/>
    <bean id="theTracingAfterAdvice"
    class="example.aop.advice.TracingAfterAdvice"/>

    </beans>

  • #2
    The problem seems to be that you are accessing the PlayerManagerImpl bean directly rather than vai the proxy. Change your call to getBean() to use the proxy name rather than the target bean name.

    Rob

    Comment


    • #3
      Thanks

      Thanks Rob! Do you have an example of how to do that please?

      ~ Sam

      Comment


      • #4
        now getting this Error.

        Rob,

        I changed the getBean to call the proxy bean name and now getting the following exception:

        java.lang.NoClassDefFoundError: org/apache/oro/text/regex/MalformedPatternException
        at org.springframework.aop.support.RegexpMethodPointc utAdvisor.createPointcut(RegexpMethodPointcutAdvis or.java:151)
        at org.springframework.aop.support.RegexpMethodPointc utAdvisor.getPointcut(RegexpMethodPointcutAdvisor. java:134)
        at org.springframework.aop.framework.AdvisorChainFact oryUtils.calculateInterceptorsAndDynamicIntercepti onAdvice(AdvisorChainFactoryUtils.java:74)
        at org.springframework.aop.framework.HashMapCachingAd visorChainFactory.getInterceptorsAndDynamicInterce ptionAdvice(HashMapCachingAdvisorChainFactory.java :46)
        at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:154)
        at $Proxy0.savePlayer(Unknown Source)
        at example.business.PlayerManagerImpl.main(PlayerMana gerImpl.java:94)
        Exception in thread "main"

        -------------------------------

        I'm guessing that I'm missing some type of JAR. Have you seen anything like this before?

        Thanks,
        ~ Sam

        Comment


        • #5
          You need to have Apache ORO on the classpath.


          Rob

          Comment


          • #6
            Thanks

            Very cool! It worked, thanks!

            ~ Sam

            Comment

            Working...
            X