Skip to content

Commit d49cc44

Browse files
author
trisberg
committed
BATCH-63:added id and class attributes to <listener> element
1 parent 7cfafab commit d49cc44

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/StepParser.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,30 @@ private void handleListenersElement(Element element, RootBeanDefinition bd, Pars
271271
DomUtils.getChildElementsByTagName(listenersElement, "listener");
272272
if (listenerElements != null) {
273273
for (Element listenerElement : listenerElements) {
274+
String id = listenerElement.getAttribute("id");
274275
String listenerRef = listenerElement.getAttribute("ref");
275-
if (StringUtils.hasText(listenerRef)) {
276-
listenerRefs.add(listenerRef);
277-
RuntimeBeanReference bean = new RuntimeBeanReference(listenerRef);
278-
if (bean != null) {
279-
listenerBeans.add(bean);
280-
}
276+
String className = listenerElement.getAttribute("class");
277+
if ((StringUtils.hasText(id) || StringUtils.hasText(className))
278+
&& StringUtils.hasText(listenerRef)) {
279+
throw new BeanCreationException("Both 'id' or 'ref' plus 'class' specified; use 'class' with an optional 'id' or just 'ref' for <" + listenerElement.getTagName() + "> element with" + (StringUtils.hasText(id) ? " id=\"" + id + "\"" : "" ) + (StringUtils.hasText(className) ? " class=\"" + className + "\"" : "") + (StringUtils.hasText(listenerRef) ? " ref=\"" + listenerRef + "\"" : ""));
280+
}
281+
if (StringUtils.hasText(listenerRef)) {
282+
listenerRefs.add(listenerRef);
283+
BeanReference bean = new RuntimeBeanReference(listenerRef);
284+
listenerBeans.add(bean);
285+
}
286+
else if (StringUtils.hasText(className)) {
287+
RootBeanDefinition beanDef = new RootBeanDefinition(className, null, null);
288+
if (!StringUtils.hasText(id)) {
289+
id = parserContext.getReaderContext().generateBeanName(beanDef);
281290
}
291+
parserContext.getRegistry().registerBeanDefinition(id, beanDef);
292+
BeanReference bean = new RuntimeBeanReference(id);
293+
listenerBeans.add(bean);
294+
}
295+
else {
296+
throw new BeanCreationException("Neither 'ref' or 'class' specified for <" + listenerElement.getTagName() + "> element");
297+
}
282298
}
283299
}
284300
ManagedList arguments = new ManagedList();

spring-batch-core/src/main/resources/org/springframework/batch/core/configuration/xml/spring-batch-2.0.xsd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,9 @@
225225
</xsd:annotation>
226226
<xsd:complexType>
227227
<xsd:sequence>
228-
<xsd:element name="listener">
228+
<xsd:element name="listener" minOccurs="1" maxOccurs="unbounded">
229229
<xsd:complexType>
230+
<xsd:attribute name="id" type="xsd:ID"/>
230231
<xsd:attribute name="ref" type="xsd:string"/>
231232
<xsd:attribute name="class" type="xsd:string"/>
232233
</xsd:complexType>

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepWithChunkOrientedJobParserTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.batch.core.repository.JobRepository;
3030
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
3131
import org.springframework.beans.factory.annotation.Autowired;
32+
import org.springframework.beans.factory.annotation.Qualifier;
3233
import org.springframework.test.context.ContextConfiguration;
3334
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3435

@@ -51,6 +52,7 @@ public class StepWithChunkOrientedJobParserTests {
5152
private TestReader reader;
5253

5354
@Autowired
55+
@Qualifier("listener")
5456
private TestListener listener;
5557

5658
@Autowired

spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/StepWithChunkOrientedJobParserTests-context.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<step name="step1">
1111
<chunk-oriented reader="reader" processor="processor" writer="writer">
1212
<listeners>
13+
<listener class="org.springframework.batch.core.configuration.xml.TestListener"/>
1314
<listener ref="listener"/>
1415
</listeners>
1516
</chunk-oriented>

0 commit comments

Comments
 (0)