Spring @Repository最佳做法 [英] Spring @Repository best practices

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

问题描述

上下文:Web应用程序

Context: Web application

我以前没有使用过Spring,但是根据Spring文档,所有的bean都是singleton,除非我们将它们声明为prototype.

I haven't used Spring before, but according to the Spring docs, all the beans are singleton, unless we declare them as prototype.

  • 不使用Spring:

通常,当有业务/服务层调用时,我会实例化新的DAO. 如果它是RESTfull服务,我将实例化几乎所有依赖于调用的对象.

Normally I instantiate new DAO when there is a call to the business/service layer. If it is a RESTfull service, I instantiate almost all the objects which depend on the call.

  • 使用Spring:

我可以用@Repository注释数据访问类,也可以将@Service用于服务层类.

I can annotate data access classes with @Repository and also I can use @Service for service layer classes.

因此,带有上述批注的我的类在默认情况下为singleton. 有一个@Scope批注,我们可以将它们声明为原型,但似乎没有人这样做.

So my classes with above annotations are singleton by default. There is a @Scope annotation that we can declare them as prototype, but nobody seems doing this.

  • 没有弹簧:每次new Object();
  • 使用Spring:singleton
  • Without Spring : new Object(); each time
  • With Spring: singleton

我的问题是

  1. 我以前使用的方式(每次创建新实例)都不正确?
  2. 如果@Repositorysingleton,那么在没有解决此类问题时,它将如何处理线程安全性? (假设它是由春季代理完成的)
  3. 什么是最佳做法, @Repository就足够了,还是添加@Scope('prototype')会更好?
  4. 我看不到有人将@Scope('prototype')@Repository结合使用(根据教程,博客等).有众所周知的原因吗?
  5. 如果我的DAO类被多个非常高频率的线程访问,该怎么办? (这是我最关心的那个)
  1. The way I used before (creating new instance each time) is incorrect ?
  2. If @Repository is singleton, how does it handle thread safety when there is no such a thing addressed? (Assume it is done by spring proxies)
  3. What is the best practice, @Repository is enough or adding @Scope('prototype') would be better ?
  4. I don't see anyone use @Scope('prototype') with @Repository (according to the tutorials, blogs, etc). Is there a well know reason?
  5. What if my DAO class is accessed by multiple large number of threads with very high frequency? (This is the one I concern the most)

谢谢

推荐答案

您是正确的-在 Spring 世界中,大多数豆类都是单例.

You're correct - in Spring world most of the beans are singletons.

  1. 我以前使用的方式(每次创建新实例)都不正确?

这是正确的,因为它可以工作.问题在于,您在每个请求上实例化一个DAO的新实例-在某些情况下,它可能会很昂贵,而且无论如何它都没有任何意义-为什么您需要一堆DAO实例? 另一方面,Spring不仅会创建一个单例对象,而且还会将DAO注入服务或其他DAO的e.t.c.即为您做了很多工作

It is not incorrect since it works. The problem about it is that you instantiate a new instance of DAO on each request - in some cases it might be expensive, and anyway it doesn't make any sense - why would you need a bunch of DAO instances? Spring on the other hand not only creates a singleton but also injects DAO's to services or other DAO's e.t.c. i.e. does a lot of work for you

  1. 如果@Repository是singleton,那么在没有解决此类问题时,它将如何处理线程安全性? (假设它是由春季代理完成的)

编写@Repository bean时,通常会在其中注入 DataSource EntityManager . DataSource.getConnection()方法应该是线程安全的.对于 EntityManager Spring 将注入一个代理,该代理对于不同的线程的行为将有所不同,即不同的线程不会共享相同的JPA会话.

When you are writing a @Repository bean, you would normally inject there a DataSource or an EntityManager. DataSource.getConnection() method should be thread safe. With regard to EntityManager, Spring will inject a proxy which will behave differently for different threads, i.e. different threads won't share the same JPA session.

  1. 最佳实践是什么,@ Repository够用还是添加@Scope('prototype')会更好?

最佳实践(或更广泛地使用)是使用@Repository

The best practice (or rather a most wide-spread approach) is to just use @Repository

  1. 我看不到有人将@Scope('prototype')与@Repository结合使用(根据教程,博客等).有众所周知的原因吗?

原因是,创建多个@Repository bean实例没有任何好处

The reason is that there's no profit from creating multiple instances of @Repository beans

  1. 如果我的DAO类被多个非常高频率的线程访问,该怎么办? (这是我最关心的那个)

同样,单例比为每个请求创建一个新对象要好.只是避免冗余同步,这样您的线程就不会在某些监视器上阻塞

Again here singleton is better than creating a new object for each request. Just avoid redundant synchronization so your threads won't block on some monitor

这篇关于Spring @Repository最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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