如何在 spring 引导中每次使用时销毁或清除 Bean 对象状态
How to destroy or clear Bean object state in every use in spring boot
我是 spring boot 的新手,我试图找出我们何时使用 @Bean 创建一个 bean 并尝试在需要使用 @Autowired 的地方访问该 bean。但我知道@Bean 默认情况下是单例的,它会保存它的状态,但我想清除它的状态,以便它会提供新的新追加数据,如果没有追加数据则为 null。请帮我解决这个问题。而且我还想知道我是否通过将 Bean 与自动装配一起使用来遵循正确的编码标准,因为我希望我的每个 api 都给出类似类型的响应,这就是为什么我创建一个 pojo 并将其变成一个 bean,这样我就不会' 必须一次又一次地创建对象。对不起,如果我的问题很愚蠢..提前致谢
这是我的主要class
@SpringBootApplication
public class GyftiV2Application {
public static void main(String[] args) {
SpringApplication.run(GyftiV2Application.class, args);
}
@Bean
public ResponseData getResponse() {
return new ResponseData();
}
}
下面是pojo
public class ResponseData {
private boolean responce;
private String error;
private List<?> data = new ArrayList<>();
public ResponseData() {
}
public boolean isResponce() {
return responce;
}
public void setResponce(boolean responce) {
this.responce = responce;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public List<?> getData() {
return data;
}
public void setData(List<?> data) {
this.data = data;
}
}
下面是使用我的bean的服务
@Service
public class UserServiceImpl implements UserService {
@Autowired
private ResponseData resData;
@Autowired
private UserRepository userRepository;
public ResponseData changePassword(PasswordChange pass) {
User user = userRepository.getOne(pass.getUserId());
if (null != user) {
if (pass.getOldPassword().equals(user.getUser_password())) {
if ((pass.getNewPassword().trim()).equals(pass.getConfirmPassword().trim())) {
user.setUser_password(pass.getNewPassword());
userRepository.save(user);
resData.setResponce(true);
resData.setData(Collections.singletonList("Password change successfully"));
return resData;
} else {
resData.setResponce(false);
resData.setError("Please write the same new password in the confirm section");
return resData;
}
} else {
resData.setResponce(false);
resData.setError("Please write the correct old password");
return resData;
}
} else {
resData.setResponce(false);
resData.setError("Something went wrong userId is not correct");
return resData;
}
}
}
带控制器
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping(value = "/changePassword")
public ResponseEntity<ResponseData> changePassword(@RequestBody PasswordChange pass) {
ResponseData response = userService.changePassword(pass);
if (response.isResponce()) {
return new ResponseEntity<>(response, HttpStatus.OK);
}
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
}
但是当我没有传递相同的 newPassoword 和 confirmPassword 时,我得到了回复
{
"responce": false,
"error": "Please write the same new password in the confirm section",
"data": []
}
当我通过一切正确时,我得到了回应
{
"responce": true,
"error": "Please write the same new password in the confirm section",
"data": [
"Password change successfully"
]
}
你会清楚地看到 resData 保存了它的状态,这就是错误键仍然存在的原因。我知道如果我在错误字段中传递“”它会解决这个问题但是有什么原因要清除@Bean 状态吗?感谢您的帮助。
如评论中所述,您不应该将其用作 Bean...它只是一个常规对象。
这是您的服务代码:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
public ResponseData changePassword(PasswordChange pass) {
User user = userRepository.getOne(pass.getUserId());
if (null != user) {
if (pass.getOldPassword().equals(user.getUser_password())) {
if ((pass.getNewPassword().trim()).equals(pass.getConfirmPassword().trim())) {
user.setUser_password(pass.getNewPassword());
userRepository.save(user);
ResponseData resData = new ResponseData();
resData.setResponce(true);
resData.setData(Collections.singletonList("Password change successfully"));
return resData;
} else {
ResponseData resData = new ResponseData();
resData.setResponce(false);
resData.setData("Please write the same new password in the confirm section");
return resData;
}
} else {
...
}
} else {
...
}
}
}
需要考虑的事项:
- 您不应该将您的服务用于 return 直接发送给客户端的 ResponseDate 对象。也许在你的服务中使用异常,比如
PasswordAndConfirmationAreDifferentException
。这样,在你的控制器中更容易按照你想要的方式处理。
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping(value = "/changePassword")
public ResponseEntity<ResponseData> changePassword(@RequestBody PasswordChange pass) {
try {
userService.changePassword(pass);
ResponseData resData = new ResponseData();
resData.setResponce(true);
resData.setData(Collections.singletonList("Password change successfully"));
return new ResponseEntity<>(resData, HttpStatus.OK);
} catch (PasswordAndConfirmationAreDifferentException e) {
ResponseData resData = new ResponseData();
resData.setResponce(false);
resData.setData("Password incorrect");
return new ResponseEntity<>(resData, HttpStatus.BAD_REQUEST);
}
}
}
- 使用构建器轻松构建错误类型
ResponseData.Error("my error")
和数据类型 ResponseData.Data("my message 1", "my message2", ...)
我是 spring boot 的新手,我试图找出我们何时使用 @Bean 创建一个 bean 并尝试在需要使用 @Autowired 的地方访问该 bean。但我知道@Bean 默认情况下是单例的,它会保存它的状态,但我想清除它的状态,以便它会提供新的新追加数据,如果没有追加数据则为 null。请帮我解决这个问题。而且我还想知道我是否通过将 Bean 与自动装配一起使用来遵循正确的编码标准,因为我希望我的每个 api 都给出类似类型的响应,这就是为什么我创建一个 pojo 并将其变成一个 bean,这样我就不会' 必须一次又一次地创建对象。对不起,如果我的问题很愚蠢..提前致谢
这是我的主要class
@SpringBootApplication
public class GyftiV2Application {
public static void main(String[] args) {
SpringApplication.run(GyftiV2Application.class, args);
}
@Bean
public ResponseData getResponse() {
return new ResponseData();
}
}
下面是pojo
public class ResponseData {
private boolean responce;
private String error;
private List<?> data = new ArrayList<>();
public ResponseData() {
}
public boolean isResponce() {
return responce;
}
public void setResponce(boolean responce) {
this.responce = responce;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public List<?> getData() {
return data;
}
public void setData(List<?> data) {
this.data = data;
}
}
下面是使用我的bean的服务
@Service
public class UserServiceImpl implements UserService {
@Autowired
private ResponseData resData;
@Autowired
private UserRepository userRepository;
public ResponseData changePassword(PasswordChange pass) {
User user = userRepository.getOne(pass.getUserId());
if (null != user) {
if (pass.getOldPassword().equals(user.getUser_password())) {
if ((pass.getNewPassword().trim()).equals(pass.getConfirmPassword().trim())) {
user.setUser_password(pass.getNewPassword());
userRepository.save(user);
resData.setResponce(true);
resData.setData(Collections.singletonList("Password change successfully"));
return resData;
} else {
resData.setResponce(false);
resData.setError("Please write the same new password in the confirm section");
return resData;
}
} else {
resData.setResponce(false);
resData.setError("Please write the correct old password");
return resData;
}
} else {
resData.setResponce(false);
resData.setError("Something went wrong userId is not correct");
return resData;
}
}
}
带控制器
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping(value = "/changePassword")
public ResponseEntity<ResponseData> changePassword(@RequestBody PasswordChange pass) {
ResponseData response = userService.changePassword(pass);
if (response.isResponce()) {
return new ResponseEntity<>(response, HttpStatus.OK);
}
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
}
但是当我没有传递相同的 newPassoword 和 confirmPassword 时,我得到了回复
{
"responce": false,
"error": "Please write the same new password in the confirm section",
"data": []
}
当我通过一切正确时,我得到了回应
{
"responce": true,
"error": "Please write the same new password in the confirm section",
"data": [
"Password change successfully"
]
}
你会清楚地看到 resData 保存了它的状态,这就是错误键仍然存在的原因。我知道如果我在错误字段中传递“”它会解决这个问题但是有什么原因要清除@Bean 状态吗?感谢您的帮助。
如评论中所述,您不应该将其用作 Bean...它只是一个常规对象。
这是您的服务代码:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
public ResponseData changePassword(PasswordChange pass) {
User user = userRepository.getOne(pass.getUserId());
if (null != user) {
if (pass.getOldPassword().equals(user.getUser_password())) {
if ((pass.getNewPassword().trim()).equals(pass.getConfirmPassword().trim())) {
user.setUser_password(pass.getNewPassword());
userRepository.save(user);
ResponseData resData = new ResponseData();
resData.setResponce(true);
resData.setData(Collections.singletonList("Password change successfully"));
return resData;
} else {
ResponseData resData = new ResponseData();
resData.setResponce(false);
resData.setData("Please write the same new password in the confirm section");
return resData;
}
} else {
...
}
} else {
...
}
}
}
需要考虑的事项:
- 您不应该将您的服务用于 return 直接发送给客户端的 ResponseDate 对象。也许在你的服务中使用异常,比如
PasswordAndConfirmationAreDifferentException
。这样,在你的控制器中更容易按照你想要的方式处理。
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping(value = "/changePassword")
public ResponseEntity<ResponseData> changePassword(@RequestBody PasswordChange pass) {
try {
userService.changePassword(pass);
ResponseData resData = new ResponseData();
resData.setResponce(true);
resData.setData(Collections.singletonList("Password change successfully"));
return new ResponseEntity<>(resData, HttpStatus.OK);
} catch (PasswordAndConfirmationAreDifferentException e) {
ResponseData resData = new ResponseData();
resData.setResponce(false);
resData.setData("Password incorrect");
return new ResponseEntity<>(resData, HttpStatus.BAD_REQUEST);
}
}
}
- 使用构建器轻松构建错误类型
ResponseData.Error("my error")
和数据类型 ResponseData.Data("my message 1", "my message2", ...)