我想在控制器的update(...)被执行时/在执行之前填充requestScopedBean.userDetails.
I want to populate requestScopedBean.userDetails when my controller's update(...) gets executed / before it is executed.
在我的spring网络项目中,我有基于Java的MyConfiguration extends WebMvcConfigurerAdapter,其中:
In my spring web project I have java based MyConfiguration extends WebMvcConfigurerAdapter in which I have:
@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) ; } }和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; } }我该怎么做,以使requestScopedBean.userDetails在LOCATION 1处保存在此函数中接收的变量pUserDetails作为参数.
What can I do such that at LOCATION 1, requestScopedBean.userDetails holds variable pUserDetails received in this functions as parameter.
我正在寻找一些基于设置者的依赖项注入或一些基于工厂方法的注入,但请告知其他方法是否更合适.谢谢
I am looking for some setter-based dependency injection or some factory method based injection though please do advise if some other method is more suitable. Thanks
推荐答案如果只想保留来自Request的UserDetails对象,只需将该对象设置为requestScopedBean.
If you just want to keep the UserDetails object which came from Request, just set the object to requestScopedBean.
@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 { requestScopedBean.setUserDetails(pUserDetails); // return ResponseEntity; } }除此之外,我建议您不要在RequestScopedBean中的UserDetails对象中自动接线
Apart from this, I advice you not to Autowire in your UserDetails object in RequestScopedBean
public class RequestScopedBean { public UserDetails userDetails; public void setUserDetails(UserDetails pUserDetails){ userDetails = pUserDetails; } @Override public String toString() { return "" + (userDetails != null) ; } }让我知道,这种方法面临的问题是什么.
Let me know, what is the problem you are facing with this approach.
更多推荐
春季:将@RequestBody注入@Bean
发布评论