java - 如何在众多 JAR 之间分离 Spring JPA 配置?

标签 java spring configuration

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

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.

<小时/>

应用程序 #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 ... 
    }
}

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

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

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.

最佳答案

感谢大家的帮助,我找到了解决方案。

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

第 1 步:

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

第 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:

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

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

关于java - 如何在众多 JAR 之间分离 Spring JPA 配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44772532/

相关文章:

java - 通过Java中的IP地址查找并打印到网络打印机

java - 如何制作可扩展的ArrayList?

Java 作业调度 : Is this possible with Quartz, 如果不是,我的替代方案是什么?

json - Heroku - 在 app.json 中配置调度程序

python - 如何在 XCode 4.6 中配置 Python?

java - SimpleStringProperty 和 StringProperty 的区别

java - 从基于 REST 的 Spring Controller 返回 "Resource"是否有意义?

java - 在 Spring Boot 中从 application.properties 配置国际化

java - 如何从 XFire AbstractHandler 的 invoke() 方法中获取 ServletRequest 对象?

regex - key :value pairs 的 Scala 正则表达式解析器