Skip to main content

Spring’s AOP with Struts

Using Spring’s AOP with your Struts Application:

To tackle cross-cutting you can use Spring interceptors
To use Spring’s AOP in your Struts Applications you need to :

1. Create the interceptor.
2. Registor the interceptor.
3. Delclare interceptors.

For example
A sample logging interceptor
  
package net.champ.example1.employee.interceptors;

import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;

public class LoggingInterceptor implements MethodBeforeAdvice {

public void beforeMethod(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("logging before intersection");
}
}

This interceptor will execute the beforeMethod() before every intersection (We are using MethodBeforeAdvice). Here we are just printing a line but we can use it for anything we like. Next let us register the interceptor.
Registering the interceptor in the Spring config file
  
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="empSearchService" class="net.champ.example1.employee.business.EmpSearchServiceImpl">
<bean name="/submitSearch"
class="net.champ.example1.employee.actions.SubmitSearch">
<property name="empSearchService">
<ref bean="empSearchService">
</property>
</bean>

<!-- Interceptors -->
<bean name="logger"
class="net.champ.example1.employee.interceptors.LoggingInterceptor"/>
<!-- AutoProxies -->
<bean name="loggingAutoProxy"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>/submitSearch>/values>
</property>
<property name="interceptorNames">
<list>
<value>logger>/value>
</list>
</property>
</bean>

</beans>

Explanation:
First we have to register the interceptor:
 
<bean name="logger"
class="net.champ.example1.employee.interceptors.LoggingInterceptor"/>

Now we need to define the intersections, we will use autoproxy:

<bean name="loggingAutoProxy"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">

Now we have to register the Struts action’s that will be intercepted.

<property name="beanNames">
<value>/submitSearch>/values>
// <value>/otherBeans>/values>
</property>

Apply interceptors to the beanNames:

<property name="interceptorNames">
<list>
<value>logger>/value>
</list>
</property>

Comments

Popular posts from this blog

Sorting an List in Java

// Create a list String[] strArray = new String[] {"z", "a", "C"}; List list = Arrays.asList(strArray); // Sort Collections.sort(list); // C, a, z // Case-insensitive sort Collections.sort(list, String.CASE_INSENSITIVE_ORDER); // a, C, z // Reverse-order sort Collections.sort(list, Collections.reverseOrder()); // z, a, C // Case-insensitive reverse-order sort Collections.sort(list, String.CASE_INSENSITIVE_ORDER); Collections.reverse(list); // z, C, a

Linked List Example

/* * Copyright (c) 2000 David Flanagan. All rights reserved. * This code is from the book Java Examples in a Nutshell, 2nd Edition. * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied. * You may study, use, and modify it for any non-commercial purpose. * You may distribute it non-commercially as long as you retain this notice. * For a commercial use license, or to purchase the book (recommended), * visit http://www.davidflanagan.com/javaexamples2. */ /** * This class implements a linked list that can contain any type of object that * implements the nested Linkable interface. Note that the methods are all * synchronized, so that it can safely be used by multiple threads at the same * time. */ public class LinkedList { /** * This interface defines the methods required by any object that can be * linked into a linked list. */ public interface Linkable { public Linkable getNext(); // Returns the next element in the list public...