XML Schema Based AOP Example by R4R Team

XML Schema Based AOP Example:-
To understand the concepts related to XML Schema Based AOP, let us write an example which will implement few of the advices. To write our example with few advices, let us have working Eclipse IDE in place and follow the following steps to create a Spring application:

1 Create a project with a name SpringTest and create a package com.r4r.in under the src folder in the created project.

2 Add required Spring libraries using Add External JARs option . 

3 Add Spring AOP specific libraries aspectjrt.jar, aspectjweaver.jar and aspectj.jar in the project. 

4 Create Java classes Login, Employee and MainApp under the com.r4r.in package. 

5 Create Beans configuration file Beans.xml under the src folder. 

6 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

1 Create the Login.java class.

package com.r4r.in;

public class Login {

public void beforeAdvice()

{

System.out.println("Knowing the Employee Information.");

}

public void afterAdvice()

{

System.out.println("Finish the Employee Information.");

}

public void afterReturningAdvice(Object retVal)

{

System.out.println("Returning:" + retVal.toString() );

}

public void AfterThrowingAdvice(IllegalArgumentException ex)

{

System.out.println("An exception is: " + ex.toString());

}

}

2 Create Employee.java class.

package com.r4r.in;

public class Emloyee {

private Integer id;

private String name;

public void setId(Integer id)

{

this.id = id;

}

public Integer getId()

{

System.out.println("Id : " + id );

return id;

}

public void setName(String name)

{

this.name = name;

}

public String getName()

{

System.out.println("Name : " + name );

return name;

}

public void printThrowException()

{

System.out.println("Exception raised");

throw new IllegalArgumentException();

}

}

3 Create MainApp.java file.

package com.r4r.in;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

Employee emp = (Employee) context.getBean("emp");

emp.getName();

emp.getId();

emp.printThrowException();

}

}

4 Create Beans.xml file.

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

xsi:schemaLocation="http://www.springframework.org/schema/beans

                              http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

                             http://www.springframework.org/schema/aop

                            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

<aop:config>

            <aop:aspect id="log" ref="login">

          <aop:pointcut id="selectAll" expression="execution(* com.r4r.in*(..))"/>

          <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>

        <aop:after pointcut-ref="selectAll" method="afterAdvice"/>

       <aop:after-returning pointcut-ref="selectAll" returning="retVal" method="afterReturningAdvice"/>

      <aop:after-throwing pointcut-ref="selectAll" throwing="ex" method="AfterThrowingAdvice"/>

        </aop:aspect>

</aop:config>

<!-- Definition for student bean -->

<bean id="emp" class="com.r4r.in.Employee">

<property name="name" value="Vipul" />

<property name="id" value="01"/>

</bean>

<!-- Definition for login aspect -->

<bean id="login" class="com.r4r.in.Login"/>

</beans>

5 Now Run the Application .If everything is ok then show the below message.

Knowing the Employee Information.

Name : Vipul

Finish the Employee Information.

Returning:Vipul

 Knowing the Employee Information.

Id : 01

Finish the Employee Information.

Returning:01

Knowing the Employee Information.

Exception raised

Finish the Employee Information.

An exception is : java.lang.IllegalArgumentException

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!