So currently, I have two projects, let's say Storefront and Dashboard, and both of these projects have the same POJO classes, Services, and some API Endpoints. For example, consider a class Student, we have this class in Storefront as well as in the Dashboard along with its service class. Further, in near future, we'll be implementing another project for clients, a Client Dashboard, that'll have almost 90% of the same resources.
So I thought, what if I create a maven project with all the libraries I need across all projects and import them as needed. This would reduce the redundancy and would also make other projects light weighted. I've previously used the in-project repository and Dropbox as a maven repository so it'll be a bit easier for me this time.
Now the problem is: I have the StudentRepository corresponding to the Student, which is further used with the '@Autowired' annotation right? As per my knowledge, everything will work when I run the '@SpringBootApplication' but as I previously mentioned, I'll be importing these packages, and doing so, the program will through a NullPointerException cause the instance of the StudentRepository will be null.
@SpringBootApplication
@EnableAutoConfiguration
@EntityScan
public class DemoApplication implements CommandLineRunner {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
StudentRepository repository;
public static void main(String[] args) {
SpringApplication.run(PaperTrueLibrariesApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
// This will work
logger.info("Inserting -> {}", repository.save(new Student("studentName", "primaryKey")));
}
// This won't
public void addAStudent(String studentName, String primaryKey) {
repository.save(new Student(studentName, primaryKey));
}
}
public class Test {
public static void main(String[] args) {
// This will throw a NullPointerException
new DemoApplication().addAStudent("yourNameProbably", "yourSocialSecurityNumber");
}
}
So is there any other way to make this work? Any suggestions will be appreciated and thanks in advance.