2

I have Controllers, Services and DAOs in separate spring boot modules. I tried calling a stored procedure from DAO module using JPA API StoredProcedureQuery method, for that I need to autowire EntityManager. This set up works if I run DAO alone as a spring boot application. But I am getting NullPointerException when I call this DAO class from controller.

My LoginDAO in DAO project

@Repository
public class LoginDAO implements ILoginDAO {

@Autowired
EntityManager entityManager;
@Transactional
public LoginEntity getLoginCheck(String[] loginfo) {
    StoredProcedureQuery storedProcedure =entityManager.createStoredProcedureQuery("DPR_LOGIN_CHECK");
    }
}

My LoginController class in Controller Module

@Controller
public class LoginController {

@Autowired
LoginDAO loginDAO;

@RequestMapping(value = "/LoginAction", method = RequestMethod.POST)
public @ResponseBody
String getLoginCheck(@RequestParam(value = "dataString") String dataString,
        HttpSession session, HttpServletRequest request,
        HttpServletResponse response) {
loginDAO.getLoginCheck(null,loginDetails);
    }
}

My controller project is dependent on DAO. Here is my main controller class

@SpringBootApplication(scanBasePackages={"com.mars.presentation","com.mars.UserManagementControllers","com.mars","com.mars.CommonDAO"})
public class MarsApplication extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(MarsApplication.class);
}

public static void main(String[] args) throws Exception {
    SpringApplication.run(MarsApplication.class, args);
    }
}

and main DAO class

@SpringBootApplication(scanBasePackages={"com.mars.CommonDAO"}) 
@EntityScan(basePackages={"com.mars.CommonEntities"})
@EnableJpaRepositories
public class CommonDao {

public static void main(String[] args) {
    ApplicationContext ctx = SpringApplication.run(CommonDao.class, args);
    String[] loginfo = new String[] {"cjva", "Banaacvdvna", "Orange", "Grapes"};
    LoginDAO loginDAO = ctx.getBean(LoginDAO.class);
    loginDAO.getLoginCheck(null,loginfo);
    }
}
Indrajith K V
  • 197
  • 1
  • 1
  • 8
  • Add the code... – Alien Jul 19 '18 at 04:29
  • Kindly show the code and configurations you have tried – codeLover Jul 19 '18 at 04:29
  • @Alien Added LoginDAO class in DAO project/module.It works fine if I run DAO project alone. But I am getting NullPointerException when I call from classes outside DAO project/module – Indrajith K V Jul 19 '18 at 04:47
  • you need to autowire LoginDAO before using it wherever you want to. – Alien Jul 19 '18 at 04:50
  • @Alien nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginDAO': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [com.empay.commonDAO.LoginDAO] from ClassLoader [ParallelWebappClassLoader now I am getting this error.. I included package of LoginDAO class in scanBasePackages of controller. – Indrajith K V Jul 19 '18 at 05:19
  • https://stackoverflow.com/questions/40089443/how-to-add-a-dependency-to-a-spring-boot-jar-in-another-project – himanshu bisht Jul 19 '18 at 06:21

0 Answers0