Advance Java Practical Journal
ISBN 9788119221226

Highlights

Notes

  

Annexure – III: Steps for Executing Spring AOP program using Eclipse

    1. Create new Project

Include Following Jar file in Project

Define the following Target Class

package com.spring.aop.service;

public class EmployeeService {

public String addEmployee()

{

System.out.println(“Add Employee”);

return “Employee Information is added successfully”;

}

public void modifyEmployee()

{

System.out.println(“Update or Modify Employee Information”);

}

public void deleteEmployee()

{

System.out.println(“Delete Employee Information”);

}

}

Define Configuration file in different sub-package

package com.spring.aop.config;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import com.spring.aop.service.EmployeeService;

@Configuration

@ComponentScan(basePackages=“com.spring.aop”)

public class EmployeeConfig {

@Bean(name=“empService”)

public EmployeeService getBean()

{

return new EmployeeService();

}

}

Then Define the Log file to implements various Aspect’s Advices

package com.spring.aop.aspect;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.springframework.context.annotation.EnableAspectJAutoProxy;

import org.springframework.stereotype.Component;

@Aspect

@Component

@EnableAspectJAutoProxy

public class LogginAspect {

@Before(“execution(* com.spring.aop.EmployeeService.addEmployee())”)

public void logBefore(JoinPoint joinPoint)

{

System.out.println(“LogBefore() is running”);

System.out.println(“, Before “+joinPoint.getSignature().getName()+”method”);

}

@After(“execution(* com.spring.aop.EmployeeService.addEmployee())”)

public void logAfter(JoinPoint joinPoint)

{

System.out.println(“LogAfter() is running”);

System.out.println(“, after “+joinPoint.getSignature().getName()+”method”);

}

}

Above Code is for Aspect oriented Advices declaration as a Log file

Define Main Code to execute

package com.spring.aop.test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.spring.aop.config.EmployeeConfig;

import com.spring.aop.service.EmployeeService;

public class MainApp {

public static void main(String[] args) {

// TODO Auto-generated method stub

ApplicationContext context;

context = new AnnotationConfigApplicationContext(EmployeeConfig.class);

EmployeeService emp = (EmployeeService)context.getBean(EmployeeService.class);

emp.addEmployee();

emp.modifyEmployee();

emp.deleteEmployee();

}

}

package com.spring.aop.service;

public class EmployeeService {

public String employeeService;

public String addEmployee()

{

System.out.println(“Add Employee”);

return “Employee Information is added Successfully”;

}

public void modifyEmployee()

{

System.out.println(“Modify Employee”);

}

public void deleteEmployee()

{

System.out.println(“Delete Employee”);

}

}

package com.spring.aop.config;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import com.spring.aop.service.EmployeeService;

@Configuration

@ComponentScan(basePackages=“com.spring.aop”)

public class EmployeeConfig {

@Bean(name=“employeeService”)

public EmployeeService getBean()

{

return new EmployeeService();

}

}

LogginFile

package com.spring.aop.aspect;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

import org.springframework.context.annotation.EnableAspectJAutoProxy;

import org.springframework.stereotype.Component;

@Aspect

@Component

@EnableAspectJAutoProxy

public class LogginAspect {

@Before(“execution(* com.spring.aop.service.EmployeeService.addEmployee())”)

public void logBefore(JoinPoint joinPoint)

{

System.out.println(“LogBefore() is running”);

System.out.println(“, Before”+joinPoint.getSignature().getName()+”method”);

}

@After(“execution(* com.spring.aop.service.EmployeeService.addEmployee())”)

public void logAfter(JoinPoint joinPoint)

{

System.out.println(“LogAfter() is running”);

System.out.println(“, After”+joinPoint.getSignature().getName()+”method”);

}

@Pointcut(“execution(* com.spring.aop.service.EmployeeService.addEmployee())”)

public void addEmployee()

{

}

/*

@AfterReturning(pointcut =“com.spring.aop.service.EmployeeService.addEmployee()”,returning=“result”)

public void logAfterReturning(JoinPoint joinPoint, Object result)

{

System.out.println(“logAfterReturning is running”);

System.out.println(“,after “+joinPoint.getSignature().getName()+”method”);

System.out.println(“Method returned value is =“+result);

}

*/

@Around(“execution(* com.spring.aop.service.EmployeeService.modifyEmployee())”)

public void aroundAdvice(ProceedingJoinPoint pjp) throws Throwable

{

System.out.println(“Around advice Initial Message”);

pjp.proceed();

System.out.println(“Around Advice later message”);

}

}

package com.spring.aop.test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.spring.aop.config.EmployeeConfig;

