如果我从“清洁架构"实现架构,谁必须使用包修饰符创建服务?书 [英] Who must create services with package modifier if I implement architecture from "Clean Architecture" book

查看:17
本文介绍了如果我从“清洁架构"实现架构,谁必须使用包修饰符创建服务?书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了鲍勃叔叔的书 - 清洁建筑".有一章是西蒙·布朗写的.他修改了几种类型的建筑.他提议将实现封装在包中.

I read Uncle Bob's book - "Clean Architecture". There is one chapter written by Simon Brown. He revised a few types of architecture. He offers to incapsulate implementations in packages.

如果我把包裹带回来并标记(通过图形淡化)那些可以使访问修饰符更具限制性的类型,图片变得很有趣(图34.8)

If I bring the packages back and mark (by graphically fading) those types where the access modifier can be made more restrictive, the picture becomes pretty interesting (Figure 34.8)

我使用 spring DI 实现了一种方法:

I implemented one approach with spring DI:

com.my.service

public interface OrderService {
    List<Order> getOrders();
}

和实施:

com.my.service.impl

@Service
class OrderServiceImpl implements OrderService {
    //...
}

它工作正常,因为 Spring 找到了 OrderServiceImpl 标记的 @Service 注释.OrderServiceImpl 被封装在(图 34.8.)

It works fine because Spring finds OrderServiceImpl marked @Service annotation. OrderServiceImpl is encapsulated as on the (Figure 34.8.)

但是如果没有 Spring 注释配置,我怎么能重复这个呢?例如,如果我使用 Spring java 配置,我应该像这样创建一个 bean:

But how can I repeat this without Spring annotation configuration? For example, if I use Spring java configuration, I should create a bean like this:

@Configuration
public class AppConfig {

    @Bean
    OrderService orderService(){
        return new OrderServiceImpl();
    }
}

但是 OrderServiceImpl 有一个包修饰符.

But OrderServiceImpl has a package modifier.

如果我不使用 Spring,我该怎么做才能重复这种方法?

If I don't use Spring, what should I do to repeat this approach?

推荐答案

我会根据不同的包装类型来放置配​​置.

I would place the configs according to the different packaging types.

逐层封装

我会创造

  • WebConfig 在 com.mycompany.myapp.web` 包中,用于为 web 层创建 bean
  • ServiceConfig 位于 com.mycompany.myapp.service 包中,用于为服务层创建 bean
  • DataConfig 位于 com.mycompany.myapp.data 包中,用于为数据层创建 bean
  • WebConfig in the com.mycompany.myapp.web` package that creates the beans for the web layer
  • ServiceConfig in the com.mycompany.myapp.service package that creates the beans for the service layer
  • DataConfig in the com.mycompany.myapp.data package that creates the beans for the data layer

按功能打包

我会创造

  • OrdersConfig 位于 com.mycompany.myapp.orders 包中,用于为订单功能创建 bean
  • OrdersConfig in the com.mycompany.myapp.orders package that creates the beans for the orders feature

端口和适配器

我会创造

  • WebPortsConfig 位于创建 Web bean 的 com.mycompany.myapp.web 包中.
  • OrderConfig 位于 com.mycompany.myapp.domain 包中,用于创建端口和适配器架构的域对象.
  • DatabaseAdapersConfig 在创建数据库适配器 bean 的 com.mycompany.myapp.database 中.
  • WebPortsConfig in the com.mycompany.myapp.web package that creates the web beans.
  • OrderConfig in the com.mycompany.myapp.domain package that creates the domain objects of the ports and adapters architecture.
  • DatabaseAdapersConfig in the com.mycompany.myapp.database that creates the database adapter beans.

按组件打包

我会创造

  • WebConfig 位于 com.mycompany.myapp.web 包中,用于为 Web 内容创建所有 bean.
  • OrdersConfig 位于 com.mycompany.myapp.orders 包中,用于为订单组件创建所有 bean.
  • WebConfig in the com.mycompany.myapp.web package that creates all the beans for the web stuff.
  • OrdersConfig in the com.mycompany.myapp.orders package that creates all the beans for the order component.

部署单位

如果 spring 配置在同一个包中的事件,它们不能在同一个部署单元或模块中.

Event if the spring configs are in the same package they must not be in the same deployment unit or module.

例如您可以创建一个 service.jar 和一个 service-config.jar 来将纯应用程序与框架内容分开.

E.g. you can create a service.jar and a service-config.jar to separate the pure application from the framework stuff.

service.jar
+- com
   +- mycompany
      +- myapp
         +- service
            +- OrderService.class
            +- OrderServiceImpl.class


service-config.jar
+- com
   +- mycompany
      +- myapp
         +- service
            +- ServiceConfig.class

然后你只需要将两个 jars 放在类路径上,你可以将 com.mycompany.myapp.service 添加到你的主类中的组件扫描中,或者你直接引用 ServiceConfig.

Then you just have to put both jars on the classpath and you can either add com.mycompany.myapp.service to your component scan in your main class or you directly reference the ServiceConfig.

这篇关于如果我从“清洁架构"实现架构,谁必须使用包修饰符创建服务?书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