美文网首页
7、spring boot使用dubbo中的service空指针

7、spring boot使用dubbo中的service空指针

作者: ltjxwxz | 来源:发表于2018-02-08 13:08 被阅读0次

之前在spring中使用dubbo,后来用了spring boot,使用dubbo的service竟然空指针了,网上很多资料说是跟spring的扫描顺序有关,嗯,后来证明确实是spring控制的bean中引用了dubbo的bean,但是此时dubbo的bean还没有被初始化,所以报错。正确的做法是让dubbo的bean在spring的bean之前初始化。
spring boot中包的扫描顺序:从Application开始,扫描Application以及它的子包下面的文件,而微服务的jar包中的service不在Application下面,所以该service在spring中的service后面初始化,此时@ComponentScan 就发挥作用了,@ComponentScan(basePackages={"cn.com.dubbo.mail.EDM.", "com.autoplateform."}),这样就控制了扫描顺序。

Application如下:

@ImportResource({"classpath:applicationContext_dubbo.xml"})
@ComponentScan(basePackages={"cn.com.xxx.dubbo.mail.EDM.*", "com.xxx.autoplateform.*"})
@MapperScan(basePackages={"com.xxx.autoplateform.dao"})
@SpringBootApplication
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

相关文章

网友评论

      本文标题:7、spring boot使用dubbo中的service空指针

      本文链接:https://www.haomeiwen.com/subject/mqoozxtx.html