如何在众多JAR之间分离我的Spring JPA配置?

时间:2017-06-27 04:57:59

标签: java spring configuration

我有一个用例,我需要将Spring配置分成两个完全独立的应用程序。

  

应用程序#1将包含带有Hibernate映射,JPA存储库,DTO模型以及从DAO转换为DTO的逻辑的DAO模型。

     

应用程序#2将包含包含所有业务逻辑的RESTful控制器和服务类。

应用程序#1 配置:

// 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();
    }
}

应用程序#2 配置:

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);
    }
}

应用程序#2 数据加载:

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 ... 
    }
}

这是我尝试启动Application#2时遇到的异常。应用程序#1自行启动正常,但我没有在那里使用存储库,并且相信如果我尝试它可能会抛出错误。最终我只想在应用程序#2中使用逻辑。你对我做错了什么有任何想法吗?

我需要初始化Application#1配置中的所有依赖项,然后在运行Application#2配置之前从Application#2内部运行该配置。

  

说明       com.apptwo.pkg.two.DataLoader中的字段groupRepository需要一个类型的bean       'com.appone.pkg.three.jpa.repository.GroupRepository'无法找到。

     

动作:       考虑在中定义一个'com.appone.pkg.three.jpa.repository.GroupRepository'类型的bean       你的配置。

2 个答案:

答案 0 :(得分:1)

感谢一群人,我找到了解决方案。

部分帮助来自这篇文章:Can't Autowire @Repository annotated interface in Spring Boot

步骤1:

从Application#1中删除了Application.class,使其无法作为独立的Spring Boot App运行。

步骤2:

我改变了AppTwoConfig.java,

// For configuration aspects important for 1 project in particular
package com.appone.pkg.three

@Configuration
@EnableJpaRepositories("com.pkg.three.jpa.repository")
@EntityScan("com.pkg.three.jpa.model")
public class AppTwoConfig {

    @Bean
    GroupValidator groupValidator(){
        return new GroupValidator();
    }
    @Bean
    UserValidator userValidator(){
        return new UserValidator();
    }
    @Bean
    ResourceMapper resourceMapper(){
       return new ResourceMapper();
    }
}

第3步:

我更改了应该作为Spring Boot应用程序运行的Application#2中的Application.java。

package com.apptwo.pkg.one;

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackageClasses = {
        com.apptwo.pkg.one.config.AppConfig.class,
        com.appone.pkg.two.GlobalAppConfig.class,
        com.appone.pkg.three.AppTwoConfig.class
})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

答案 1 :(得分:0)

组件仅扫描扫描com.apptwo.pkg.one的子包。您必须在@SpringBootApplication

中配置正确的包
@SpringBootApplication(basePackages ={"com.appone", "com.apptwo"})