1

I have a bean post processor (RepoRegistry) that picks up spring-data repositories and puts them i a map to make them available by type (repoRegistry.getRepositoryFor(MyEntity.class)). It seems as if the repo beans are created lazily. As a workaorud I now have to autowire all repositories manually somwhere so that they get created and processed by the post processor.

Is there another way to declare that some (or at least all) spring data repositories are declared non-lazy? I tried to add @Lazy to the repository interfaces without success.

James
  • 11,654
  • 6
  • 52
  • 81
  • I don't see why the lazy init is a problem. When the first method of the bean is called, it's created. Do you get errors or do you just feel this is bad? – Aaron Digulla Jul 22 '14 at 07:18
  • The bean post processor is not called with the repository bean if I don't manually @Autowire the repository somewhere. And if the post processes has not been called with this bean, it's not available in `getRepositoryFor(MyEntity.class)`. – James Jul 22 '14 at 07:24

2 Answers2

4

You're probably seeing this on a Spring Data version that is older than the Codd release train (e.g. Spring Data JPA 1.4.x). The Codd release train (and thus Spring Data JPA 1.5) switched this model to eager init by default. Prior to that, repository beans were only create if there had been an injection target.

We generally recommend to use the Repositories type we already provide with Spring Data Commons to obtain all repositories contained in a BeanFactory:

Repositories repositories = new Repositories(applicationContext);
repositories.getRepositoryFor(Person.class);
Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
  • We're using Spring Data JPA 1.6.1 though, so I'm not sure why the beans are not eagerly initialized. But anyway thanks for the tip with the `Repositories` class, which is exactly what I was looking for. It may be worth noting this useful class somewhere in the docs... ;-) – James Jul 23 '14 at 06:50
  • Do you have them annotated with `@Lazy` maybe? `Repositories` looks up the factories and uses those directly, so even lazy repository beans should be created by it. The class is not that prominently mentioned as application code shouldn't have a need to work with it. If you need it, it's usually an indicator that you write technical code that you shouldn't have to write. It's listed in the [API docs](http://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/support/Repositories.html), though. – Oliver Drotbohm Jul 23 '14 at 08:19
1

Add this method to RepoRegistry:

 @Autowired
 public void setReps( List<Repository> repos ) {...}

Note: I haven't used Spring Data, yet. You need to use the common interface type of all repos as type argument of the List.

Spring will then collect all beans which implement Repository and pass them into the setter as a list.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • I'd argue this is sub-obtimal in a variety of ways: it requires annotation processing being active, it's using setter-injection ([here](http://stackoverflow.com/questions/24337486/how-to-properly-do-dependency-injection-in-spring/24363707#24363707)'s why this is a bad idea) and repositories do not necessarily implement a common type. – Oliver Drotbohm Jul 22 '14 at 16:13