Java Reference
In-Depth Information
String alert = context.getMessage("alert.checkout",
new Object[] { 4, new Date() }, Locale.US);
System.out.println(alert);
}
}
In the Main class, you can resolve text messages because you can access the application context
directly. But for a bean to resolve text messages, it has to implement either the ApplicationContextAware
interface or the MessageSourceAware interface. Now you can delete the message resolution from the
Main class.
package com.apress.springenterpriserecipes.shop;
...
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
public class Cashier implements MessageSourceAware {
...
private MessageSource messageSource;
public void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
}
public void checkout(ShoppingCart cart) throws IOException {
...
String alert = messageSource.getMessage("alert.checkout",
new Object[] { total, new Date() }, Locale.US);
System.out.println(alert);
}
}
1-9. Loading External Resources
Problem
Sometimes your application may need to read external resources (e.g., text files, XML files, properties
file, or image files) from different locations (e.g., a file system, classpath, or URL). Usually, you have to
deal with different APIs for loading resources from different locations.
Solution
Spring's resource loader provides a unified getResource() method for you to retrieve an external
resource by a resource path. You can specify different prefixes for this path to load resources from
different locations. To load a resource from a file system, you use the file prefix. To load a resource
from the classpath, you use the classpath prefix. You may also specify a URL in this resource path.
 
Search WWH ::




Custom Search