简体   繁体   English

如何在两个项目之间共享存储库和服务类

[英]How to share Repository and Service classes between 2 projects

I am working on 2 projects, one web app (Spring MVC) and one standalone backend service application (Spring boot) that heavily interact together. 我正在研究2个项目,一个Web应用程序(Spring MVC)和一个独立的后端服务应用程序(Spring Boot),这些项目之间相互影响很大。 I am using hibernate for both and they are both coded using the Netbeans IDE. 我对两者都使用了休眠,它们都使用Netbeans IDE进行了编码。

My "issue" is that i end up with duplicate code in both project, mainly in the Repository and Service layers. 我的“问题”是我最终在两个项目中都得到重复的代码,主要是在存储库和服务层。 My entities are obviously also duplicated since both projects use the same database. 由于两个项目都使用相同的数据库,因此我的实体显然也被复制了。

Is there a way to make some sort of class library (a third project maybe?) and put all the common code in there? 有没有办法制作某种类库(也许是第三个项目?)并将所有通用代码放入其中? If that is indeed possible, how do you then change each project so they can still access this code as if it were part of them? 如果确实可行,那么您如何更改每个项目,以便他们仍然可以像访问代码一样将其访问? I was thinking of putting all my Repositories, Services and entities in there to avoid code duplication and greatly reduce the risk of error. 我当时正在考虑将我所有的存储库,服务和实体放在其中,以避免代码重复并大大降低出错的风险。

Thank you! 谢谢!

Separate those Repository and Service classes to a submodule. 将那些RepositoryService类分离到一个子模块。

The structure looks like: 结构如下:

-- your app
  -- api    (dependent on `common` module)
  -- webapp (dependent on `common` module)
  -- common

Then the problem is to initialize beans inside common module. 然后问题是初始化通用模块中的bean。 AFAIK, you have two options: AFAIK,您有两个选择:

  1. In @Configuration class of api or webapp module, add base packages of common module to component scan packages apiwebapp模块的@Configuration类中,将common模块的基本程序包添加到组件扫描程序包中
  2. In api or webapp resources folder, add Spring configuration factory /src/main/resources/META-INF/spring.factories org.springframework.boot.autoconfigure.EnableAutoConfiguration=your.path.AutoConfiguration apiwebapp资源文件夹中,添加Spring配置工厂/src/main/resources/META-INF/spring.factories org.springframework.boot.autoconfigure.EnableAutoConfiguration = your.path.AutoConfiguration

    Define service/repository @Bean inside AutoConfiguration class AutoConfiguration类中定义服务/存储库@Bean

I am assuming in this answer your projects are connected to each other 我假设在这个答案中您的项目相互联系

You can set multiple properties within one Spring project, where you store your database connection parameters etc. with the help of multiple property files. 您可以在一个Spring项目中设置多个properties ,并在多个属性文件的帮助下存储数据库连接参数等。
For example: 例如:

application-web.properties

application-backend.properties

You can use these in your project, by activating the needed properties file per application. 通过激活每个应用程序所需的属性文件,可以在项目中使用它们。 The profile names will be web and backend in these cases. 在这种情况下,配置文件名称将是webbackend

When using maven, this is the command line I am using: 使用Maven时,这是我正在使用的命令行:

mvn spring-boot:run -Drun.profiles=<<profile>>

Now, back to your java code. 现在,回到您的Java代码。 If there are classes only one of your application is using, you can specify this by 'profile'. 如果只有您的应用程序正在使用的类,则可以通过“配置文件”进行指定。 Example: 例:

@Controller

@Profile({ "web" })
public class WebEndpoint {
}

This way you can make the shared code available for both applications, without duplicating most of the code. 这样,您可以使共享的代码可用于两个应用程序,而无需重复大多数代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM