JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Spring AOP Advices - Around advice example - xml based configuration


In Spring, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. AOP forms a basis for aspect-oriented software development.

In this page you will see an example for Spring AOP - Around advice. It combines all three advices (before advice, after returning advice and around advice) during method execution.

pom.xml file gives all required dependencies:

<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>SpringJavaBasedConfig</groupId>
	<artifactId>SpringJavaBasedConfig</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<properties>
		<spring.version>3.2.0.RELEASE</spring.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>3.1</version>
		</dependency>
	</dependencies>
</project>

My business logic service class:

package com.java2novice.bean;

public class MyBusinessService {

	public void runMyBusinessLogic(){
		System.out.println("************************************");
		System.out.println("Running business logic...");
		System.out.println("************************************");
	}
	
	public void testThrowException() {
		throw new NullPointerException();
	}
}

Now create "Around Advice". Create a class which implements MethodInterceptor interface. You must call Object result = metInvocation.proceed() method to proceed on the original method execution, else the original method will not execute.

package com.java2novice.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class ExecuteAroundMethod implements MethodInterceptor{

	@Override
	public Object invoke(MethodInvocation metInvocation) throws Throwable {
		
		System.out.println("Inside RunBeforeExecution.before() method...");
		System.out.println("Running before advice...");
		try{
			Object result = metInvocation.proceed();
			
			System.out.println("Inside RunAfterExecution.afterReturning() method...");
			System.out.println("Running after advice...");
			
			return result;
		} catch(NullPointerException ne){
			//this is for ThrowsAdvice
			throw ne;
		}
		
	}

}

Here is the xml based configuration file. Add bean entry for ExecuteAroundMethod class (around advice class). Also create a proxy for MyBusinessService class. In this proxy configuration, you should add two properties called 'target' and 'interceptorNames'. 'target' defines in which bean you want to introduce advice. 'interceptorNames' defines that which advice class you want to introduce to the said target.

<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">
	
    <bean id="busService" class="com.java2novice.bean.MyBusinessService" />
	<bean id="aroundThrow" class="com.java2novice.aop.ExecuteAroundMethod" />
    <bean id="busServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean" >
        <property name="target" ref="busService" />
        <property name="interceptorNames">
			<list>
				<value>aroundThrow</value>
			</list>
		</property>
    </bean>
</beans>

Here is the final demo class: Note that we are calling proxy bean object, not the business service bean directly.

package com.java2novice.test;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.java2novice.bean.MyBusinessService;

public class SpringDemo {

	public static void main(String a[]){

		String confFile = "applicationContext.xml";
		ConfigurableApplicationContext context 
								= new ClassPathXmlApplicationContext(confFile);
		MyBusinessService busServ = (MyBusinessService) context.getBean("busServiceProxy");
		busServ.runMyBusinessLogic();
	}
}

Output:
Inside RunBeforeExecution.before() method...
Running before advice...
************************************
Running business logic...
************************************
Inside RunAfterExecution.afterReturning() method...
Running after advice...
<< Previous Program | Next Program >>
blog comments powered by Disqus

Spring framework examples

  1. Spring 3 hello world example
  2. Spring bean java based configuration using @Configuration and @Bean
  3. How to get spring application context object reference?
  4. How to load multiple spring bean configuration files?
  5. Spring java based configuration @Import example
  6. Spring Dependency Injection and Types
  7. Spring Dependency Injection via setter method
  8. Spring Dependency Injection via Constructor
  9. Constructor overloading issue with spring constructor injection
  10. Constructor vs Setter dependency Injection in Spring
  11. How to inject value into spring bean instance variables?
  12. Spring bean tag properties
  13. Differen types of spring bean scopes
  14. How to inject inner bean in spring?
  15. Set spring bean scope using annotation
  16. How to invoke spring bean init and destroy methods?
  17. Spring bean initialization callback
  18. Spring bean destruction callback
  19. Configure default initialization and destroy method in all spring beans
  20. Spring bean init and destroy methods using annotations
  21. Spring Bean Post Processors
  22. How to read property file in spring using xml based configuration file?
  23. How to read property file in spring 3.0 using java based configuration?
  24. How to inject date into spring bean property?
  25. How to inject date into spring bean with CustomDateEditor?
  26. Spring bean inheritance configuration
  27. Spring dependency checking with @Required annotation
  28. How to define a custom Required-style annotation for dependency checking?
  29. How to inject List into spring bean?
  30. How to inject Set into spring bean?
  31. How to inject Map into spring bean?
  32. How to enable auto component scanning in spring?
  33. Difference between @Component, @Service, @Repository and @Controller
  34. How to filter components in auto scanning?
  35. Spring expression language basic example using xml based configuration.
  36. Spring expression language basic example using annotations.
  37. Bean reference example using spring expression language
  38. Spring expression language operators example
  39. Spring expression language ternary operator example
  40. How to use regular expressions with spring expression language?
  41. How to use collections with spring expression language?
  42. Spring bean auto-wiring modes
  43. Spring auto-wiring mode byName
  44. Spring auto-wiring mode byType
  45. Spring auto-wiring mode constructor
  46. Spring auto-wiring using @Autowired annotation example
  47. Spring auto-wiring using @Qualifier annotation example
  48. Spring log4j configuration
  49. How to schedule jobs using @Scheduled annotation in spring?
  50. Send E-mail using spring 3
  51. Send E-mail with attachment using spring 3
  52. Simple spring JDBC example
  53. Spring JDBC example with JdbcTemplate
  54. Spring JDBC example with JdbcDaoSupport
  55. Spring JDBC query example using JdbcDaoSupport
  56. How to query single column using spring JdbcTemplate?
  57. Spring JDBC batch updates using JdbcTemplate?
  58. Spring AOP Advices - Before advice example - xml based configuration
  59. Spring AOP Advices - After returning advice example - xml based configuration
  60. Spring AOP Advices - After throwing advice example - xml based configuration
  61. Spring AOP Advices - Around advice example - xml based configuration
  62. Spring AOP Advice - Pointcuts – Name match example
  63. Spring AOP Advice - Pointcuts – Regular expression example
  64. Spring AOP - AspectJ - @Before example
  65. Spring AOP - AspectJ - @After example
  66. Spring AOP - AspectJ - @AfterReturning example
  67. Spring AOP - AspectJ - @AfterThrowing example
  68. Spring AOP - AspectJ - @Around example
Knowledge Centre
Can you list serialization methods?
Serialization interface does not have any methods. It is a marker interface. It just tells the JVM that your class can be serializable.
Famous Quotations
There is a great difference between worry and concern. A worried person sees a problem, and a concerned person solves a problem.
-- Harold Stephens

About Author

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.