当我使用自定义tcp协议时,我在Mule中遇到了问题,而在自定义协议中,使用了@Autowired注解的spring依赖注入。
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配置代码段
<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>当ContentLengthProtocol上的流读取输入和处理读取方法时,适配器始终为空。但奇怪的是,如果我只定义了TCP,而没有引用ContentLengthProtocol连接器中的bean作为自定义协议,spring注入就会像往常一样工作,并且适配器不为空。
有人能告诉我这里发生了什么吗?如有任何帮助,我们不胜感激。谢谢。
发布于 2012-09-25 01:34:47
我发现了改变@Autowired进程的确切问题。有一些细节我还没有通知,Adapter类实际上是一个包含MyBatis映射器的服务。MyBatis映射器是使用spring-mybatis集成配置的,具体地说就是org.mybatis.spring.mapper.MapperScannerConfigurer。这个类(Bean)扫描要代理的某些包。不知何故,如果我将这个bean与spring bean和mule组合在一起,@Autowired就不能工作,甚至使用<property>手动将对象注入ContentLengthProtocol也不能正常工作( Adapter bean被注入,但Adapter bean中的MyBatis映射器类没有)。作为一种变通方法,我设法使用了一种又旧又乏味的方法,即org.mybatis.spring.mapper.MapperFactoryBean bean。基本上,必须为我拥有的每个映射器声明这个bean。
发布于 2012-09-21 00:51:12
在Mule和Spring注解之间存在潜在的注入冲突。According to the doc,我也不认为使用@Inject会有什么帮助。
所以使用常规的Spring注入可能更好:
<spring:bean id="contentLengthProtocol"
class="id.company.protocol.ContentLengthProtocol"
p:adapter-ref="adapter"
p:inboundEndpoint-ref="atm-inbound" />https://stackoverflow.com/questions/12510843
复制相似问题