-1

Inside my entities managed by spring data I need some services that should be autowired by spring.

public class MyEntity {
  @Autowired
  private SomeService service;

  @Id
  private String id;

  ...
}

Is it possible to tell spring to autowire the given service when loading that entity?

I know I could do something like this:

public class Worker {
  @Autowired
  private AutowireCapableBeanFactory autowireBeanFactory;

  @Autowired
  private MyEntityRepository repo;

  public void doSomething() {
    MyEntity entity = repo.findOne("1");
    autowireBeanFactory.autowireBean(entity);
    entity.useService();
  }
}

Can I automate that autowiring?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
mibutec
  • 2,929
  • 6
  • 27
  • 39
  • 2
    It is no good design. Entities are abstraction of persistence object. This should not use any services. – Jens Apr 10 '15 at 08:11
  • Why? Why must I implement service over service to do things an entity could do on its own? – mibutec Apr 10 '15 at 09:39

1 Answers1

0

Finally I had to overwrite the MappingMongoConverter. How that can be done is described under Set MongoDb converter programatically

Community
  • 1
  • 1
mibutec
  • 2,929
  • 6
  • 27
  • 39