Jac*_*ine 5 java spring annotations
我使用@ Repository,@ Resource,@ Component,@ Service注释注释了我的类,但这些类必须在2个环境中运行.第一个环境是基于Spring 2.x而另一个环境根本没有弹簧.我确信如果没有弹簧罐,代码就会失败,我想知道你如何保留注释但仍能在两种环境中工作的想法
为了能够使用你提到的注释,或者真的,让Spring为你使用它们以便获得好处,你需要至少使用Spring 2.5.x,就在它们被引入的时候.
此外,注释不需要在类路径上.他们将被忽略.因为当你使用spring 2.0时,将没有代码试图"扫描"它们.
由于您无法删除已接受的帖子,因此我建议您阅读/投票 Hans 的帖子,这比我原来的帖子更好的解释:Dependency on Spring's Annotations
当使用构造型注释(@Service 等)时,获得编译时 bean 验证的代价是您在代码中耦合到 spring-context 库。我看到 3 个直接选项:
1) 删除注释,并在 XML 中配置您的 bean。
2) 将 spring-context.jar(或包含构造型注释的等效库)复制到非 Spring 项目中以解决依赖关系,但保持 Spring 未配置,以便在代码中不使用它。
3)从具体类中删除注释,并使用“Spring”版本扩展它们。这种方法可能会对您的设计造成一点侵入,也可能不会,但值得考虑:
public class MyDAO {
protected SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
// .. Your DAO code resides here ..
}
Run Code Online (Sandbox Code Playgroud)
和 Spring 子类:
@Repository
public class MySpringDAO extends MyDAO {
@AutoWired
protected SessionFactory sessionFactory;
}
Run Code Online (Sandbox Code Playgroud)
这样,您的非 Spring 项目就可以使用“MyDAO”,并从构建中排除“MySpringDAO”类。
| 归档时间: |
|
| 查看次数: |
1251 次 |
| 最近记录: |