赞
踩
最近学习SpringBoot整合dubbo,provider启动之后在user中Test方法始终得不到值,一直报空指针错误
pom.xml
application.properties
接口类:
public interface TicketService {
public String getTicket();
}
接口实现类
import com.alibaba.dubbo.config.annotation.Service;
import com.tong.providerticket.service.TicketService;
import org.springframework.stereotype.Component;
@Component
@Service
public class TicketServiceImpl implements TicketService {
@Override
public String getTicket() {
return "《闻香识女人》";
}
}
pom.xml 相同
application.properties:
UserService:
import com.alibaba.dubbo.config.annotation.Reference;
import com.tong.providerticket.service.TicketService;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Reference
TicketService ticketService;
public String hello() {
String ticket = ticketService.getTicket();
System.out.println("get :"+ticket);
return ticket;
}
}
测试方法:
调用之后就报空指针错误。
因为楼主springboot版本是2.2.1,网上看的教程用的是1.5.1的版本,配置好了直接启动就可以接受到数据。
2.2.1需要在带main方法的启动类添加@EnableDubbo注解,如下:
@SpringBootApplication
@EnableDubbo
public class ProviderTicketApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderTicketApplication.class, args);
}
}
如此,启动之后能成功调用,不再报nullpointer
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。