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

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

问题描述

我阅读了Bob叔叔的书-"Clean Architecture".西蒙·布朗(Simon Brown)撰写了一章.他修改了几种类型的体系结构.他提供了将实现封装在包中的方法.

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找到了标记为 @Service 批注的 OrderServiceImpl . 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 有一个包修饰符.

如果我不使用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.

按层包装

我会创建

    com.mycompany.myapp.web`包中的
  • WebConfig ,用于为Web层创建Bean
  • com.mycompany.myapp.service 包中的
  • ServiceConfig ,用于为服务层创建Bean
  • com.mycompany.myapp.data 软件包中的
  • DataConfig ,该软件包为数据层创建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

按功能打包

我会创建

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

端口和适配器

我会创建

    创建Web Bean的 com.mycompany.myapp.web 包中的
  • WebPortsConfig .
  • com.mycompany.myapp.domain 包中的
  • OrderConfig ,用于创建端口和适配器体系结构的域对象.
  • 创建数据库适配器bean的 com.mycompany.myapp.database 中的
  • DatabaseAdapersConfig .
  • 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.

按组件包装

我会创建

    com.mycompany.myapp.web 包中的
  • WebConfig ,它为Web东西创建了所有bean.
  • com.mycompany.myapp.orders 软件包中的
  • OrdersConfig ,该软件包为订单组件创建了所有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 config位于同一软件包中,则它们不得位于同一部署单元或模块中.

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

然后,您只需要将两个jar都放在类路径上,就可以在主类的组件扫描中添加 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天全站免登陆