In this tutorial we will focus on how to load ehCache.xml from external location Spring Boot. Out of the box the ehCache is looking for the ehCache.xml configuration file in resources path which is packed into jar file. In case when you want to use external ehCache.xml configuration file to can use the VM options and pass there appropriate value.
Table of Contents
What is JAVA?
Java is a popular programming language used for building a wide range of applications. It is known for its simplicity, versatility, and scalability, which makes it a popular choice for developing applications for various domains.
What is Spring Boot?
Pivotal maintains the Spring Boot micro framework as an open-source project. It provides a framework for Java developers to get started with a production-grade Spring application that is auto customizable. It allows developers to get up and running quickly without having to waste time planning and configuring their Spring application.
Spring Boot is a framework that allows developers to build standalone, production-grade applications based on the Spring framework. It provides a number of features and tools that make it easier to develop Spring-based applications, including:
- Automatic configuration of Spring-based applications
- Support for embedded servers, such as Tomcat and Jetty
- Support for a wide range of data access technologies, including JDBC, Hibernate, and JPA
- A powerful and flexible MVC (Model-View-Controller) architecture
- Support for testing, including unit testing and integration testing
Spring Boot is a popular choice for building microservices and other types of web-based applications. It makes it easy to get started with Spring development and helps developers build robust, high-performing applications quickly and efficiently.
What is Java Annotation?
In Java, an annotation is a special kind of syntax that can be used to provide additional information about a program element, such as a class, method, or variable. An annotation is similar to a comment, but it has a more structured format and can be processed by the Java compiler or other tools at compile time or runtime.
Annotations are defined using the
@ symbol, followed by the name of the annotation and a set of optional element-value pairs. For example:
@AnnotationName(element1 = "value1", element2 = "value2")
Annotations can be used to provide metadata about a program element, such as its purpose, visibility, or behavior. They can also be used to specify certain directives or instructions to the Java compiler or other tools, such as code generation, optimization, or error checking.
There are several built-in annotations in Java, such as
@SuppressWarnings, which provide information about how a program element should be used or handled. In addition, developers can define custom annotations to provide their own metadata or directives in their Java code.
Few More Words
This article will walk you through the basics of Spring Boot and how to use it.
But, before we get started, let’s have a look at the underlying tool: Spring. Why? To establish a constructive assessment about Spring Boot, you must first understand Spring.
What is EhCache (EhCache.xml?)
ehCache is an open source, standards-based cache that boosts performance, offloads your database, and simplifies scalability. It’s the most widely-used Java-based cache because it’s robust, proven, full-featured, and integrates with other popular libraries and frameworks. ehCache scales from in-process caching, all the way to mixed in-process/out-of-process deployments with terabyte-sized caches.
ehCache is a Java-based in-memory cache that can be used to improve the performance of applications by storing frequently accessed data in memory. It is open source and widely used in Java-based enterprise applications to provide a fast cache for frequently accessed data, reducing the need to access the database or other external resources.
ehCache is designed to be easy to use and configure, and it offers features such as cluster support, disk storage, and cache persistence. It can be used as a standalone cache or integrated with other frameworks and technologies, such as Hibernate and Spring.
In general, using a cache like ehCache can help improve the performance of an application by reducing the number of expensive database or resource lookups that are required, and by providing a fast in-memory store for frequently accessed data.
Configure EhCache With Spring Boot
Add the ehCache dependency to the project’s pom.xml file. The newest version you can find in Maven repository.
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>126.96.36.199</version> </dependency>
The most important part is the “file:” prefix before the path. Without it the application will looking for the configuration file in the classpath.
Ehcache Spring Boot Configuration
Use @Cacheable Annotation
Annotate the methods in the service layer with
@Cacheable to enable caching for those methods.
Clear Cache By @CacheEvict Annotation
@CacheEvict annotation to specify when to clear the cache.
Ehcache 3.10 is now available!
The major feature in this release is addition of new variant JARs that adopt the Jakarta EE namespaced JTA and JAXB APIs.
It requires Java 8+.
- Revamped API that leverages Java generics and simplifies Cache interactions,
- Full compatibility with javax.cache API (JSR-107),
- Offheap storage capabilities, including offheap only caches,
- Out of the box Spring Caching and Hibernate integration thanks to the javax.cache support,
- And many more …
More Java And Spring Boot
Could You Please Share This Post? I appreciate It And Thank YOU! :) Have A Nice Day!
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?