0

I have a spring-hibernate-springboot project deploying as WAR. I need to use it as jar in another JAVA project. I have seperated its controller part and now made changes in maven to deploy it as jar. I am able to attached it to another project and can call service method. But I have autowired Dao into that service file; when any method of dao is called null pointer exception is thrown. It means Dao is not getting autowired.

Any help will be appriciated.

charmi
  • 197
  • 3
  • 17
  • This might depend on package names in main app and jar but you should probably add an `@Import(
    )` on one of your configuration classes in the main app so it will load.
    – grekier Feb 08 '23 at 14:36
  • HI grekier, Sorry but If I am using multiple dao in 1 service method then I have to import all dao? – charmi Feb 08 '23 at 14:52
  • No. Just the main class. It usually has the rest of the configuration within itself. – grekier Feb 08 '23 at 16:31
  • Hi grekier it didn't work. The project that needs the spring api as jar is JAVA project so I think it will not run all configurations if I import the main class. – charmi Feb 09 '23 at 07:09
  • You need a scanBasePackages in “@SpringBootApplication” that points to a package in the jar that includes the daos. Or a “@ComponentScan” annotation. – John Williams Feb 09 '23 at 07:43
  • @JohnWilliams, I already have "@SpringBootApplication" in the jar file class[Application.java]. and I imported that jar class[Application.java] in my new java[it is simple java project it doesnot uses spring] project. Still the error is as it is. – charmi Feb 09 '23 at 11:46
  • Which package are the DAOs in? Which package is the main() in? Which package is the class with SpringBootApplication annotation in? Could be same. – John Williams Feb 09 '23 at 21:10
  • @John Williams main() class is in the package com.test.backend and dao calsses are in com.test.backend.admin.dao.daoimpl package. Likewise have multiple packages under com.test.backend that contains dao. – charmi Feb 10 '23 at 12:29
  • That all looks fine. What package is the service implementation in? Are other Autowired working, ie not null? – John Williams Feb 10 '23 at 13:23
  • @John Williams service implementation are in com.test.backend.admin.service.impl. – charmi Feb 13 '23 at 05:09
  • That all looks fine. Sorry I can't help. Perhaps you can move your SpringBootApplication main() class out of the jar into the app which depends on the jar. That is more natural and how I have worked successfully before. – John Williams Feb 13 '23 at 17:53

0 Answers0