In my web application i use hibernate and spring. Entity classes that are returned from Hibernate layer need to access other service classes in some scenarios. Entity classes are not just DTO's they contains some business logic, and to perform some business logic (like may be send out emails etc when some conditions are met) these need to access service classes. Service classes are spring beans. so what's the recommended method in such scenarios to get hold of spring beans from within these entity classes which are created outside spring context?
Asked
Active
Viewed 2.1k times
14
-
2Personally, I would not like having my DTO's *access* my service classes, but rather *provide* them to the service classes as a part of the workflow of retrieving Entities from Hibernate. – nicholas.hauschild Oct 10 '13 at 17:30
-
1@nicholas.hauschild these are meant to be rich domain objects and therefore need to carry business logic. – Dev Blanked Oct 11 '13 at 12:46
-
I've worked on this app https://danjee.github.io/hedgehog/ Maybe it will be helpful in the future – Daniel Jipa Jul 11 '16 at 16:24
2 Answers
19
You are looking for Service-locator pattern,
Implementation in Spring
You can register ApplicationContextAware
and get reference to ApplicationContext
and statically serve bean
public class ApplicationContextUtils implements ApplicationContextAware {
private static ApplicationContext ctx;
private static final String USER_SERVICE = "userServiceBean";
@Override
public void setApplicationContext(ApplicationContext appContext)
throws BeansException {
ctx = appContext;
}
public static ApplicationContext getApplicationContext() {
return ctx;
}
public static UserService getUserService(){return ctx.getBean(USER_SERVICE);}
}

jmj
- 237,923
- 42
- 401
- 438
-
5But this implementation can give major error in Sonar(code conversion tool) because we are setting static field from non static method. So to avoid that you may have to create separate AppContext class where you initialize application context through static getApplicationContext() method and set context using static setApplicationContext(). Call this set method of AppContext from class implementing ApplicationContextAware like below `@Override public void setApplicationContext(ApplicationContext applicationContext) { AppContext.setApplicationContext(applicationContext); }` – Arpit Dec 16 '14 at 07:11
5
Read about @Configurable
annotation that allows to configure beans using AspectJ:
If you don't want to use AspectJ, you could use the
ApplicationContext.getAutowireCapableBeanFactory().autowireBean()
method to configure beans that live outside the spring container. (see java docs).

Jose Luis Martin
- 10,459
- 1
- 37
- 38
-
does AspectJ work with final classes and classes that are serialized to the DB and stored as BLOB? – Dev Blanked Oct 11 '13 at 12:45
-
I'm getting error Cannot make a static reference to the non-static method getAutowireCapableBeanFactory() from the type AbstractApplicationContext – Ori Marko Nov 21 '17 at 14:30