Wednesday, May 16, 2012
How can I read a resource relative to the classpath ?
How can I read a resource relative to the classpath ?
You have to get the applicationcontext. I have blogged on this topic.
a) Create a resource that has a reference to a path
example :
public org.springframework.core.io.Resource getResource() {
logger.debug("entering getResource");
org.springframework.core.io.Resource resource = applicationContext.getResource("classpath:telemetry/telemetrieA+Cons20090101_200906112209055.csv");
logger.debug("leaving getResource");
return resource;
}
b) Read the resource
example :
CSVReader reader = new CSVReader(new FileReader(getResource().getFile()));
That's all folks !
How can I access the spring context ?
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;
}
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;
}
Tuesday, May 18, 2010
The bean identifiers (id and name)
http://static.springsource.org/spring/docs/1.2.9/reference/beans.html#beans-beanname
- Every bean has one or more ids (also called identifiers, or names; these terms refer to the same thing). These ids must be unique within the BeanFactory or ApplicationContext the bean is hosted in. A bean will almost always have only one id, but if a bean has more than one id, the extra ones can essentially be considered aliases.
In an XmlBeanFactory (including ApplicationContext variants), you use the id or name attributes to specify the bean id(s), and at least one id must be specified in one or both of these attributes. The id attribute allows you to specify one id, and as it is marked in the XML DTD (definition document) as a real XML element ID attribute, the parser is able to do some extra validation when other elements point back to this one. As such, it is the preferred way to specify a bean id. However, the XML spec does limit the characters which are legal in XML IDs. This is usually not really a constraint, but if you have a need to use one of these characters, or want to introduce other aliases to the bean, you may also or instead specify one or more bean ids (separated by a comma (,) or semicolon (;) via the name attribute.
Subscribe to:
Comments (Atom)
