I have a multi-Maven module Spring boot project having following structure:
parent
|_ pom.xml
|_ webservices
|_ src/main/java
|_ webservices
|_ WebServicesConfig.java
|_ WebServicesStarter.java
|_ GlobalPropertiesLoader.java
|_ pom.xml
|_ backend
|_ src/main/java
|_ backend
|_ BackendStarter.java
|_ pom.xml
|_ commons
|_ src/main/java
|_ commons
|_ GlobalPropertiesDAO.java
|_ GlobalPropertiesRepository.java
|_ CommonsConfig.java;
|_ pom.xml
Both webservices, and backend are individual Spring boot applications (they generate a jar file which I use for launching them) and they depend on the commons module. So I have included commons as a dependency in webservices and backend's pom.xml.
I have few questions about starting my applications.
- How do I start both backend and webservices in a single JVM? (On the same port)
- I want to auto-wire GlobalPropertiesRepository (located within commons project) in my backend and webservices project. How do I do this? Can I auto-wire across different modules? Just importing commons doesn't work. It throws a "no bean definition error". I think this is because GlobalPropertiesRepository is not launched by the Spring container if I import it.
============= UPDATE =============
Adding my Configuration classes for the applications:
The commons application has an empty Configuration class for now since I only have my Repository class over there. Below is the empty Configuration class:
package commons;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CommonsConfig {
}
And this is the GlobalPropertiesRepository:
package commons;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface GlobalPropertiesRepository extends CrudRepository<GlobalPropertiesDAO, Long>{
}
Below are the necessary classes in the webservices application:
The starter class:
package webservices;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
@SpringBootApplication
@EnableJpaRepositories
@ComponentScan({"commons", "webservices"})
public class WebServicesStarter {
public static void main(String[] args) throws Exception {
SpringApplication.run(WebServicesStarter.class, args);
ClassPathScanningCandidateComponentProvider provider =
new ClassPathScanningCandidateComponentProvider(true);
}
}
The Configuration class:
package webservices;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import commons.CommonsConfig;
@Configuration
@Import(CommonsConfig.class)
public class WebServicesConfig {
@Autowired CommonsConfig commonsConfig;
public WebServicesConfig() {
}
}
And the class where I'm trying to autowire the repository:
package webservices;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import commons.GlobalPropertiesDAO;
import commons.GlobalPropertiesRepository;
@Component
@Scope("singleton")
public class GlobalPropertiesLoader {
@Autowired
public GlobalPropertiesRepository globalPropertiesRepository;
private GlobalPropertiesDAO globalProperties;
@PostConstruct
public void init(){
globalProperties = globalPropertiesRepository.findOne(1L);
}
public GlobalPropertiesDAO getGlobalProperties(){
return globalProperties;
}
}
This is the error I get:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public commons.GlobalPropertiesRepository webservices.GlobalPropertiesLoader.globalPropertiesRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [commons.GlobalPropertiesRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
Thanks.