It seems as if the SpringBeanAutowiringInterceptor prevents lifecycle methods (postCreate and preDestroy) from being called
My original SLSB looked like this:
The problem was that I always received a NullPointerException when I created the session (highlighted in blue). Apparently the connection was not initialized. I found out that the method initConnection(), annotated with @PostConstruct was not called.Code:@Stateless @Local(IMessageHandler.class) @Interceptors(SpringBeanAutowiringInterceptor.class) public class MyMessageHandler implements IMessageHandler { @Resource(mappedName = "<queueName>") private Destination targetDestination; @Resource(mappedName = "<connFactoryName>") private ConnectionFactory factory; private Connection conn; public void dispatch(Object toDispatch) throws Exception { Session session = null; MessageProducer producer = null; try { session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE); producer = session.createProducer(targetDestination); Message message = createMessage(toDispatch, session); producer.send(message); } catch (JMSException e) { … // handle JMS exception } finally { … // close session and producer } } … @PostConstruct void initConnection() { try { conn = factory.createConnection(); } catch (JMSException e) { ... // handle JMS exception } } @PreDestroy void cleanUp() { try { conn.close(); } catch (JMSException e) { ... // handle JMS exception } } }
As soon as I removed the interceptor (highlighted in line 3), everything worked just fine.
My setup
applicationContext-messaging.xml contains a default implementation for the bean messageHandler (this session bean):This context is imported in serviceContext.xml and the bean reference for messageHandler is overwritten.Code:<beans …> <bean id="messageHandler" class="….MyMessageHandler" /> … </beans>And finally this is the application context loaded from within beanRefContext.xml:Code:<beans …> <import resource="classpath:applicationContext.xml"/> … <import resource="classpath:applicationContext-messaging.xml"/> … <!-- Override from JNDI --> … <jee:jndi-lookup id="batchProcessor" jndi-name="<jndiName>" /> … </beans>
With this I can do nearly all integration testing without the need for a JEE container or embedded microcontainer. The beans are only real EJBs when being loaded inside the EJB container.Code:<beans …> <bean id="serviceContext" class="org.springframework.context.support.ClassPathXmlApplicationContext"> <constructor-arg value="serviceContext.xml"/> </bean> </beans>
Is this an expected behavior? Did I misunderstand something with this setup?
Thank you,
Kariem


Reply With Quote
