Java 服务接口和服务实现类spring的代码架构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37541168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:26:08  来源:igfitidea点击:

Code architecture of service interface and service impl classes spring

javaspringdesign-patternsmodel-view-controller

提问by viper

I found out that in an MVC pattern, there are mainly 4 classes; the controller, the service, the service impl and repo.

我发现在一个MVC模式中,主要有4个类;控制器、服务、服务实现和回购。

Service is an interface and service impl implements service class and contains all the logical codes. The structure would be something like :-

服务是一个接口,服务实现实现了服务类并包含了所有的逻辑代码。结构将类似于:-

Serviceinterface

Service界面

Service{

public void someMethod();

}

ServiceImplclass

ServiceImpl班级

 ServiceImpl implements Service{
  public void someMethod(){
   //do something

   }    
 }

But when we want to access the service impl codes from controller, we call the method of service class as :-

但是当我们想从控制器访问服务实现代码时,我们调用服务类的方法为:-

@Autowired 
Service service;

Object obj =  service.someMethod();

How does the controller execute code of ServiceImplclass

控制器如何执行ServiceImpl类的代码

采纳答案by Jesper

This is basically how Spring works:

这基本上是 Spring 的工作方式:

The service implementation should be a Spring bean (it either has to have a @Componentor @Serviceannotation, or should be defined in a Spring XML configuration file), so that Spring will find it and register it in the Spring application context.

服务实现应该是一个 Spring bean(它要么必须有一个@Componentor@Service注释,要么应该在 Spring XML 配置文件中定义),以便 Spring 找到它并在 Spring 应用程序上下文中注册它。

Then you use dependency injection, through the @Autowiredannotation, to inject the implementation of the service into the controller. This means that Spring will look at your controller, it will find the @Autowiredannotation on the servicemember variable and initialize it with a bean that it finds in the application context, which will be the instance of the service implementation class that it has registered earlier. So, after Spring is done, servicewill refer to the instance of ServiceImpl.

然后使用依赖注入,通过@Autowired注解,将服务的实现注入到控制器中。这意味着 Spring 将查看您的控制器,它将找到成员变量@Autowired上的注释,service并使用它在应用程序上下文中找到的 bean 对其进行初始化,该 bean 将是它之前注册的服务实现类的实例。所以,Spring 做完之后,service会引用ServiceImpl.

See the Spring Framework reference documentation for information on how dependency injection works with Spring: The IoC container

有关依赖注入如何与 Spring 协同工作的信息,请参阅 Spring Framework 参考文档:IoC 容器

回答by gWombat

When you use the annotation @Autowired, Spring will automatically search in its application context a candidate to be injected in the controller. A valid candidate should be a concrete class marked as a Spring bean, using annotation @Servicefor example.

当您使用注解@Autowired 时,Spring 将自动在其应用程序上下文中搜索要注入控制器的候选对象。一个有效的候选者应该是一个标记为 Spring bean 的具体类,例如使用注解@Service

回答by LNT

Basic idea behind having this kind of architecture is little different than just spring convention.

拥有这种架构背后的基本思想与弹簧惯例没什么不同。

Lets say tomorrow you decide, you dont want to have single application for both projects, and go into one deployment for webapp and another for service Example UserService WebApp

假设明天您决定,您不想为两个项目使用单个应用程序,并为 webapp 进行一个部署,为服务进行另一个部署 Example UserService WebApp

so for WebApp to connect to UserService it will have to make http requests to get any kind of data. then you will have to change all your WebApp code to make it compatible to new changes. For example instead directly calling method of Service you will call httpClient. To avoid this rework what you can do is, Using interface Service you implement your own ServiceImpl and make all http request in there, rest remains intact.

所以为了让 WebApp 连接到 UserService,它必须发出 http 请求来获取任何类型的数据。那么您将不得不更改所有 WebApp 代码以使其与新更改兼容。例如,您将调用 httpClient,而不是直接调用 Service 的方法。为了避免这种返工,您可以做的是,使用接口 Service 您实现自己的 ServiceImpl 并在其中发出所有 http 请求,其余部分保持不变。

Similar stuff will be done in UserService, it will have its own ServiceImpl as before but will be called in Controller as a singleton object.

类似的事情将在 UserService 中完成,它将像以前一样拥有自己的 ServiceImpl,但将在 Controller 中作为单例对象调用。

Your answer : You can refer to ServiceImpl directly, it will serve the purpose, difference is only when ServiceImpl is not part of current module or any dependency, but final bundled project will have its implementation through some sibling module probably

您的回答:您可以直接引用 ServiceImpl,它可以达到目的,区别仅在于 ServiceImpl 不属于当前模块或任何依赖项时,但最终捆绑的项目可能会通过某些兄弟模块实现

回答by surendrapanday

@Controller - Controller is an entry point where you consume request coming from User Interface, and define a bunch of services, and dump a requestmapping URI path which tells what request is coming in to backend, what to return. Then, you create bunch of services where you write bunch of interfaces, and then you can go on, and create bunch of ServiceImplementation files, and then DAO. Note on Autowired :it automatically brings instance that you specify in @Service - Particular class is to be treated as service, used for writing DAO transactions. @Autowired brings an 'instance' you need from somewhere, usually we try to find 'services' that you want from Autowired annotation, then you can add @qualifier to avoid confusion of what specific instance to bring out of similar beans.

@Controller - 控制器是一个入口点,您可以在其中使用来自用户界面的请求,并定义一堆服务,并转储一个请求映射 URI 路径,该路径告诉后端哪些请求进入,返回什么。然后,你创建一堆服务,在那里你编写一堆接口,然后你可以继续,创建一堆 ServiceImplementation 文件,然后是 DAO。关于 Autowired 的注意事项:它会自动带来您在 @Service 中指定的实例 - 特定类将被视为服务,用于编写 DAO 事务。@Autowired 从某处带来一个你需要的“实例”,通常我们会尝试从 Autowired 注释中找到你想要的“服务”,然后你可以添加 @qualifier 以避免混淆从类似的 bean 中带出什么特定实例。