Spring Tutorials

Spring Tutorial

Introduction of Spring Framework

Advantages and Disadvantages of Spring Framework

Features of Spring Framework

Basic concept of Spring Framework

Architecture of Spring Framework

Modules of Spring Framework

Goal of Spring Framework

Create Application of Spring without any IDE

Create Application of Spring in Eclipse IDE

Create Application of Spring in MYEclipse IDE

Important JAR of Spring Framework

IOC container in Spring Framework

Bean and Applicationcontext container in Spring Framework

Example of Spring BeanFactory Container in Spring Framework

Example of Spring ApplicationContext Container in Spring Framework

Bean in Spring Framework

Lifecycle of Bean in Spring Framework

Scope of Bean in Spring Framework

Autowiring in Spring Framework

Dependency Injection in Spring Framework

Constructor-based Dependency Injection Example in Spring Framework

Setter-based Dependency Injection Example in Spring Framework

Introduction of AOP in Spring Framework

Core concept and Goal of AOP in Spring Framework

AOP Proxies in Spring framework

XML Schema Based AOP in Spring Framework

AOP Xml based Configuration Example in Spring Framework

Example of Declaring AOP Advices in Spring Framework

AOP AspectJ Annotation with Example in Spring Framework

Declaring an aspect and pointcut using Annotation in Spring Framework

Declaring AOP Advices using Annotation Example in Spring Framework

DAO support in Spring Framework

Introduction of Spring JDBC Framework in Spring Framework

Introduction of Spring JdbcTemplate in Spring Framework

Example of Spring JdbcTemplate class in Spring framework

Example of Executing SQL statements in Spring Framework

Example of Executing DDL statements in Spring Framework

SQL Stored Procedure in Spring Framework

Example of NamedParameterJdbcTemplate in Spring Framework

Example of RowMapperJdbcTemplate in Spring Framework

Introduction of ORM Framework in Spring Framework

Integration of Spring with Hibernate in Spring Framework

Integration of Spring with JPA in Spring Framework

Introduction of Spring Expression Language (SpEL) in Spring Framework

Example of Spring Expression Language (SpEL) in Spring framework

Example of Spring EL in XML and Annotation in Spring Framework

Language Reference with SpEL in Spring Framework

Operators in Spring Expression Language(SpEL) in Spring Framework

Variable in Spring Expression Language(SpEL) in Spring Framework

Introduction of Spring Framework Transaction Management in Spring Framework

Spring Framework Transaction Abstraction

Spring Declarative Transaction Management in Spring Framework

Spring Programmatic Transaction Management in Spring Framework

Introduction of Spring OXM (Object XML Mapping) in Spring Framework

Integration of Spring with Jaxb Example in Spring framework

Example of Spring with Xstream in Spring Framework

Example of Spring with Castor in Spring Framework

Remote Method Invocation(RMI) in Spring Framework

Spring and RMI Integration with Example in Spring Framework

Example of Spring and Httpinvoker in Spring Framework

Example of Spring and Hessian in Spring Framework

Integration of Spring with JMS in Spring Framework

Introduction of Webservice in Spring Framework

Spring Web Services in Spring Framework

Web Services with Jax-WS in Spring framework

Exposing and Exporting servlet-based Web Services using JAX-WS in Spring Framework

Accessing Web Services using JAX-WS in Spring Framework

Introduction of JMS in Spring Framework

JMS Messaging Models in Spring Framework

Using Spring JMS in Spring Framework

Sending and Receiving a Message Using JMS API in Spring Framework

Introduction of JMX (Java Management Extension) in Spring Framework

Integrating Beans with JMX in Spring Framework

Creating a MBeanServer in Spring Framework

Introduction Java Mail with Spring in Spring Framework

Example of Java Mail with spring in Spring Framework

Introduction of EJB(Enterprise JavaBeans) in Spring Framework

Introduction of EJB(Enterprise JavaBeans) Integration in Spring Framework

Integration of Spring With Struts 2 Example in Spring Framework

Spring MVC

Spring MVC

adplus-dvertising
Spring AOP AspectJ Annotation with Example
Previous Home Next

Initially Spring 2.5 it became possible to configure the dependency injection using annotations. So instead of using XML to describe a bean wiring, you can move the bean configuration into the component class itself by using annotations on the relevant class, method, or field declaration. @AspectJ refers to a style of declaring aspects as regular Java classes annotated with annotations.

The @AspectJ style was introduced by the AspectJ project as part of the AspectJ 5 release. Spring interprets the same annotations as AspectJ 5, using a library supplied by AspectJ for pointcut parsing and matching. The AOP runtime is still pure Spring AOP though, and there is no dependency on the AspectJ compiler or weaver.

