最初教我构建 3 层的方法如下:
- 道
- 域名
- 服务
- 实用程序
- jsf( bean 类) ...
可实例化的 *er 类不适合这些包,尤其是 utils,其中类主要对静态方法进行分组。它可以作为一个副想法添加到服务中,但这感觉很尴尬。
从那时起,我看到了更复杂的包结构(可能是从 Maven 借来的):
- 常量(编码常量和枚举)
- 道
- dao.impl(接口(interface)的实现)
- 型号
- 资源(用于属性和配置文件)
- 服务
- service.impl
- 用户界面 ...
但是,我仍然不知道在哪里可以放置 *er 类,现在我看到其他类型的类弹出,例如自定义异常和 Spring 的这种原始模式(见下文)。一般来说,这些类类型似乎是您在框架/API 中常见的类型。
import org.springframework.context.ApplicationContext;
public class AppContext {
private static ApplicationContext ctx;
/**
* Injected from the class "ApplicationContextProvider" which is automatically
* loaded during Spring-Initialization.
*/
public static void setApplicationContext(ApplicationContext applicationContext) {
ctx = applicationContext;
}
/**
* Get access to the Spring ApplicationContext from everywhere in your Application.
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return ctx;
}
}
<小时/>
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class ApplicationContextProvider implements ApplicationContextAware {
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
// Wiring the ApplicationContext into a static method
AppContext.setApplicationContext(ctx);
}
}
您如何对这些或任何其他“不可分类”进行分组?
最佳答案
这取决于读/写和电子邮件发送器类的作用。
假设 Emailer 类发送电子邮件,即使它是可实例化的类,也适合 util 或 helper 包,因为它与应用程序逻辑没有紧密绑定(bind)。
对于异常,我看到了一种方法,他们将异常类放入一个单独的 *.exceptions 包中,紧邻正在使用的包旁边,或者放在使用它的包内。
例如:
application.dao
application.dao.impl
application.dao.exceptions
关于java - 在这个包结构中,在哪里放置 Reader/Writer/Emailer ... *er 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6504728/