This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Succeeding with AI and have Veljko Krunic on-line!
See this thread for details.
Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

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

 
Greenhorn
Posts: 26
Hibernate jQuery Java
  • Mark post as helpful
  • send pies
  • 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: 21923
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not pass the MyObj instance as an argument to getTrade?
 
Saloon Keeper
Posts: 11923
253
  • Mark post as helpful
  • send pies
  • 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: 22011
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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: 11923
253
  • Mark post as helpful
  • send pies
  • 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
  • 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: 11923
253
  • Mark post as helpful
  • send pies
  • 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: 22011
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic