Add alternate constructor to MessageListenerAdapter

As a convenience when configuring with Java, added another constructor
call that allows setting the delegate and the default listener method
in one line of code.

```java
	public MessageListenerAdapter(Object delegate, String defaultListenerMethod) {
		this(delegate);
		setDefaultListenerMethod(defaultListenerMethod);
	}
```

This supports configuring a POJO-based listener with a single step:

```java
	@Test
	public void testCustomMethodWithAlternateConstructor() throws Exception {
		MessageListenerAdapter adapter = new MessageListenerAdapter(target, "customMethod");
		adapter.afterPropertiesSet();

		adapter.onMessage(STRING_MSG, null);

		verify(target).customMethod(PAYLOAD);
	}
```

A pure Java configuration bean can now look like this:
```java
	 @Bean
	 MessageListenerAdapter listenerAdapter(Receiver receiver) {
		 return new new MessageListenerAdapter(receiver, "pojoMethod");
	 }
	 @Bean
	 Receiver receiver() {
		 return new Receiver();
	 }
```
No longer do we have to A) assign the adapter to a variable and B) call
setDefaultListenerMethod, slimming down pure POJO configuration with pure
Java.
This commit is contained in:
Greg Turnquist
2013-05-31 11:16:31 -04:00
parent 7c8dafce67
commit 49dc960ab2
2 changed files with 36 additions and 2 deletions

View File

@@ -105,6 +105,7 @@ import org.springframework.util.StringUtils;
*
* @author Juergen Hoeller
* @author Costin Leau
* @author Greg Turnquist
* @see org.springframework.jms.listener.adapter.MessageListenerAdapter
*/
public class MessageListenerAdapter implements InitializingBean, MessageListener {
@@ -206,6 +207,19 @@ public class MessageListenerAdapter implements InitializingBean, MessageListener
initDefaultStrategies();
setDelegate(delegate);
}
/**
* Create a new {@link MessageListenerAdapter} for the given delegate.
*
* @param delegate the delegate object
* @param defaultListenerMethod method to call when a message comes
*
* @see #getListenerMethodName
*/
public MessageListenerAdapter(Object delegate, String defaultListenerMethod) {
this(delegate);
setDefaultListenerMethod(defaultListenerMethod);
}
/**
* Set a target object to delegate message listening to. Specified listener