addheader

Wednesday, 11 July 2012


TUTIORALS FROM MADHAV:


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

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



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

Ex1:  this example program will describes working with Before advice: annotation based.
 @Before



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;
import org.aspectj.lang.annotation.*;
@Aspect
public class LoginAspect {
      @Before("execution(public void showDetails())")
 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">


 <aop:aspectj-autoproxy/>
<bean id="emp" class="madhav.Employee"></bean>
<bean id="aspect" class="madhav.LoginAspect"></bean>

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


Ex2:
@After

@After("execution(public void showDetails())")




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  


                   

No comments:

Post a Comment