Place holder resolution in @JmsListener arguments
This commit allows to use place holder definitions for JmsListener attributes, effectively allowing to externalize those settings from the code. Issue: SPR-12134
This commit is contained in:
@@ -29,6 +29,7 @@ import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.SmartInitializingSingleton;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.jms.config.JmsListenerConfigUtils;
|
||||
@@ -218,19 +219,19 @@ public class JmsListenerAnnotationBeanPostProcessor
|
||||
endpoint.setMethod(method);
|
||||
endpoint.setMessageHandlerMethodFactory(this.messageHandlerMethodFactory);
|
||||
endpoint.setId(getEndpointId(jmsListener));
|
||||
endpoint.setDestination(jmsListener.destination());
|
||||
endpoint.setDestination(resolve(jmsListener.destination()));
|
||||
if (StringUtils.hasText(jmsListener.selector())) {
|
||||
endpoint.setSelector(jmsListener.selector());
|
||||
endpoint.setSelector(resolve(jmsListener.selector()));
|
||||
}
|
||||
if (StringUtils.hasText(jmsListener.subscription())) {
|
||||
endpoint.setSubscription(jmsListener.subscription());
|
||||
endpoint.setSubscription(resolve(jmsListener.subscription()));
|
||||
}
|
||||
if (StringUtils.hasText(jmsListener.concurrency())) {
|
||||
endpoint.setConcurrency(jmsListener.concurrency());
|
||||
endpoint.setConcurrency(resolve(jmsListener.concurrency()));
|
||||
}
|
||||
|
||||
JmsListenerContainerFactory<?> factory = null;
|
||||
String containerFactoryBeanName = jmsListener.containerFactory();
|
||||
String containerFactoryBeanName = resolve(jmsListener.containerFactory());
|
||||
if (StringUtils.hasText(containerFactoryBeanName)) {
|
||||
Assert.state(this.beanFactory != null, "BeanFactory must be set to obtain container factory by bean name");
|
||||
try {
|
||||
@@ -248,13 +249,25 @@ public class JmsListenerAnnotationBeanPostProcessor
|
||||
|
||||
private String getEndpointId(JmsListener jmsListener) {
|
||||
if (StringUtils.hasText(jmsListener.id())) {
|
||||
return jmsListener.id();
|
||||
return resolve(jmsListener.id());
|
||||
}
|
||||
else {
|
||||
return "org.springframework.jms.JmsListenerEndpointContainer#" + counter.getAndIncrement();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the specified value if possible.
|
||||
*
|
||||
* @see ConfigurableBeanFactory#resolveEmbeddedValue
|
||||
*/
|
||||
private String resolve(String value) {
|
||||
if (this.beanFactory != null && this.beanFactory instanceof ConfigurableBeanFactory) {
|
||||
return ((ConfigurableBeanFactory) this.beanFactory).resolveEmbeddedValue(value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A {@link MessageHandlerMethodFactory} adapter that offers a configurable underlying
|
||||
|
||||
@@ -55,6 +55,9 @@ public abstract class AbstractJmsAnnotationDrivenTests {
|
||||
@Test
|
||||
public abstract void fullConfiguration();
|
||||
|
||||
@Test
|
||||
public abstract void fullConfigurableConfiguration();
|
||||
|
||||
@Test
|
||||
public abstract void customConfiguration();
|
||||
|
||||
@@ -107,13 +110,25 @@ public abstract class AbstractJmsAnnotationDrivenTests {
|
||||
assertEquals("queueIn", endpoint.getDestination());
|
||||
assertEquals("mySelector", endpoint.getSelector());
|
||||
assertEquals("mySubscription", endpoint.getSubscription());
|
||||
assertEquals("1-10", endpoint.getConcurrency());
|
||||
}
|
||||
|
||||
@Component
|
||||
static class FullBean {
|
||||
|
||||
@JmsListener(id = "listener1", containerFactory = "simpleFactory", destination = "queueIn",
|
||||
selector = "mySelector", subscription = "mySubscription")
|
||||
selector = "mySelector", subscription = "mySubscription", concurrency = "1-10")
|
||||
public String fullHandle(String msg) {
|
||||
return "reply";
|
||||
}
|
||||
}
|
||||
|
||||
@Component
|
||||
static class FullConfigurableBean {
|
||||
|
||||
@JmsListener(id = "${jms.listener.id}", containerFactory = "${jms.listener.containerFactory}",
|
||||
destination = "${jms.listener.destination}", selector = "${jms.listener.selector}",
|
||||
subscription = "${jms.listener.subscription}", concurrency = "${jms.listener.concurrency}")
|
||||
public String fullHandle(String msg) {
|
||||
return "reply";
|
||||
}
|
||||
|
||||
@@ -51,6 +51,13 @@ public class AnnotationDrivenNamespaceTests extends AbstractJmsAnnotationDrivenT
|
||||
testFullConfiguration(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fullConfigurableConfiguration() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"annotation-driven-full-configurable-config.xml", getClass());
|
||||
testFullConfiguration(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Test
|
||||
public void customConfiguration() {
|
||||
|
||||
@@ -29,6 +29,8 @@ import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
|
||||
import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory;
|
||||
import org.springframework.jms.config.JmsListenerContainerTestFactory;
|
||||
@@ -63,6 +65,13 @@ public class EnableJmsTests extends AbstractJmsAnnotationDrivenTests {
|
||||
testFullConfiguration(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fullConfigurableConfiguration() {
|
||||
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
EnableJmsFullConfigurableConfig.class, FullConfigurableBean.class);
|
||||
testFullConfiguration(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Test
|
||||
public void customConfiguration() {
|
||||
@@ -131,6 +140,22 @@ public class EnableJmsTests extends AbstractJmsAnnotationDrivenTests {
|
||||
}
|
||||
}
|
||||
|
||||
@EnableJms
|
||||
@Configuration
|
||||
@PropertySource("classpath:/org/springframework/jms/annotation/jms-listener.properties")
|
||||
static class EnableJmsFullConfigurableConfig {
|
||||
|
||||
@Bean
|
||||
public JmsListenerContainerTestFactory simpleFactory() {
|
||||
return new JmsListenerContainerTestFactory();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
|
||||
return new PropertySourcesPlaceholderConfigurer();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableJms
|
||||
static class EnableJmsCustomConfig implements JmsListenerConfigurer {
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:jms="http://www.springframework.org/schema/jms"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/jms
|
||||
http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<jms:annotation-driven/>
|
||||
|
||||
<bean class="org.springframework.jms.annotation.AbstractJmsAnnotationDrivenTests$FullConfigurableBean"/>
|
||||
|
||||
<bean id="simpleFactory" class="org.springframework.jms.config.JmsListenerContainerTestFactory"/>
|
||||
|
||||
<context:property-placeholder location="classpath:org/springframework/jms/annotation/jms-listener.properties"/>
|
||||
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,6 @@
|
||||
jms.listener.id=listener1
|
||||
jms.listener.containerFactory=simpleFactory
|
||||
jms.listener.destination=queueIn
|
||||
jms.listener.selector=mySelector
|
||||
jms.listener.subscription=mySubscription
|
||||
jms.listener.concurrency=1-10
|
||||
Reference in New Issue
Block a user