Skip to content

Commit a92933e

Browse files
jmaxwellartembilan
jmaxwell
authored andcommitted
INT-4427 add value attribute to the @publisher
JIRA: https://jira.spring.io/browse/INT-4427
1 parent f479270 commit a92933e

File tree

2 files changed

+158
-22
lines changed

2 files changed

+158
-22
lines changed

spring-integration-core/src/main/java/org/springframework/integration/annotation/Publisher.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,16 +21,16 @@
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
2323

24+
import org.springframework.core.annotation.AliasFor;
25+
2426
/**
25-
* Annotation to indicate that a method, or all public methods if applied at
26-
* class-level, should publish Messages.
27+
* Annotation to indicate that a method, or all public methods if applied at class-level,
28+
* should publish Messages.
2729
* <p>
2830
* By default, the Message will be constructed from the return value of the method
29-
* invocation
30-
* and sent to a channel specified by the {@link #channel()} attribute.
31-
* However, a combination of both @Payload and @Header annotations
32-
* can be used to further manage the message structure. See the reference manual for
33-
* examples.
31+
* invocation and sent to a channel specified by the {@link #channel()} attribute.
32+
* However, a combination of both @Payload and @Header annotations can be used to further
33+
* manage the message structure. See the reference manual for examples.
3434
* <p>
3535
* Note: unlike @Gateway, this annotation is used to generate an AOP Advice for an
3636
* existing service and its method implementation. The message sending is a side effect
@@ -40,17 +40,28 @@
4040
* The XML equivalent is {@code <int:publishing-interceptor>}
4141
*
4242
* @author Mark Fisher
43+
* @author Jeff Maxwell
4344
*
4445
* @since 2.0
46+
*
4547
* @see org.springframework.integration.aop.MessagePublishingInterceptor
4648
*/
47-
@Target({ElementType.METHOD, ElementType.TYPE})
49+
@Target({ ElementType.METHOD, ElementType.TYPE })
4850
@Retention(RetentionPolicy.RUNTIME)
4951
public @interface Publisher {
5052

53+
/**
54+
* Alias for the {@link #channel()} attribute.
55+
* @return The name of the Message Channel to which Messages will be published.
56+
* @since 5.0.4
57+
*/
58+
@AliasFor("channel")
59+
String value() default "";
60+
5161
/**
5262
* @return The name of the Message Channel to which Messages will be published.
5363
*/
64+
@AliasFor("value")
5465
String channel() default "";
5566

5667
}

spring-integration-core/src/test/java/org/springframework/integration/aop/PublisherAnnotationAdvisorTests.java

Lines changed: 138 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,27 +29,27 @@
2929

3030
import org.springframework.aop.framework.ProxyFactory;
3131
import org.springframework.context.support.StaticApplicationContext;
32-
import org.springframework.messaging.Message;
33-
import org.springframework.messaging.handler.annotation.Payload;
3432
import org.springframework.integration.annotation.Publisher;
3533
import org.springframework.integration.channel.QueueChannel;
34+
import org.springframework.messaging.Message;
35+
import org.springframework.messaging.handler.annotation.Payload;
3636

