diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/listener/JobListenerFactoryBean.java b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/JobListenerFactoryBean.java index acd827933..9fb962002 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/listener/JobListenerFactoryBean.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/JobListenerFactoryBean.java @@ -32,6 +32,7 @@ import org.springframework.batch.support.MethodInvoker; import org.springframework.batch.support.MethodInvokerUtils; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; +import org.springframework.core.Ordered; import org.springframework.util.Assert; /** @@ -107,8 +108,15 @@ public class JobListenerFactoryBean implements FactoryBean, InitializingBean { // be called ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(delegate); - proxyFactory.setInterfaces(new Class[] { JobExecutionListener.class }); - proxyFactory.addAdvisor(new DefaultPointcutAdvisor(new MethodInvokerMethodInterceptor(invokerMap))); + + boolean ordered = false; + if (delegate instanceof Ordered) { + ordered = true; + proxyFactory.addInterface(Ordered.class); + } + + proxyFactory.addInterface(JobExecutionListener.class); + proxyFactory.addAdvisor(new DefaultPointcutAdvisor(new MethodInvokerMethodInterceptor(invokerMap, ordered))); return proxyFactory.getProxy(); } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/listener/JobListenerFactoryBeanTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/listener/JobListenerFactoryBeanTests.java index d4b7ea297..756e5e9ff 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/listener/JobListenerFactoryBeanTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/listener/JobListenerFactoryBeanTests.java @@ -23,10 +23,12 @@ import java.util.Set; import org.junit.Before; import org.junit.Test; +import org.springframework.aop.framework.ProxyFactory; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobExecutionListener; import org.springframework.batch.core.annotation.AfterJob; import org.springframework.batch.core.annotation.BeforeJob; +import org.springframework.core.Ordered; /** * @author Lucas Ward @@ -74,6 +76,15 @@ public class JobListenerFactoryBeanTests { assertTrue(delegate.afterJobCalled); } + @Test + public void testVanillaInterfaceWithProxy() throws Exception { + JobListenerWithInterface delegate = new JobListenerWithInterface(); + ProxyFactory factory = new ProxyFactory(delegate); + factoryBean.setDelegate(factory.getProxy()); + Object listener = factoryBean.getObject(); + assertTrue(listener instanceof JobExecutionListener); + } + @Test public void testUseInHashSet() throws Exception { JobListenerWithInterface delegate = new JobListenerWithInterface(); @@ -102,6 +113,23 @@ public class JobListenerFactoryBeanTests { assertTrue(JobListenerFactoryBean.isListener(new JobListenerWithInterface())); } + @Test + public void testAnnotationsWithOrdered() throws Exception { + Object delegate = new Ordered() { + @SuppressWarnings("unused") + @BeforeJob + public void foo(JobExecution execution) { + } + + public int getOrder() { + return 3; + } + }; + JobExecutionListener listener = JobListenerFactoryBean.getListener(delegate); + assertTrue("Listener is not of correct type", listener instanceof Ordered); + assertEquals(3, ((Ordered) listener).getOrder()); + } + @Test public void testEqualityOfProxies() throws Exception { JobListenerWithInterface delegate = new JobListenerWithInterface();