jDub9:
I have a use-case that I need to separate my Spring configuration into 2 completely separate applications.
Application #1 will contain DAO models with Hibernate Mappings, JPA Repositories, DTO models, and logic to convert from a DAO to a DTO.
Application #2 will contain the RESTful Controllers and Service classes that contain all the business logic.
Application #1 configuration:
// For application startup
package com.appone.pkg.one
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
// For global configuration files across numerous projects
package com.appone.pkg.two
@Configuration
public class GlobalAppConfig {
@Bean
MapperFacade mapperFacade() {
MapperFactory factory = new DefaultMapperFactory.Builder().build();
factory.classMap(Group.class, GroupDTO.class).byDefault().register();
factory.classMap(Role.class, RoleDTO.class).byDefault().register();
factory.classMap(User.class, UserDTO.class).byDefault().register();
return factory.getMapperFacade();
}
}
// For configuration aspects important for 1 project in particular
package com.appone.pkg.three
@Configuration
@EnableJpaRepositories("com.pkg.three.jpa.repository")
public class AppTwoConfig {
@Autowired
private EntityManager em;
private RepositoryFactorySupport factorySupport;
private void initialize(){
if(factorySupport == null){
factorySupport = new JpaRepositoryFactory(em);
}
}
@Bean
GroupRepository groupRepository(){
initialize();
return factorySupport.getRepository(GroupRepository.class);
}
@Bean
RoleRepository roleRepository(){
initialize();
return factorySupport.getRepository(RoleRepository.class);
}
@Bean
UserRepository userRepository(){
initialize();
return factorySupport.getRepository(UserRepository.class);
}
@Bean
GroupValidator groupValidator(){
return new GroupValidator();
}
@Bean
UserValidator userValidator(){
return new UserValidator();
}
@Bean
ResourceMapper resourceMapper(){
return new ResourceMapper();
}
}
Application #2 configuration:
package com.apptwo.pkg.one;
@SpringBootApplication
@EnableAutoConfiguration
@EntityScan(basePackageClasses = {com.appone.pkg.three.AppTwoConfig.class})
public class ApplicationTwo {
public static void main(String[] args) {
SpringApplication.run(ApplicationTwo.class, args);
}
}
Application #2 data load:
package com.apptwo.pkg.two;
@Component
public class DataLoader implements ApplicationRunner {
@Autowired
private GroupRepository groupRepository;
@Autowired
private RoleRepository roleRepository;
@Autowired
private UserRepository userRepository;
@Autowired
private ResourceMapper resourceMapper;
/**
* Loads initial data into the h2 database.
*/
public void run(ApplicationArguments args) {
// Load the initial data ...
}
}
This is the exception I am getting when I try to launch Application #2. Application #1 starts up fine on it's own, but I am not using the Repositories there and believe it could throw errors if I tried. Ultimately I want the logic only in Application #2 though. Do you have any ideas on what I am doing wrong?
I need to initialize all the dependencies in Application #1 configuration and then run that configuration from inside Application #2 before I run the Application #2 configuration.
Description: Field groupRepository in com.apptwo.pkg.two.DataLoader required a Bean of type 'com.appone.pkg.three.jpa.repository.GroupRepository' that could not be found.
Action: Consider defining a bean of type 'com.appone.pkg.three.jpa.repository.GroupRepository' in your configuration.
Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE