依赖于Spring的注解 [英] Dependency on Spring's annotations

查看:149
本文介绍了依赖于Spring的注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注明我的班,@Repository,@Resource,@Component,@Service注解但这些类必须在2环境中运行。第一个环境是基础,而其他有没有在春春的所有2.X。我敢肯定,code将无春罐子和放大器失败;我想知道我如何能留住注释从你的想法,但在两个环境中仍能正常工作

I have annotated my classes with @Repository, @Resource, @Component, @Service annotations but these classes must run in 2 environments. The first environment is Spring 2.x based while the other has no spring at all. I'm sure the code will fail without the spring jars & I want to know ideas from you on how I can retain the annotations but still work in both environments

推荐答案

既然你不能删除一个公认的帖子,我建议你读/票汉斯后,这是比我原来的一个更好的解释:<一href=\"http://stackoverflow.com/questions/2468390/dependency-on-springs-annotations/2473776#2473776\">http://stackoverflow.com/questions/2468390/dependency-on-springs-annotations/2473776#2473776

Since you cannot delete an accepted post, I suggest you read/vote Hans' post, which is a much better explanation than my original one: http://stackoverflow.com/questions/2468390/dependency-on-springs-annotations/2473776#2473776

<击>当使用典型化注解(@服务等),在权衡获得编译时Bean验证是你成为耦合到春天上下文库在code。我看到3直接的选择:

When using the stereotype annotations (@Service etc), the trade-off for gaining the compile-time bean validation is that you become coupled to the spring-context library in your code. I see 3 immediate options:

1)拆下注释,并在XML配置豆类。

1) Remove the annotations, and configure your beans in XML.

2)复制弹簧context.jar(或等值库牵着你的刻板印象注释)到非Spring项目,以解决依赖关系,但保留取消配置春季所以它是不是在你的code使用。

2) Copy the spring-context.jar (or the equivalent library holding your stereotype annotations) into your non-Spring project to resolve the dependencies, but leave Spring unconfigured so it is not used in your code.

3)从具体类中删除注释,并将它们与春的版本扩展。这种方法可能是也可能不是一点点侵入到你的设计,但是值得考虑的:

3) Remove the annotations from your concrete classes, and extend them with "Spring" versions. This approach may or may not be a little invasive to your design, but worth considering:

public class MyDAO {
    protected SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    // .. Your DAO code resides here ..
}

和Spring的子类:

And the Spring sub-class:

@Repository
public class MySpringDAO extends MyDAO {

    @AutoWired
    protected SessionFactory sessionFactory;
}

这样,你的非Spring项目可以用MyDAO,并从构建排除MySpringDAO级。

这篇关于依赖于Spring的注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