当我的控制器的update(...)
被执行/在执行之前,我希望填充requestScopedBean.userDetails
。
在我的Spring Web项目中,我有一个基于Java的MyConfiguration extends WebMvcConfigurerAdapter
,其中我有:
@Bean(name = "requestScopedBean")
@Scope(value = "prototype")
public RequestScopedBean requestScopedBean() {
return new RequestScopedBean();
}
在
RequestScopedBean.java
中:public class RequestScopedBean {
public @Autowired UserDetails userDetails;
public void setUserDetails(UserDetails pUserDetails){
userDetails = pUserDetails;
}
@Override
public String toString() {
return "" + (userDetails != null) ;
}
}
And UserDetails.java
public class UserDetails {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我有一个 UsersController.java
文件,其中包含:
@RestController
@RequestMapping("/users")
@Scope("request")
public class UsersController {
@Autowired
private RequestScopedBean requestScopedBean;
@RequestMapping(
value = {"{uid}" },
method = RequestMethod.PUT,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE
)
public ResponseEntity<?> update(
@PathVariable String uid,
@RequestBody UserDetails pUserDetails
) throws Exception {
// LOCATION 1
// return ResponseEntity;
}
}
我该如何做才能让LOCATION 1
处的requestScopedBean.userDetails
持有这个函数作为参数接收到的pUserDetails
变量。
我正在寻找一些基于setter的依赖注入或者一些基于工厂方法的注入,不过如果有其他更适合的方法,请建议。谢谢。
setUserDetails
。我没有看到任何问题。 - a better oliversetUserDetails
。 - a better oliver