Saturday, January 4, 2014

Spring AOP example Declarative Approach Using AOP namespace (Before Advice)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
">
  
    <bean id='msg' class="aopproj.MessageClass"></bean>
    <bean id='logging' class='aopproj.BeforeAdvice'></bean>
    <aop:config>
      
        <aop:aspect ref='logging'>
            <aop:before method='logBefore' pointcut="within (aopproj.MessageClass)"/>
          
        </aop:aspect>
      
      
      
      
    </aop:config>
  
  
</beans>

BeforeAdvice.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package aopproj;

/**
 *
 * @author Rishitha
 */
public class BeforeAdvice {
   
    public void logBefore(){
        System.out.println("log before ");
       
       
    }
   
}

MessageClass.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package aopproj;

/**
 *
 * @author Rishitha
 */
public class MessageClass {
   
   
    private String messageHeader="msgHeader";
    private String messageBody="msgBody";
   
    public MessageClass(){
       
    }

    public String getMessageHeader() {
        return messageHeader;
    }

    public void setMessageHeader(String messageHeader) {
        this.messageHeader = messageHeader;
    }

    public String getMessageBody() {
        return messageBody;
    }

    public void setMessageBody(String messageBody) {
        this.messageBody = messageBody;
    }
   
   
   
   
   
   
   
   
   
}




TestMain.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package aopproj;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author Rishitha
 */
public class TestMain {
   
    public static void main(String args[]){
       
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-beans.xml");
        MessageClass msg = (MessageClass)ctx.getBean("msg");
       
        msg.getMessageBody();
       
       
    }
   
   
   
}

Jars Required:

Spring related jars
aop-alliance.jar
aspectj.jar
aspectjweaver.jar



No comments:

Post a Comment