I'm trying to generate some repositories at runtime for spring data jpa with OpenHFT to reduce boilerplate code. The problem is, when running with IDEA it goes just fine, however if i run it in external tomcat or simply in command line: "mvn spring-boot:run", it fails to recognize external resources such as @Resository
and some written classes.
I've tried hard to locate problem but no use, however i've created minimal demo for this problem: github. I also tried to use different classloaders as parameter for OpenHFT's compiler, such as .class, Repository.class and Thread.currentThread().getContextClassLoader(). But still end up in error.
@EventListener
private void initRepositoryMapping(ApplicationReadyEvent event) {
unMappedWithRepoEntity.add(Department.class);
unMappedWithRepoEntity.forEach((clazz) -> {
String className = "com.example.repository.DepartmentRepository";
String javaCode =
"\npackage com.example.repository;" +
"\nimport com.example.entity.Department;" +
"\nimport org.springframework.stereotype.Repository;" +
"\nimport java.util.UUID;" +
"\nimport com.example.repository.BaseRepository;" +
"\n@Repository" +
"\npublic interface DepartmentRepository extends BaseRepository<Department, UUID> {}" ;
logger.info("generating java class : {} code:{}", className, javaCode);
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
logger.info("using classloader: {}", classLoader);
CompilerUtils.CACHED_COMPILER.loadFromJava(classLoader, className, javaCode);
System.out.println(Class.forName("com.example.repository.DepartmentRepository"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
});
}