问题,在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>
...