gpt4 book ai didi

java - 从另一个类调用方法时 Autowiring 组件空指针异常

转载 作者:行者123 更新时间:2023-12-02 10:19:29 27 4
gpt4 key购买 nike

我有一个带有 Autowiring 组件和方法的类。如果我直接调用该方法,效果很好。但是,如果对该方法的调用来自另一个类,那么我在使用 Autowired 组件的行中收到 java.lang.NullPointerException 错误。 Autowired 组件是充当代理的接口(interface)组件。我已经为界面组件和 Autowiring 组件尝试了不同的注释,但仍然收到错误。

我不明白为什么如果直接调用该方法,Autowired 组件不为 null,但如果从另一个类调用,则它为 null。

这是界面组件

@FeignClient(name = "authentication-server", url = "localhost:8010")
public interface AuthenticationProxy {

@GetMapping("/headers")
public HttpEntity<String> retrieveHeaders();

@GetMapping("/auth-token")
public AuthorizationTokenBean retrieveToken();

这是使用 Autowired 组件的类

@RestController
public class UserController {

@Autowired
private AuthenticationProxy authenticationProxy;

@PostMapping("/user/create")
public UserResponseBean createUser(ValuesBean userValues) {

UserCreateRequestBean bodyBean = new UserCreateRequestBean();
ValuesBean valuesBean = new ValuesBean();

bodyBean.setValues(userValues);

// This line triggers the null pointer error
// (only if method called from another class)
String token = authenticationProxy
.retrieveToken()
.getAuthorizationToken();

HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", token);
headers.add("Content-type", "application/json");
headers.add("accept", "application/json");

HttpEntity<Object> requestEntity =
new HttpEntity<>(bodyBean, headers);

ResponseEntity<String> responseEntity = new RestTemplate().exchange(
"https://api.acme.com/user/create",
HttpMethod.POST,
requestEntity,
String.class
);

String output = responseEntity.getBody();
Gson gson = new Gson();
return gson.fromJson(output,UserResponseBean.class);

}
}

这是调用该方法的类

@RestController
public class TestController {

@GetMapping("/test/user/create")
public void testUserCreate() {

ValuesBean valuesBean = new ValuesBean();
valuesBean.setDate_of_birth("1917-05-16");
valuesBean.setFirst_name("Juan");
valuesBean.setLast_name("Rulfo");
valuesBean.setGender("Male");
valuesBean.setOccupation("Escritor");

UserController testUser = new USerController();
testUSer.createUser(valuesBean);

}
}

最佳答案

首先:这个世界上没有魔法。

依赖注入(inject)只有依赖注入(inject)框架才可能实现,Spring 提供了其中之一。

实例化类时使用:

UserController testUser = new UserController();

您没有使用任何依赖注入(inject)框架,仅使用纯 Java 对象实例化。所以你不能指望 @Autowired 字段会被魔法填充。

下面的代码可以填充java对象实例中的@Autowired字段:

@Autowired private ApplicationContext applicationContext;

...

UserController bean = new UserController();
AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
factory.autowireBean( bean );

但我认为你的目标是使用 Spring 已经实例化的 UserController 而不是你创建的新实例。所以下面的代码可能就是您真正想要的:

@RestController
public class TestController {

@Autowired private UserController testUser;

@GetMapping("/test/user/create")
public void testUserCreate() {

ValuesBean valuesBean = new ValuesBean();
valuesBean.setDate_of_birth("1917-05-16");
valuesBean.setFirst_name("Juan");
valuesBean.setLast_name("Rulfo");
valuesBean.setGender("Male");
valuesBean.setOccupation("Escritor");

testUser.createUser(valuesBean);

}
}

关于java - 从另一个类调用方法时 Autowiring 组件空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54447169/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com