springMVC中使用dubbo注解配置的问题
2015-12-16 15:27
661 查看
问题,在controller中无法通过注解自动注入dubbo服务,
但是在service中可以自动注入。
package com.sl;
@Controller
public class P{
//期望注入dubbo服务
@Reference(version="1.0.0")
private I0 o;
//注入service
@Autowired
private S s;
@RequestMapping("p")
public void p() throws IOException{
//o is null!没有自动注入
//s 自动注入且s.s()中dubbo服务也注入成功
}
}
package com.sl;
@Service
public class S{
//成功注入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>
...
<!--
这里再生成springMVC的上下文,其父是根上下文。
使用默认的springMVC-servlet.xml配置文件
当一个Request对象产生时,会把这个WebApplicationContext上下文保存在Request对象中,
key是DispatcherServlet.class.getName() + ".CONTEXT"。
可以用spring工具类获得:RequestContextUtils.getWebApplicationContext(request);
-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
applicationContext.xml
...
<!-- dubbo使用注解 -->
<dubbo:annotation />
<!-- 需要扫描注解的包 -->
<context:component-scan base-package="com.sl" />
<!-- dubbo会在根上下文注册postProcessBeforeInitialization,
所以以后这里生成的bean中的@Reference都会被正确解析注入
-->
springMVC-servlet.xml
...
<!-- MVC注解驱动 -->
<mvc:annotation-driven />
<!-- sprint MVC 注解扫描包-->
<context:component-scan base-package="com.sl" />
<!-- springMVC中并没有dubbo配置所以这里生成的controll bean不会解析 @Reference-->
...
解决办法:
1.只将dubbo服务注入到service和Repository中而不是Controller中,其实大部分时候都可如此
2.去掉web.xml中listener,将全部配置都放到springMVC-servlet.xml,这样只生成一个上下文。
3.在springMVC-servlet.xml也加入duboo的配置,这样虽然有了冗余,但是可以保证两个上下文。
但是在service中可以自动注入。
package com.sl;
@Controller
public class P{
//期望注入dubbo服务
@Reference(version="1.0.0")
private I0 o;
//注入service
@Autowired
private S s;
@RequestMapping("p")
public void p() throws IOException{
//o is null!没有自动注入
//s 自动注入且s.s()中dubbo服务也注入成功
}
}
package com.sl;
@Service
public class S{
//成功注入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>
...
<!--
这里再生成springMVC的上下文,其父是根上下文。
使用默认的springMVC-servlet.xml配置文件
当一个Request对象产生时,会把这个WebApplicationContext上下文保存在Request对象中,
key是DispatcherServlet.class.getName() + ".CONTEXT"。
可以用spring工具类获得:RequestContextUtils.getWebApplicationContext(request);
-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
applicationContext.xml
...
<!-- dubbo使用注解 -->
<dubbo:annotation />
<!-- 需要扫描注解的包 -->
<context:component-scan base-package="com.sl" />
<!-- dubbo会在根上下文注册postProcessBeforeInitialization,
所以以后这里生成的bean中的@Reference都会被正确解析注入
-->
springMVC-servlet.xml
...
<!-- MVC注解驱动 -->
<mvc:annotation-driven />
<!-- sprint MVC 注解扫描包-->
<context:component-scan base-package="com.sl" />
<!-- springMVC中并没有dubbo配置所以这里生成的controll bean不会解析 @Reference-->
...
解决办法:
1.只将dubbo服务注入到service和Repository中而不是Controller中,其实大部分时候都可如此
2.去掉web.xml中listener,将全部配置都放到springMVC-servlet.xml,这样只生成一个上下文。
3.在springMVC-servlet.xml也加入duboo的配置,这样虽然有了冗余,但是可以保证两个上下文。
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- 模拟Spring的简单实现
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- Spring Bean基本管理实例详解
- java实现简单美女拼图游戏
- 详解Java的Spring框架中的事务管理方式
- 解析Java的Spring框架的BeanPostProcessor发布处理器
- Java开发框架spring实现自定义缓存标签
- java基本教程之线程休眠 java多线程教程
- JSP开发中在spring mvc项目中实现登录账号单浏览器登录
- spring boot实战之内嵌容器tomcat配置
- 浅析Java的Spring框架中IOC容器容器的应用
- 基于Spring框架的Shiro配置方法