I have problem in Mule when I was using custom tcp protocol and inside the custom protocol has a spring dependency injection using @Autowired annotation.
CustomProtocol.java
public class ContentLengthProtocol extends AbstractByteProtocol{
@Autowired
private Adapter adapter;
@Lookup("atm-inbound")
private ImmutableEndpoint inboundEndpoint;
public ContentLengthProtocol(){
super(true);
}
public Object read(InputStream is) throws IOException{
// do some reading
}
}
Mule Configuration snippet
<spring:beans>
<spring:bean id="adapter" class="id.company.dao.Adapter"/>
<spring:bean id="contentLengthProtocol" class="id.company.protocol.ContentLengthProtocol"/>
</spring:beans>
<tcp:connector name="TCPConnector" validateConnections="true" sendBufferSize="0" receiveBufferSize="1024" receiveBacklog="50" reuseAddress="true" keepAlive="true" clientSoTimeout="0" serverSoTimeout="0" socketSoLinger="0" doc:name="TCPConnector">
<tcp:custom-protocol ref="contentLengthProtocol"/>
</tcp:connector>
<tcp:endpoint name="tcp-inbound" address="tcp://localhost:1234" connector-ref="TCPConnector" doc:name="TCP"/>
<flow name="AdapterFlow" doc:name="AdapterFlow">
<tcp:inbound-endpoint ref="tcp-inbound" doc:name="Inbound TCP"/>
<echo-component doc:name="Echo"/>
</flow>
When the flow reading input and processing read method on ContentLengthProtocol, the adapter always null. But the strange thing is, if i just define ContentLengthProtocol bean but doesn’t referenced the bean inside the TCP connector as custom protocol, spring injection works as usual and adapter is not null.
Can someone give me enlightment of what happened here ?
Any help is kindly appreciated.
Thanks.
I found the exact problem which alter the
@Autowiredprocess. There are some details that I haven’t informed, The Adapter class is actually a service that holds a MyBatis mapper. The MyBatis mapper is configured using spring-mybatis integration,org.mybatis.spring.mapper.MapperScannerConfigurerto be specific. This class(bean) scan certain package to be proxied. Somehow If I combine this beans with a spring bean and mule, @Autowired doesn’t work, even using<property>to manually inject object intoContentLengthProtocolwon’t work properly (The Adapter bean is injected, but not the MyBatis mapper class inside the Adapter bean). As a workaround, I managed to make it work using the old and tedious way, which isorg.mybatis.spring.mapper.MapperFactoryBeanbean. Basically this bean has to be declared for each mapper I have.