gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:46:08 28 4
gpt4 key购买 nike

我有一个用例,需要将 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com