Advice Parameters-1

Started by thiruvasagamani, Sep 24, 2008, 11:36 AM

Previous topic - Next topic

thiruvasagamani

Advice Parameters-1

The beans.xml file is now changed to add the arguments in the pointcut defination. The beans.xml is as follows:-

<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 [url]http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">[/url%]

    <!-- 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:before pointcut-ref="beforeMethodExecution"

                method="beforeMethod" />

        </aop:aspect>

    </aop:config>

</beans>



The main class is changed now to call the parameter values.




package com.visualbuilder.aop;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext;



public final class MainClass {



    public static void main(String[] args) throws Exception {

       

        BeanFactory ctx = new ClassPathXmlApplicationContext("com/visualbuilder/aop/beans.xml");

        Service object = (Service) ctx.getBean("service");

        object.testAdvice("visualbuilder");

    }

}


Output:-

When the Paramter is Visualbuilder then the output is as follows:-

Sep 19, 2008 1:56:05 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 13:56:05 IST 2008]; root of context hierarchy

Sep 19, 2008 1:56:05 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 1:56:05 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@2bb514

Sep 19, 2008 1:56:05 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

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

The method is advised.

This is the Test Advice Method for Name visualbuilder

When the Paramter is any other value then the output is as follows:-

Sep 19, 2008 1:56:05 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 13:56:05 IST 2008]; root of context hierarchy

Sep 19, 2008 1:56:05 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 1:56:05 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@2bb514

Sep 19, 2008 1:56:05 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2bb514: 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 joe parker
Thiruvasakamani Karnan