Tuesday, August 29, 2017

Spring AOP (Aspect oriented programming) Simple Example


Overview: The spring-aop module provides an AOP Alliance-compliant aspect-oriented programming implementation allowing you to define, for example, method interceptors and pointcuts to cleanly decouple code that implements functionality that should be separated

Step by Step Run a simple Spring AOP program:

1. Command:

mvn archetype:generate -DgroupId=com.dk -DartifactId=AopSpring -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


2. Command for compile:
   mvn clean compile
3. Run:
   mvn exec:java (It will fail because of missing mojo plugin, We need to add it.)
POM.xml initially:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dk</groupId>
<artifactId>AopSpring</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>AopSpring</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

4. Addplugin:

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions><execution>
<goals><goal>java</goal></goals>
</execution></executions>
<configuration>
<mainClass>com.dk.App</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
</plugin>
</plugins>
</build>

4. Now pom.xml look like this:


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dk</groupId>
<artifactId>AopSpring</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>AopSpring</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions><execution>
<goals><goal>java</goal></goals>
</execution></executions>
<configuration>
<mainClass>com.dk.App</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
</plugin>
</plugins>
</build>
</project>

5. command: mvn clean compile -->>No error

6. command : mvn exec:java -->>No error

6.1 Snap from cmd:

D:\projects\AopSpring>mvn exec:java
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building AopSpring 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.1:java (default-cli) @ AopSpring >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.1:java (default-cli) @ AopSpring <<<
[INFO]
[INFO] --- exec-maven-plugin:1.1:java (default-cli) @ AopSpring ---
Hello World!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.045s
[INFO] Finished at: Tue Apr 26 09:32:27 GMT 2016
[INFO] Final Memory: 1M/3M
[INFO] ------------------------------------------------------------------------
D:\projects\AopSpring>


7. Make Folder resources in the package com.main i.e com.main.resources and create springconfig.xml in it.It is used to define properties i.e beans

7.1: Also run command mvn eclipse:eclipse to import it in eclipse and do work on it in eclipse

8. In the spring .xml add xsd,namespaces,beans,tags to support aop ( imp: <aop:aspectj-autoproxy/>)

springconfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<aop:aspectj-autoproxy/>
<bean class="com.dk.LogAspect"/>
<bean id="b1" class="com.dk.BusinessLogicClass"/>
</beans>

9. Need to ADD dependencies for spring core, spring context and aop.
Now pom.xml-->
------------------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dk</groupId>
<artifactId>AopSpring</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>AopSpring</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>

</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.dk.App</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
</plugin>
</plugins>
</build>
</project>





https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpu7PoMMF6Nu7LXGe0R6s-UXi5jCgMFZVUmGstAiMaQoGZmAfbMpwEuhVIuOZavnVAC74dy7Z9D3-V8bivZLA9_6Sp9Uz4uQdzBCe7c13Hc1LNcUHzIwrXYhTG5sBgaLQgDeGwg5LjJbFT/s320/123.png








Contents of Classes
1. BusinessLogicClass.java

package com.dk;
public class BusinessLogicClass {

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

public String businessLogicMethod2()
{
System.out.println("businessLogicMethod2");
return null;
}

}


2. App.java

package com.dk;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App
{
public static void main( String[] args )
{
System.out.println( "Aspect1" );
AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("springconfig.xml");
BusinessLogicClass b1=(BusinessLogicClass) ctx.getBean("b1");
b1.businessLogicMethod1();
}
}

3. LogAspect.java

package com.dk;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class LogAspect {

@Before("execution(public void businessLogicMethod1())")
private void method1()
{
System.out.println("Log aspect is running");
}
}


Springconfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<aop:aspectj-autoproxy/>
<bean class="com.dk.LogAspect"/>
<bean id="b1" class="com.dk.BusinessLogicClass"/>
</beans>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dk</groupId>
<artifactId>AopSpring</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>AopSpring</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>

</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.dk.App</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
</plugin>
</plugins>
</build>
</project>



Compile: mvn clean compile
Run: mvn exec:java

Output:

Log aspect is running
businessLogicMethod1
















Using @After

package com.dk;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class LogAspect {


@Before("execution(public void businessLogicMethod1())")
private void method1()
{
System.out.println("Log aspect is running");
}

@After("execution(public void businessLogicMethod2())")
private void method2() {
System.out.println("After Log aspect is running");
}

}

App.java
package com.dk;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App
{
public static void main( String[] args )
{

System.out.println( "Aspect1" );

AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("springconfig.xml");

BusinessLogicClass b1=(BusinessLogicClass) ctx.getBean("b1");

b1.businessLogicMethod1();
b1.businessLogicMethod2();
}
}







Output:

Aspect1

Apr 26, 2016 1:00:11 PM org.sp.........

Log aspect is running
businessLogicMethod1
businessLogicMethod2
After Log aspect is running





No comments:

Post a Comment