春季:将@RequestBody注入@Bean

编程入门 行业动态 更新时间:2024-10-11 07:31:06
本文介绍了春季:将@RequestBody注入@Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在控制器的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

本文发布于:2023-11-24 22:08:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627050.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:春季   RequestBody   Bean

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!