This is a Spring Boot autoconfiguration and starter artifact for Database Resource Bundle
which is an database backed implementation of java.util.ResourceBundle
.
- Add dependency via Jitpack
- Create a JPA entity class which implements
BundleEntity
- Populate the table which corresponds to the entity class you've declared in step 2 and you're ready to roll.
See usage demo project for code samples.
This starter instantiates a bean of JpaBundleContentLoaderStrategy
if there is no bean of type BundleContentLoaderStrategy
is present in context.
When using JpaBundleContentLoaderStrategy
you need to have an entity which implements BundleEntity
.
By default, this starter will scan for an entity which implements BundleEntity
. However, this takes time (~1s) and decrease your startup time
(but has no effect on runtime performance). If startup time is important for you then you can instantiate a bean of JpaBundleContentLoaderStrategy
where you explicitly declare your entity class. So that there won't be any scanning.
@Bean
BundleContentLoaderStrategy bundleContentLoaderStrategy(EntityManager entityManager) {
return new JpaBundleContentLoaderStrategy<>(entityManager, MyBundleEntity.class);
}
DefaultBundleContentLoaderStrategy
uses a prepared statement to load resource content.
By default it assumes you have a table in your database which
is created via database-resource-bundle.jar/create.sql
. Hence uses following queries:
DEFAULT_LOAD_QUERY = "SELECT DISTINCT b.key, b.value FROM " + DEFAULT_TABLE_NAME + " b WHERE name = ? AND language = ? AND country = ? AND variant = ? ;"
DEFAULT_NEEDS_RELOAD_QUERY = "SELECT MAX(last_modified) FROM " + DEFAULT_TABLE_NAME + " b WHERE name = ? ;";
DEFAULT_TABLE_NAME
is Bundle
and coming from BundleContentLoaderStrategy.DEFAULT_TABLE_NAME
.
You can override those queries by using DefaultBundleContentLoaderStrategy
's 3-parameter constructor. When you do this, you can use any database
table structure or name as you like as long as you conform to expected return values of the queries.
Expected return values of the load query
is a key-value string pair.
Expected return values of the needs reload query
is a long value which represents the epoch milliseconds of latest change on the table.
This starter;
- Instantiates a bean of
JpaBundleContentLoaderStrategy
if there is no bean of typeBundleContentLoaderStrategy
is present in context. If there is a bean of typeBundleContentLoaderStrategy
, for exampleDefaultBundleContentLoaderStrategy
or your custom implementation, thenJpaBundleContentLoaderStrategy
is not instantiated (hence there won't be a class scanning for an entity class implementingBundleEntity
) and the existing bean is used. - Configures a
DatabaseResourceBundleMessageSource
which uses theBundleContentLoaderStrategy
implementation. DatabaseResourceBundleMessageSource
is aMessageSource
so you don't need to configure anything else, just use Spring'smessages.properties
support as before but this time instead of aproperties
file it's backed by a database table.
You can use Spring Boot's message source properties to change DatabaseResourceBundleMessageSource
's behavior:
spring.messages.always-use-message-format=false # Whether to always apply the MessageFormat rules, parsing even messages without arguments.
spring.messages.basename=messages # Comma-separated list of basenames (essentially a fully-qualified classpath location), each following the ResourceBundle convention with relaxed support for slash based locations.
spring.messages.cache-duration= # Loaded resource bundle files cache duration. When not set, bundles are cached forever. If a duration suffix is not specified, seconds will be used.
spring.messages.encoding=UTF-8 # Message bundles encoding.
spring.messages.fallback-to-system-locale=true # Whether to fall back to the system Locale if no files for a specific Locale have been found.
spring.messages.use-code-as-default-message=false # Whether to use the message code as the default message instead of throwing a "NoSuchMessageException". Recommended during development only.
See also MessageSourceProperties;
© Copyright 2018 Kod Gemisi Ltd.
Mozilla Public License 2.0 (MPL-2.0)
https://tldrlegal.com/license/mozilla-public-license-2.0-(mpl-2)
MPL is a copyleft license that is easy to comply with. You must make the source code for any of your changes available under MPL, but you can combine the MPL software with proprietary code, as long as you keep the MPL code in separate files. Version 2.0 is, by default, compatible with LGPL and GPL version 2 or greater. You can distribute binaries under a proprietary license, as long as you make the source available under MPL.