所以,我注意到我绝对倾向于模式化我的Spring/Hibernate堆栈对象,如下所示:
这里的问题是Foos没有任何真正的逻辑.例如,如果每次Foo到期时都需要发送电子邮件,则不会调用Foo.expire().有一个对FooService.expireFoo(fooId)的调用.这有以下几种原因:
因此,出于这些原因,我的Spring域对象往往是带有一些验证逻辑的美化结构.也许这没关系.也许Web服务可以作为程序代码.也许随着新功能的编写,创建以新方式处理相同旧对象的新服务是可以接受的.
但是我想摆脱这种设计,我想知道其他Spring使用的是什么呢?你是否用装载时编织等花哨的技巧来对抗它(我觉得不舒服)?你有其他的伎俩吗?你认为程序是好的吗?
首先让我们看看实用程序类(大多数javadoc已被删除,只是示例):
public class ApplicationContextUtils {
/**
* The application context; care should be taken to ensure that 1) this
* variable is assigned exactly once (in the
* {@link #setContext(ApplicationContext)} method, 2) the context is never
* reassigned to {@code null}, 3) access to the field is thread-safe (no race
* conditions can occur)
*/
private static ApplicationContext context = null;
public static ApplicationContext getContext() {
if (!isInitialized()) {
throw new IllegalStateException(
"Context not initialized yet! (Has the "
+ …Run Code Online (Sandbox Code Playgroud)