Java

Wednesday, 11 July 2012

Spring with aop schema based example programs


TUTIORALS FROM MADHAV:


     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY  


Spring with aop schema based example programs(weekend class material by madhav)


Ex1: this example program will describes working with Before advice:



Employee.java

package madhav;

public class Employee {
private String name;
private int id;
public void setDetails()
{
      name="madhav";
      id=101;
}
public void showDetails()
{
      System.out.println(name);
      System.out.println(id);
}
}



LoginAspect.java

package madhav;
public class LoginAspect {

 public void login()
 {
       System.out.println("login success");
 }
}







Spring.xml

<?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: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.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<bean id="emp" class="madhav.Employee"></bean>
<bean id="aspect" class="madhav.LoginAspect"></bean>
<aop:config>
<aop:aspect id="loggingAspect" ref="aspect">
<aop:before method="login"
pointcut="within(madhav.Employee)"/>
</aop:aspect>
</aop:config>
</beans>


TestCase.java

import madhav.*;
import org.springframework.beans.factory.BeanFactory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.beans.factory.xml.*;
public class TestCase {

            /**
             * @param args
             */
            public static void main(String[] args) {
                        // TODO Auto-generated method stub
  ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");
  // BeanFactory ctx=new XmlBeanFactory(new FileSystemResource("spring.xml"));
  Employee e=(Employee)ctx.getBean("emp");
   e.setDetails();
   e.showDetails();
   }
            }


               
Output:


login success
madhav
101



Types of advices:

1)      Before Advice   
2)      After advice
3)      Around advice
4)      After returning advice
5)      After throwing advice


Before advice:

<bean id="aspect" class="madhav.LoginAspect"></bean>
<aop:config>
<aop:aspect id="loggingAspect" ref="aspect">
<aop:before method="login"
pointcut="within(madhav.Employee)"/>
</aop:aspect>
</aop:config>


Note:  hear the advice method(login()) will call before calling the methods of(madhav.Exployee) class.

Output:

login success
madhav
101

After advice


<bean id="aspect" class="madhav.LoginAspect"></bean>
<aop:config>
<aop:aspect id="loggingAspect" ref="aspect">
<aop:after method="login"
pointcut="within(madhav.Employee)"/>
</aop:aspect>
</aop:config>




Note:  hear the advice method(login()) will call after calling the methods of(madhav.Exployee) class.

Output:

   madhav
     101
  login success

Around advice

<bean id="aspect" class="madhav.LoginAspect"></bean>
<aop:config>
<aop:aspect id="loggingAspect" ref="aspect">
<aop:around method="login"
pointcut="within(madhav.Employee)"/>
</aop:aspect>
</aop:config>


Note:  hear the advice method(login()) will call before and after  calling the methods of(madhav.Exployee) class.

Output:

login success


After returning advice

<bean id="aspect" class="madhav.LoginAspect"></bean>
<aop:config>
<aop:aspect id="loggingAspect" ref="aspect">
<aop:after-retruning method="login"
pointcut="within(madhav.Employee)"/>
</aop:aspect>
</aop:config>

After throwing advice


<bean id="aspect" class="madhav.LoginAspect"></bean>
<aop:config>
<aop:aspect id="loggingAspect" ref="aspect">
<aop:after-throwing method="login"
pointcut="within(madhav.Employee)"/>
</aop:aspect>
</aop:config>

click hear to download the source code.
TUTIORALS FROM MADHAV:


     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY  


3 comments:

  1. laptop,desktop and server servicing plz visit http://computers-hardware.weebly.com/ thank you madhav for good information

    ReplyDelete
  2. http://hyderabadproperty.weebly.com/

    ReplyDelete