我正在做一个关于jax-ws-spring-integration的例子。我的项目有以下结构。
在客户端
此处归档UserClient.java
public class UserClient {
static UserService service;
public static void main(String[] args) {
// TODO Auto-generated method stub
// System.out.println("From webservice"
// + service.sayHello("Nguyen van a"));
service = new UserService();
User user = service.getUser("hoaipham");
System.out.println("Hello" + user.getUserName());
System.out.println("Email" + user.getAge());
}
}
此处归档UserService.java
@WebService
@Component
public class UserService {
@Autowired
private UserBo userBo;
@WebMethod(operationName = "getUser")
public User getUser(String username){
return userBo.loadUser(username);
}
@WebMethod(operationName = "say")
public String sayHello(String name){
return "Hanoi Java say hello to" + name;
}
}
当我在客户端中运行“main”时,会出现以下错误:
线程“main”中的异常java.lang.NullPointerException at edu.java.spring.ws.UserService.getUser(UserService.java:19)at Client.UserClient.main(UserClient.java:14)
答案 0 :(得分:0)
嗯,看起来Spring不在客户端工作(在不同的jvm中)。
尝试创建AnnotationConfigApplicationContext的实例并将您的类添加到Context(或让实例扫描包)。
修改强>
尝试
public class UserClient {
static UserService service;
public static void main(String[] args) {
// TODO Auto-generated method stub
// System.out.println("From webservice"
// + service.sayHello("Nguyen van a"));
service = new AnnotationConfigApplicationContext(UserService.class)
.getBean(UserService.class);
User user = service.getUser("hoaipham");
System.out.println("Hello" + user.getUserName());
System.out.println("Email" + user.getAge());
}
}