Spring aop using Maven

Project Structure



Maven Dependency for Spring AOP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
   <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>5.0.0.M5</version>
   </dependency>
 
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.2.5.RELEASE</version>
</dependency>
 
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.0.0.M3</version>
</dependency>

Triangle.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package spy.service;
 
public class Triangle 
{
 
	private String name;
 
	public String getName() 
	{
		return name;
	}
 
	public void setName(String name) 
	{
		this.name = name;
	}
 
}

Circle.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package spy.service;
 
public class Circle 
{
	private String name;
 
	public String getName() 
	{
		return name;
	}
 
	public void setName(String name)
	{
		this.name = name;
 
	}
 
}

ShapeService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package spy.service;
 
public class ShapeService 
{
 
	private Triangle triangle;
	private Circle circle;
 
 
	public Triangle getTriangle() {
		return triangle;
	}
	public void setTriangle(Triangle triangle) 
	{
		this.triangle = triangle;
	}
	public Circle getCircle() 
	{
		return circle;
	}
	public void setCircle(Circle circle) 
	{
		this.circle = circle;
	}
 
}

LoggingAspect.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package spy.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
 
@Aspect
public class LoggingAspect 
{
	@Before("execution(public String getName())")
	public void loggingadvice()
	{
		System.out.println("advice run,before method is called");
	}
}

Spring.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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"
		xmlns:context="http://www.springframework.org/schema/context"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
				http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-5.0.0.xsd">
 
<aop:aspectj-autoproxy/>	
 
<bean name="triangle" class="spy.service.Triangle">
<property name="name"  value="triangle name" />
</bean>
 
<bean name="circle" class="spy.service.Circle">
 
<property name="name"  value="circle name" />
</bean>
 
<bean name="shapeservice" class="spy.service.ShapeService" autowire="byName">
 
</bean>
 
<bean name="loggingAspect" class="spy.aspect.LoggingAspect"/>
 
</beans>

Aopmain.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package spy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spy.service.ShapeService;
public class Aopmain 
{
	public static void main(String[] args) 
	{
		ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
		ShapeService shapeservice=(ShapeService)context.getBean("shapeservice");
	                System.out.println(shapeservice.getCircle().getName());
	}
}

Pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.training</groupId>
  <artifactId>Spring-Aop</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
 
  <name>Spring-Aop</name>
  <url>http://maven.apache.org</url>
 
<repositories>
	<repository>
	<id>spring-milestone</id>
	<name>Spring Milestone Repository</name>
	<url>https://repo.spring.io/milestone</url>
	</repository>
</repositories>
 
 
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.4</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-core</artifactId>
	    <version>5.0.0.M5</version>
	</dependency>
 
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.2.5.RELEASE</version>
</dependency>
 
 
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.0.0.M3</version>
</dependency>
	<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjtools -->
 
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
          <source>1.4</source>
          <target>1.4</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Output:

Download Project developed using eclipse
Spring-Aop

Leave a Reply

Your email address will not be published. Required fields are marked *