• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

How to inject bean using constructor and call method in Spring Boot?

 
Greenhorn
Posts: 26
Hibernate jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Service
abstract class A {
 protected MyObj obj;
 protected A(MyObj obj) {
   this.obj = obj;
 }
 public abstract XYZ getTrade();
}

@Service
public class B extends A {
  B(MyObj obj) {
    super(obj);
  }

  public XYZ getTrade() {}
}

@Service
public class C extends A {
  C(MyObj obj) {
    super(obj);
  }

  public XYZ getTrade() {}
}

@Controller
public class MyController {

 @GemMapping("/test")
 public void test() {
   MyObj obj = new MyObj();
   if(condition 1) {
      //call getTrade() method of class B
   }
   if(condition 2) {
      //call getTrade() method of class C
   }
 }
}

MyObj is a user-defined POJO that is not managed bean. I have the above classes. now I have to call getTrade() method based on some condition in Controller. Could you please help/advise?
 
Sheriff
Posts: 22715
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not pass the MyObj instance as an argument to getTrade?
 
Saloon Keeper
Posts: 14665
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To expand on what Rob said, I *think* he means that you should pass an instance of MyObj to the getTrade() method, instead of passing it to the service constructor.
 
Saloon Keeper
Posts: 26529
187
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More to the point, why is MyObj not a managed bean instance(s)? What benefit is there to manually managing it when you already have Spring managing assets?
 
Sac Jadhav
Greenhorn
Posts: 26
Hibernate jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all,
@Rob/@Stephan- I want to pass  MyObj is in the constructor, the reason is Class A is base class and it will have some common methods where MyObj is needed for the mapping so I am passing it to the constructor and initializing instance variable which I can use in both superclass and subclass.

@Tim - Sorry I have mentioned it incorrectly Actually MyObj is not the user-defined class. It is the class from the third party library.
 
Stephan van Hulst
Saloon Keeper
Posts: 14665
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if it's from a third party, you can still provide an instance of it in your beans.xml or through a bean factory.
 
Sac Jadhav
Greenhorn
Posts: 26
Hibernate jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Stephan, to be specific. It is AbstractMx object which I get when parser swift message
 
Stephan van Hulst
Saloon Keeper
Posts: 14665
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's all fine, but what approach to use depends on what you're using the MX message for, and what kind of service your classes represent.

We can give you better advice if you tell us what the classes A, B, C and XYZ are supposed to represent and what the getTrade() method is supposed to do with the message.
 
Tim Holloway
Saloon Keeper
Posts: 26529
187
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sac Jadhav wrote:@Stephan, to be specific. It is AbstractMx object which I get when parser swift message



Spring isn't really designed for dealing with the idea of injecting dynamic objects into the objects it creates. Basically, if you want to construct an object that references another object and that other object cannot be created on-demand when the first object is being constructed, then the first object shouldn't be attempting to obtain the second one at construction time.
 
reply
    Bookmark Topic Watch Topic
  • New Topic