1

In my Spring Boot application, I have class A. In that class, I have declared my DAO layer variable with @Autowired annotation. I have a separate Java library which contains class B (which has a method that accepts generic parameters). I want to send the @Autowired variable from class A as a parameter to the method of class B and perform some operation with that variable in class B's method.

Is there any way to achieve this? Will the second class be aware of the context?

awesomemypro
  • 531
  • 1
  • 11
  • 32
  • Yes you can do it. but what is the purpose of doing so.can you help me with a use case. – Dinu Jan 02 '20 at 06:45
  • 1
    Spring bean is just another java object assembled based on your configurations and provided to you by the Spring container . what happens when you pass this object to the library method ? – R.G Jan 02 '20 at 06:46
  • It is showing up as null. – awesomemypro Jan 02 '20 at 06:47
  • @Dinu trying to build a simple app where an external library can be used. – awesomemypro Jan 02 '20 at 06:48
  • In Spring when the class is invoked the autowired variables are initialized first so that you can pass that value to other methods and classes. But make sure the class where yopu are calling the autowired is either a component or service – Dinu Jan 02 '20 at 06:55
  • "But make sure the class where yopu are calling the autowired is either a component or service " - does this mean that the other class has to be a spring bean? – awesomemypro Jan 02 '20 at 06:57
  • Not needed to be a spring bean it can be simple pojo also – Dinu Jan 02 '20 at 07:06
  • The variable is returning "null". – awesomemypro Jan 02 '20 at 07:08
  • 1
    @awesomemypro that's because your code has a bug. But you didn't post your code, so we can't find it and explain it. – JB Nizet Jan 02 '20 at 07:19
  • Note that (1) using constructor injection eliminates many of these bugs and (2) you can use an `@Bean` method to provide autowired dependencies to third-party libraries. – chrylis -cautiouslyoptimistic- Jan 02 '20 at 08:01
  • I cannot use the @Bean since the library does not need Spring. – awesomemypro Jan 02 '20 at 09:51

2 Answers2

0

Yes you can do it, but it is not a good practice. If you want to create a separate library then make it simple. You are increasing the dependency.

It is always recommended to follow loos coupling principle while building application.

0

Will the second class be aware of the context?

I assume, by second class you meant class B. ClassB doesnt need to be aware of the context. All it care is, it needs a non-null Object of DAO layer as method argument.

So, the question is, at what point are you trying to invoke the method in class B. If you are invoking it before spring completes it's part, then all you will get is null. Try calling the ClassB.method() from @PostConstruct annotated method and see whether you are still getting null reference for DAO member variable. If that's the case look at your spring bean instantiation.

so-random-dude
  • 15,277
  • 10
  • 68
  • 113