3737
/**
3838
* @author Mark Fisher
39+
* @author Jeff Maxwell
40+
*
3941
* @since 2.0
4042
*/
4143
public class PublisherAnnotationAdvisorTests {
4244

4345
private final StaticApplicationContext context = new StaticApplicationContext();
4446

45-
4647
@Before
4748
public void setup() {
4849
context.registerSingleton("testChannel", QueueChannel.class);
4950
context.registerSingleton("testMetaChannel", QueueChannel.class);
5051
}
5152

52-
5353
@Test
5454
public void annotationAtMethodLevelOnVoidReturnWithParamAnnotation() {
5555
PublisherAnnotationAdvisor advisor = new PublisherAnnotationAdvisor();
@@ -120,65 +120,190 @@ public void metaAnnotationAtClassLevel() {
120120
assertEquals("foo", message.getPayload());
121121
}
122122

123+
@Test
124+
public void annotationViaValueAtMethodLevelOnVoidReturnWithParamAnnotation() {
125+
PublisherAnnotationAdvisor advisor = new PublisherAnnotationAdvisor();
126+
advisor.setBeanFactory(context);
127+
QueueChannel testChannel = context.getBean("testChannel", QueueChannel.class);
128+
ProxyFactory pf = new ProxyFactory(new AnnotationViaValueAtMethodLevelTestBeanImpl());
129+
pf.addAdvisor(advisor);
130+
TestVoidBean proxy = (TestVoidBean) pf.getProxy();
131+
proxy.testVoidMethod("foo");
132+
Message<?> message = testChannel.receive(0);
133+
assertNotNull(message);
134+
assertEquals("foo", message.getPayload());
135+
}
136+
137+
@Test
138+
public void annotationViaValueAtMethodLevel() {
139+
PublisherAnnotationAdvisor advisor = new PublisherAnnotationAdvisor();
140+
advisor.setBeanFactory(context);
141+
QueueChannel testChannel = context.getBean("testChannel", QueueChannel.class);
142+
ProxyFactory pf = new ProxyFactory(new AnnotationViaValueAtMethodLevelTestBeanImpl());
143+
pf.addAdvisor(advisor);
144+
TestBean proxy = (TestBean) pf.getProxy();
145+
proxy.test();
146+
Message<?> message = testChannel.receive(0);
147+
assertNotNull(message);
148+
assertEquals("foo", message.getPayload());
149+
}
150+
151+
@Test
152+
public void annotationViaValueAtClassLevel() {
153+
PublisherAnnotationAdvisor advisor = new PublisherAnnotationAdvisor();
154+
advisor.setBeanFactory(context);
155+
QueueChannel testChannel = context.getBean("testChannel", QueueChannel.class);
156+
ProxyFactory pf = new ProxyFactory(new AnnotationViaValueAtClassLevelTestBeanImpl());
157+
pf.addAdvisor(advisor);
158+
TestBean proxy = (TestBean) pf.getProxy();
159+
proxy.test();
160+
Message<?> message = testChannel.receive(0);
161+
assertNotNull(message);
162+
assertEquals("foo", message.getPayload());
163+
}
164+
165+
@Test
166+
public void metaAnnotationViaValueAtMethodLevel() {
167+
PublisherAnnotationAdvisor advisor = new PublisherAnnotationAdvisor();
168+
advisor.setBeanFactory(context);
169+
QueueChannel testMetaChannel = context.getBean("testMetaChannel", QueueChannel.class);
170+
ProxyFactory pf = new ProxyFactory(new MetaAnnotationViaValueAtMethodLevelTestBeanImpl());
171+
pf.addAdvisor(advisor);
172+
TestBean proxy = (TestBean) pf.getProxy();
173+
proxy.test();
174+
Message<?> message = testMetaChannel.receive(0);
175+
assertNotNull(message);
176+
assertEquals("foo", message.getPayload());
177+
}
178+
179+
@Test
180+
public void metaAnnotationViaValueAtClassLevel() {
181+
PublisherAnnotationAdvisor advisor = new PublisherAnnotationAdvisor();
182+
advisor.setBeanFactory(context);
183+
QueueChannel testMetaChannel = context.getBean("testMetaChannel", QueueChannel.class);
184+
ProxyFactory pf = new ProxyFactory(new MetaAnnotationViaValueAtClassLevelTestBeanImpl());
185+
pf.addAdvisor(advisor);
186+
TestBean proxy = (TestBean) pf.getProxy();
187+
proxy.test();
188+
Message<?> message = testMetaChannel.receive(0);
189+
assertNotNull(message);
190+
assertEquals("foo", message.getPayload());
191+
}
123192

124193
interface TestBean {
125194

126195
String test();
127196

128197
}
129198

130-
131199
interface TestVoidBean {
132200

133201
void testVoidMethod(String s);
134202

135203
}
136204

137-
138205
static class AnnotationAtMethodLevelTestBeanImpl implements TestBean, TestVoidBean {
139206

207+
@Override
140208
@Publisher(channel = "testChannel")
141209
public String test() {
142210
return "foo";
143211
}
144212

213+
@Override
145214
@Publisher(channel = "testChannel")
146-
public void testVoidMethod(@Payload String s) { }
147-
}
215+
public void testVoidMethod(@Payload String s) {
216+
}
148217

218+
}
149219

150220
@Publisher(channel = "testChannel")
151221
static class AnnotationAtClassLevelTestBeanImpl implements TestBean {
152222

223+
@Override
153224
public String test() {
154225
return "foo";
155226
}
156227

157228
}
158229

159-
160-
@Target({ElementType.METHOD, ElementType.TYPE})
230+
@Target({ ElementType.METHOD, ElementType.TYPE })
161231
@Retention(RetentionPolicy.RUNTIME)
162232
@Publisher(channel = "testMetaChannel")
163233
public @interface TestMetaPublisher {
164-
}
165234

235+
}
166236

167237
static class MetaAnnotationAtMethodLevelTestBeanImpl implements TestBean {
168238

239+
@Override
169240
@TestMetaPublisher
170241
public String test() {
171242
return "foo";
172243
}
173-
}
174244

245+
}
175246

176247
@TestMetaPublisher
177248
static class MetaAnnotationAtClassLevelTestBeanImpl implements TestBean {
178249

250+
@Override
251+
public String test() {
252+
return "foo";
253+
}
254+
255+
}
256+
257+
static class AnnotationViaValueAtMethodLevelTestBeanImpl implements TestBean, TestVoidBean {
258+
259+
@Override
260+
@Publisher("testChannel")
179261
public String test() {
180262
return "foo";
181263
}
264+
265+
@Override
266+
@Publisher("testChannel")
267+
public void testVoidMethod(@Payload String s) {
268+
}
269+
270+
}
271+
272+
@Publisher("testChannel")
273+
static class AnnotationViaValueAtClassLevelTestBeanImpl implements TestBean {
274+
275+
@Override
276+
public String test() {
277+
return "foo";
278+
}
279+
280+
}
281+
282+
@Target({ ElementType.METHOD, ElementType.TYPE })
283+
@Retention(RetentionPolicy.RUNTIME)
284+
@Publisher("testMetaChannel")
285+
public @interface TestMetaPublisherViaValue {
286+
287+
}
288+
289+
static class MetaAnnotationViaValueAtMethodLevelTestBeanImpl implements TestBean {
290+
291+
@Override
292+
@TestMetaPublisherViaValue
293+
public String test() {
294+
return "foo";
295+
}
296+
297+
}
298+
299+
@TestMetaPublisherViaValue
300+
static class MetaAnnotationViaValueAtClassLevelTestBeanImpl implements TestBean {
301+
302+
@Override
303+
public String test() {
304+
return "foo";
305+
}
306+
182307
}
183308

184309
}

0 commit comments

Comments
 (0)