4

Is there a way to add a bean by creating it first manually and then add it to the spring bean registry?

Annotating the bean with @Service, @Component is having some negative impacts that I wish to control myself.

mjs
  • 21,431
  • 31
  • 118
  • 200
  • 1
    Well you could use xml or java to create all; your beans manually but why? What is the negative impact you are talking about? – M. Deinum Jan 15 '15 at 10:43
  • @M.Deinum I don't want to do it using xml. But programmatically. Actually, it might not even solve my problem I realize now. The issue is that autowired properties are null in a Service annotated class, even in postConstruct. My service extends another BaseService which Autowires a bean which in turn Autowires the transaction mananger. In postContruct the transaction manager is null. I think this is related: http://stackoverflow.com/questions/6335975/autowired-bean-is-null-when-referenced-in-the-constructor-of-another-bean – mjs Jan 15 '15 at 10:50
  • If you create beans yourself nothing will be autowired. Also dependencies are never available in constructor methods, but should be in `@PostConstruct` annotated methods. – M. Deinum Jan 15 '15 at 10:53
  • @M.Deinum Yeah, in PostConstruct the autowired property of another Autowried property is null. – mjs Jan 15 '15 at 10:54
  • That would be only the case, afaik, if the bean isn't under spring control (like creating new instances yourself) or is very eagerly instantiated in the lifecycle of the container (for instance a `BeanFactoryPostProcessor` or `BeanPostProcessor`). – M. Deinum Jan 15 '15 at 10:59
  • No, here is a better match: http://stackoverflow.com/questions/27570641/no-transactional-entitymanager-available-in-postconstruct – mjs Jan 15 '15 at 11:00
  • http://stackoverflow.com/questions/10385246/ejb-using-entitymanager-in-postconstruct-method – mjs Jan 15 '15 at 11:02
  • @M.Deinum http://stackoverflow.com/questions/17346679/transactional-on-postconstruct-method – mjs Jan 15 '15 at 11:05
  • Btw, I am configuring the transactional manager with annotations and not xml. Might the configuration beans be set up too late causing this issue? – mjs Jan 15 '15 at 11:15
  • No it isn't. The fact that a bean isn't available has nothing to do with a transactional entitymanager not being present. There is no way to ensure that `@PostContruct` is invoked after creation of the transaction proxy (or the full AOP proxy) so generally it is invoked on the unproxied instance. It has nothing to do with not being able to inject a `PlatformTransactionManager`, that is actually the work-around to have a transaction in `@PostConstruct` to do manual tx management there. – M. Deinum Jan 15 '15 at 11:21
  • @M.Deinum I am doing manual transactions. For that I need the transactional manager. But I am unable to inject the transactional manager. The services and postconstruct seems to run prior to the \@Configuration annoations which creates the entitymanagerfactory and transactional manager. – mjs Jan 15 '15 at 11:28
  • Try adding your `@Configuration` class as beans to your xml file before the ``. – M. Deinum Jan 15 '15 at 11:30

0 Answers0