问题描述
我阅读了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.
我用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 -
ServiceConfig
,用于为服务层创建Bean -
DataConfig
,该软件包为数据层创建Bean
com.mycompany.myapp.service
包中的 com.mycompany.myapp.data
软件包中的WebConfig
in the com.mycompany.myapp.web` package that creates the beans for the web layerServiceConfig
in thecom.mycompany.myapp.service
package that creates the beans for the service layerDataConfig
in thecom.mycompany.myapp.data
package that creates the beans for the data layer
按功能打包
我会创建
-
OrdersConfig
,用于为订单功能创建bean
com.mycompany.myapp.orders
包中的OrdersConfig
in thecom.mycompany.myapp.orders
package that creates the beans for the orders feature
端口和适配器
我会创建
- 创建Web Bean的
-
WebPortsConfig
. -
OrderConfig
,用于创建端口和适配器体系结构的域对象. 创建数据库适配器bean的 -
DatabaseAdapersConfig
.
com.mycompany.myapp.web
包中的 com.mycompany.myapp.domain
包中的 com.mycompany.myapp.database
中的WebPortsConfig
in thecom.mycompany.myapp.web
package that creates the web beans.OrderConfig
in thecom.mycompany.myapp.domain
package that creates the domain objects of the ports and adapters architecture.DatabaseAdapersConfig
in thecom.mycompany.myapp.database
that creates the database adapter beans.
按组件包装
我会创建
-
WebConfig
,它为Web东西创建了所有bean. -
OrdersConfig
,该软件包为订单组件创建了所有bean.
com.mycompany.myapp.web
包中的 com.mycompany.myapp.orders
软件包中的WebConfig
in thecom.mycompany.myapp.web
package that creates all the beans for the web stuff.OrdersConfig
in thecom.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
.
这篇关于如果我从“清洁架构"中实施架构,谁必须使用包修饰符创建服务?书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!