You will also need following AspectJ libraries on the classpath of your application. These libraries are available in the 'lib' directory of an AspectJ installation, otherwise you can download them from the internet.

  1. aspectjrt.jar
  2. aspectjweaver.jar
  3. aspectj.jar
Technologies used to run this Application.
  1. Spring 3.0
  2. Eclipse IDE
  3. JDK 1.6
Spring AspectJ AOP implementation provides below annotations
  1. @Aspect declares the class as aspect.
  2. @Pointcut declares the pointcut expression.

These annotations used to create advices are given below

  1. @Before declares the before advice. It is applied before calling the actual method.
  2. @After declares the after advice. It is applied after calling the actual method and before returning result.
  3. @AfterReturning declares the after returning advice. It is applied after calling the actual method and before returning result. But you can get the result value in the advice.
  4. @Around declares the around advice. It is applied before and after calling the actual method.
  5. @AfterThrowing declares the throws advice. It is applied if actual method throws exception.

Here the code of spring configuration file use any annotation in your Spring application.

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   <context:annotation-config/>
   <!-- bean definitions -->
</beans>
Annotation Based AOP Example

Let us have working Eclipse IDE in place and follow the following steps to create a Spring application:

Step1. Now you create a simple Java Project using Eclipse IDE. Follow the option File -> New -> Project and finally select Java Project wizard from the wizard list. Now name your project as AnnotationBasedAop using the wizard window as given below.

Add spring jar files

Step2. Now add spring jar in your project, To do this, right click on your project name AnnotationBasedAop and then follow the following option available in context menu: Build Path -> Configure Build Path to display the Java Build Path window as given below. There are some jar files required to run this application, To load the jar files in eclipse IDE, Right click on your project - Build Path - Add external archives - select all the required jar files - finish. and add the above three jars.

  1. antlr-runtime-3.0.1
  2. org.springframework.aop-3.1.0
  3. org.springframework.asm-3.1.0.
  4. org.springframework.aspects-3.1.0.
  5. org.springframework.beans-3.1.0.
  6. org.springframework.context.support-3.1.0.
  7. org.springframework.context-3.1.0.
  8. org.springframework.core-3.1.0.
  9. org.springframework.expression-3.1.0.
  10. commons-logging-1.1.1
Create Java class file

Step3. Now you create simply java file with any name. First we need to create a package with any name like com.r4r. To do this, Right click on src in package explorer section and follow the option : New -> Package. and then Right click on package(com.r4r) - New - class - Write the class name e.g. Employee - finish. Write the following code:

package com.r4r;
public class Employee {
	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 Occurred");
	throw new IllegalArgumentException();
	} 
}

Step4. Now you create another Java class named Login.java.

package com.r4r;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Around;

@Aspect public class Login {
@Pointcut("execution(* com.r4r.in.*.*(..))")
private void selectAll(){}
@Before("selectAll()")
public void beforeAdvice()
{
System.out.println("Knowing the Employee Information.");
}

@After("selectAll()")
public void afterAdvice(){
System.out.println("Finish the Employee Information.");
}

@AfterReturning(pointcut = "selectAll()", returning="retVal")
public void afterReturningAdvice(Object retVal){
System.out.println("Returning:" + retVal.toString() );
}

@AfterThrowing(pointcut = "selectAll()", throwing = "ex")
public void AfterThrowingAdvice(IllegalArgumentException ex){
System.out.println("An exception is: " + ex.toString());
}
}

Step5. Now create the xml file click on src- new -file -give the file name like Beans.xml then finish.

<?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:aspectj-autoproxy/>

<!-- Definition for employee bean -->
<bean id="employee" class="com.r4r.Employee">
<property name="name" value="John" />
<property name="id" value="01"/>

</bean> <!-- Definition for login aspect -->
<bean id="login" class="com.r4r.Login"/>
</beans>

Step6.Now Create the java Main class (MainApp.java).

package com.r4r;
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 employee = (Employee) context.getBean("employee");
employee.getName();
employee.getId();
employee.printThrowException();
} 
}
Run Application

Now run the Application you see the following output

Knowing the Employee Information.
Name : John
Finish the Employee Information.
Returning: John
Knowing the Employee Information.
Id : 01
Finish the Employee Information.
Returning:01
Knowing the Employee Information.
Exception Occurred
Finish the Employee Information.
An exception is : java.lang.IllegalArgumentException
Previous Home Next