How can I access the spring context ?
There are 2 cases :
a) In an application
b) In a test application
a) In an application
1) You have to implement the interface ApplicationContextAware
example :
public class TelemetryConsumptionSynchronizer extends BaseSynchronizer implements ApplicationContextAware
2) Implementation of the method setApplicationContext
...
ApplicationContext applicationContext;
...
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
logger.debug("entering setApplicationContext");
this.applicationContext = applicationContext;
logger.debug("leaving setApplicationContext");
}
b) In a test application
It is more easy because you only have to inject the context.
example :
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
locations = {
"classpath:el1EnergyCommSyncContextTest.xml"
}
)
public class TelemetryConsumptionSynchronizerTest {
...
@Resource
private ApplicationContext applicationContext;
}
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment