問題,在controller中無法通過註解自動注入dubbo服務,
但是在service中可以自動注入。
package com.sl;br/>@Controller
public class P{br/>//期望注入dubbo服務
@Reference(version="1.0.0")
private I0 o;br/>//注入service
@Autowired
private S s;
@RequestMapping("p")
public void p() throws IOException{
//o is null!沒有自動注入
//s 自動注入且s.s()中dubbo服務也注入成功
}
}
package com.sl;br/>@Service
public class S{br/>//成功注入dubbo
@Reference(version="1.0.0")
private I0 o;
public void s() throws IOException{
//o is not null!可以直接使用
}
}
經過分析,原來配置導致兩個上下文,一個是根上下文,一個是springMVC的上下文,
dubbo的註解配置在根上下文中,因此無法解析springMVC bean的注入。
當前配置:
web.xml
....
< !--
tomcat啓動後,此設置導致spring生成根上下文,且使用默認的applicationContext.xml來獲得其他設置。
根上下文保存在ServletContext中,key是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
可以用spring工具類獲得:WebApplicationContextUtils.getWebApplicationContext(ServletContext);
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
< /listener>
...