Spring在调用时从其他方法获取值

fykwrbwg  于 2022-10-04  发布在  Spring
关注(0)|答案(1)|浏览(181)

实现:在Service1中,将在方法transerValue中的service2中返回值,但当稍后调用CalledLaterMethod时,但当我尝试访问CalledLaterMethod中的transerValue时,它为空。有没有可能将值存储在transerValue中,并等待CalledLaterMethod被调用并获得该值?对于任何解决方案,请让我知道

  1. @Data
  2. @AllArgsConstructor
  3. @NoArgsConstructor
  4. @Builder
  5. public class Class1{
  6. private String param1;
  7. private String param2;
  8. }
  1. @Data
  2. @AllArgsConstructor
  3. @NoArgsConstructor
  4. @Builder
  5. public class Class2{
  6. private String param4;
  7. private String param5;
  8. }
  1. @Slf4j
  2. @Service
  3. public class Service1{
  4. // with method for validationString
  5. public Class1 validationString(String val1, String val2){
  6. String present = "PRESENT";
  7. String empty = "EMPTY";
  8. String param1Transfer = "";
  9. String param2Transfer = "";
  10. if(val1.isEmpty()) param1Transfer = empty;
  11. else param1Transfer = present;
  12. if(val2.isEmpty()) param2Transfer = empty;
  13. else param2Transfer = present;
  14. Class1 class1 = Class1.builder()
  15. .param1(param1Transfer)
  16. .param2(param2Transfer)
  17. .build();
  18. return Service2.transerValue(class1);
  19. }
  20. }
  1. @Slf4j
  2. @Service
  3. public class Service2{
  4. public static Class1 transerValue(Class1 class1){
  5. //log will work
  6. log.info("Well this data show ={}",class1 );
  7. return class1;
  8. }
  9. //this method CalledLaterMethod will be called later and i must get the value being transfer in transerValue
  10. // and must not change the parameter of CalledLaterMethod
  11. public static Class2 CalledLaterMethod(OtherClass1 other1,OtherClass other2 ){
  12. // i tried
  13. //this is null
  14. Class1 class1 = new Class1();
  15. Class1 classClone = transerValue(class1)
  16. log.info(classClone);
  17. //trying to do
  18. Class2 class2 = Class2.builder.
  19. .param4(classClone.getParam1)
  20. .param5(classClone.getParam2)
  21. build();
  22. return class2;
  23. }
  24. }
aor9mmx1

aor9mmx11#

您将始终得到NULL,因为您没有在CalledLaterMethod中的Class1中设置任何值,在那个类中,您只示例化对象Class1而不设置该对象中的值,除非您在CalledLaterMethod中传递了值Class1 from参数。我假设OtherClass1是要传递的Class1对象,如果不想更改CalledLaterMethod中的参数,可以这样做

  1. public class Service2{
  2. public static Class1 transerValue(Class1 class1){
  3. //log will work
  4. log.info("Well this data show ={}",class1 );
  5. return class1;
  6. }
  7. //this method CalledLaterMethod will be called later and i must get the value being transfer in transerValue
  8. // and must not change the parameter of CalledLaterMethod
  9. public static Class2 CalledLaterMethod(OtherClass1 other1,OtherClass other2 ){
  10. // assign object from parameter to a new object called class1
  11. Class1 class1 = other1;
  12. Class1 classClone = transerValue(class1)
  13. log.info(classClone);
  14. //trying to do
  15. Class2 class2 = Class2.builder.
  16. .param4(classClone.getParam1)
  17. .param5(classClone.getParam2)
  18. build();
  19. return class2;
  20. }
  21. }
展开查看全部

相关问题