Book Image

Spring Essentials

Book Image

Spring Essentials

Overview of this book

Spring is an open source Java application development framework to build and deploy systems and applications that run on the JVM. It is the industry standard and the most popular framework among Java developers with over two-thirds of developers using it. Spring Essentials makes learning Spring so much quicker and easier with the help of illustrations and practical examples. Starting from the core concepts of features such as inversion of Control Container and BeanFactory, we move on to a detailed look at aspect-oriented programming. We cover the breadth and depth of Spring MVC, the WebSocket technology, Spring Data, and Spring Security with various authentication and authorization mechanisms. Packed with real-world examples, you’ll get an insight into utilizing the power of Spring Expression Language in your applications for higher maintainability. You’ll also develop full-duplex real-time communication channels using WebSocket and integrate Spring with web technologies such as JSF, Struts 2, and Tapestry. At the tail end, you will build a modern SPA using EmberJS at the front end and a Spring MVC-based API at the back end.By the end of the book, you will be able to develop your own dull-fledged applications with Spring.
Table of Contents (14 chapters)
Spring Essentials
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Handling resources


Spring Framework provides excellent support for accessing low-level resources, thus solving many limitations of Java's standard java.net.URL and standard handlers. The org.springframework.core.io.Resource package and its many concrete implementations form a solid foundation for Spring Framework's robust resource handling.

Resource abstraction is used extensively in Spring itself, inside many implementations of ApplicationContext—it's actually very useful to use as a general utility class by itself in your own code in order to access resources. You will find the following resource implementations that come supplied right out of the box in Spring:

Resource Implementation

Description

UrlResource

It wraps java.net.URL and is useful for accessing anything that can be accessed via a URL, such as files (file:///), HTTP targets (http://), and FTP targets (ftp://).

ClassPathResource

It is used for accessing any resource from classpath using the prefix classpath:

FileSystemResource

This is the resource implementation of java.io.File.

ServletContextResource

This is the parent bean for inheriting configuration data from a parent bean definition.

InputStreamResource

This is the resource implementation for a given InputStream.

Generally, you do not directly instantiate any of these resources; rather, you use a ResourceLoader interface to do that job for you. All ApplicationContext implement a ResourceLoader interface; therefore, any ApplicationContext can be used to obtain resource instances. The code for this is as follows:

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"application-context.xml"});
Resource classPathResource = ctx.getResource("classpath:scripts/tasks-schema.sql");

Resource fileResource = ctx.getResource("file:///scripts/master-data.sql");

Resource urlResource = ctx.getResource("http://country.io/names.json");

You can inject resources into your beans by simply passing the filename or URL of your resource as an argument, as shown here. ApplicationContext, which is a ResourceLoader interface, will create an instance of an appropriate resource implementation based on the URL you supply:

@Value("http://country.io/names.json")
private Resource countriesResource;

Here is the XML version of injecting a resource:

<property name="countriesResource" value="http://country.io/names.json"/>