spring - Bean injection inside a JPA @Entity -
is possible inject beans jpa @entity using spring's dependency injection?
i attempted @autowire servletcontext but, while server did start successfully, received nullpointerexception when trying access bean property.
@autowired @transient servletcontext servletcontext;
you can inject dependencies objects not managed spring container using @configurable explained here: http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/aop.html#aop-atconfigurable.
as you've realized now, unless using @configurable , appropriate aspectj weaving configuration, spring not inject dependencies objects created using new operator. in fact, doesn't inject dependencies objects unless you've retrieved them applicationcontext, simple reason doesn't know existence. if annotate entity @component, instantiation of entity still performed new operation, either or framework such hibernate. remember, annotations metadata: if no 1 interprets metadata, not add behaviour or have impact on running program.
all being said, advise against injecting servletcontext entity. entities part of domain model , should decoupled delivery mechanism, such servlet-based web delivery layer. how use entity when it's accessed command-line client or else not involving servletcontext? should extract necessary data servletcontext , pass through traditional method arguments entity. achieve better design through approach.
Comments
Post a Comment