Spring Tutorial Home

  • Managing Database Transactions
  • Remoting in Spring
  • Working with the Web Layer

Spring Home

Java Resources

Community

Site

Creating After Throwing Advice-2


Beans.xml File


<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <!-- this is the object that will be proxied by Spring's AOP infrastructure -->

    <bean id="service" class="com.visualbuilder.aop.ServiceImpl" />

    <!-- this is the actual advice itself -->

    <bean id="advice" class="com.visualbuilder.aop.AdviceClass" />

    <aop:config>

        <aop:aspect ref="advice">

                <aop:pointcut id="beforeMethodExecution"

                expression="execution(* com.visualbuilder.aop.Service.testAdvice(String)) and args(name)" />

            <aop:after-throwing pointcut-ref="beforeMethodExecution"

                method="beforeMethod" />


        </aop:aspect>

    </aop:config>

</beans>


Output:- If we pass null as name then exception is thrown and after throwing advice executes and if we pass any string then it will print the length.


Passing Null


Sep 19, 2008 2:47:35 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh

INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@133f1d7: display name [org.springframework.context.support.ClassPathXmlApplicationContext@133f1d7]; startup date [Fri Sep 19 14:47:35 IST 2008]; root of context hierarchy

Sep 19, 2008 2:47:35 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [com/visualbuilder/aop/beans.xml]

Sep 19, 2008 2:47:36 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory

INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@133f1d7]: org.springframework.beans.factory.support.DefaultListableBeanFactory@17d5d2a

Sep 19, 2008 2:47:36 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@17d5d2a: defining beans [service,advice,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,beforeMethodExecution]; root of factory hierarchy


The method is advised.

 


Passing Value


Sep 19, 2008 2:43:16 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh

INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@133f1d7: display name [org.springframework.context.support.ClassPathXmlApplicationContext@133f1d7]; startup date [Fri Sep 19 14:43:16 IST 2008]; root of context hierarchy

Sep 19, 2008 2:43:16 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [com/visualbuilder/aop/beans.xml]

Sep 19, 2008 2:43:16 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory

INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@133f1d7]: org.springframework.beans.factory.support.DefaultListableBeanFactory@17d5d2a

Sep 19, 2008 2:43:16 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@17d5d2a: defining beans [service,advice,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,beforeMethodExecution]; root of factory hierarchy


This is the Test Advice Method for Name 13

                    

Copyright © 2010 VisualBuilder. All rights reserved