import com.spring.aop.service.EmployeeService;

public class EmployeeTest {

public static ApplicationContext context;

public static void main(String[] args) {

// TODO Auto-generated method stub

context = new AnnotationConfigApplicationContext(EmployeeConfig.class);

EmployeeService employeeService = (EmployeeService)context.getBean(EmployeeService.class);

employeeService.addEmployee();

employeeService.modifyEmployee();

employeeService.deleteEmployee();

}

}

@After(“execution(* com.spring.aop.service.EmployeeService.vikas())”)

public void logAmit(JoinPoint joinPoint)

{

System.out.println(“Hi This is Amit! Waiting for Vikas to learn Spring”);

System.out.println(“, After “+joinPoint.getSignature().getName());

}

What do you mean by PointCut in AOP and explain with example code.

Ans.

In Aspect Oriented Programming the aspects means an operation applies to all methods of target class.

But in some situation we do not want to apply aspects to certain method definition (or in terms of AOP we can say that joinPoint)

By using Simple business logic it is so complicated to implements Aspect or instrctions.

PointCut is nothing but collection of joinPoints(or methods definition in Java code) on which we has to provides (ADVICES) using aspects annotation.

We have to kinds of Point Cuts

1.Static pointcut: - It specify target class method name while declaring or mentioning pointcuts in our target class coding

2.Dynamic PointCut:- It is called during processing of target class method and pass the proxy objects at runtime.

package org.hiray.aspectj;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.Before;

public class TargetClass {

public void m1() //joinPoint

{

}

@After(“m4()”)

public void m2() //joinPoint

{

}

@Before(“m1()”)

public void m3() //joinPoint

{

}

public void m4() //joinPoint

{

}

public void m5() //joinPoint

{

}

}

Define another Target class

package org.hiray.aspectj;

public class Runner {

String name;

int age;

public String getName() {

return name;

}

public int getAge() {

return age;

}

public void setName(String name) {

this.name = name;

}

public void setAge(int age) {

this.age = age;

}

public void runningBegins()

{

System.out.println(“Runner started Running marathon”);

}

public void runningEnds()

{

System.out.println(“Runner finished the marathon”);

}

}

ASPECT CLASS (Logging Class to define pointcuts)

package org.hiray.aspectj;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

import org.springframework.context.annotation.EnableAspectJAutoProxy;

import org.springframework.stereotype.Component;

@Aspect

@Component

@EnableAspectJAutoProxy

public class RunnerDetails {

@Pointcut(“execution(public void Runner.runningBegins())”)

public void beginsPointCut1()

{

}

@Pointcut(“execution(public void Runner.runningBegins())”)

public void endPointCut1()

{

}

@Before(“beginspointCut1()”)

public void begins(JoinPoint jp)

{

System.out.println(“ In the begins method”);

System.out.println(“ JointPoint-1 “+jp.getSignature().getName());

}

@AfterReturning(“public void endPointCut1()”)

public void end(JoinPoint jp)

{

System.out.println(“JointPoint-2”+jp.getSignature().getName());

System.out.println(“Running End”);

}

}

Configuration 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

xmlns:context=http://www.springframework.org/schema/context

xmlns:lang=http://www.springframework.org/schema/lang

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-4.3.xsd

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

http://www.springframework.org/schema/context/spring-context.xsd

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

http://www.springframework.org/schema/lang/spring-lang-4.3.xsd>

<bean id=“runner” class=“org.hiray.aspectj.Runner”>

<property name=“name” value=“Subh”/>

<property name=“age” value=“22”/>

</bean>

<bean id=“rd” class=“org.hiray.aspectj.RunnerDetails”>

</bean>

</beans>

Main Program

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

xmlns:lang=http://www.springframework.org/schema/lang

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-4.3.xsd

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

http://www.springframework.org/schema/context/spring-context.xsd

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

http://www.springframework.org/schema/lang/spring-lang-4.3.xsd>

<bean id=“runner” class=“org.hiray.aspectj.Runner”>

<property name=“name” value=“Subh”/>

<property name=“age” value=“22”/>

</bean>

<bean id=“rd” class=“org.hiray.aspectj.RunnerDetails”>

</bean>

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

<aop:config></aop:config>

</beans>

package org.hiray.aspectj;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

@SuppressWarnings(“resource”)

public static void main(String[] args) {

// TODO Auto-generated method stub

ApplicationContext context;

context = new ClassPathXmlApplicationContext(“bean.xml”);

Runner r = (Runner)context.getBean(Runner.class);

System.out.println(“Name “+r.getName());

System.out.println(“Age “+r.getAge());

r.runningBegins();

r.runningEnds();

}

}