Spring Tutorial Home

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

Spring Home

Java Resources

Community

Site

Annotation Support in Spring AOP-2 


Beans.xml









<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
">

    <!--  Enabling AspectJ annotation -->

     <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />

    

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

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

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

    <bean id="aspect" class="com.visualbuilder.aop.LoggerAdvice" />


</beans>



LoggerAdvice.java









package com.visualbuilder.aop;



import java.util.logging.Logger;



import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;



@Aspect

public class LoggerAdvice {

    public static Logger logger=Logger.getLogger("LoggerAdvice");

   

    @Before("execution(* com.visualbuilder.aop.AccountServiceImpl.deposit(double))")

    public void beforeDepositLog() throws Throwable {

        logger.info("******Entering into Deposit Method********");



    }


}



 


MainClass.java









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/bean.xml");


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


                        object.deposit(100);


                        System.out.println("Balance after Deposit " +object.getBalance());


                        object.withdrawal(10);


                        System.out.println("Balance after Withdrawal " +object.getBalance());


                        }


}



Output









Sep 21, 2008 11:54:46 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh


INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]; startup date [Sun Sep 21 11:54:46 IST 2008]; root of context hierarchy


Sep 21, 2008 11:54:46 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions


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


Sep 21, 2008 11:54:46 AM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory


INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]: org.springframework.beans.factory.support.DefaultListableBeanFactory@4a63d8


Sep 21, 2008 11:54:46 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons


INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4a63d8: defining beans [org.springframework.aop.config.internalAutoProxyCreator,service,aspect]; root of factory hierarchy


Sep 21, 2008 11:54:47 AM com.visualbuilder.aop.LoggerAdvice beforeDepositLog


INFO: ******Entering into Deposit Method********


Balance after Deposit 100.0



Balance after Withdrawal 90.0




 



                    

Copyright © 2010 VisualBuilder. All rights reserved