自留地

得之坦然,失之淡然

plugin

PF4J是一个Java轻量级的插件框架,可以实现动态加载,执行,卸载外部插件(支持jar以及zip),具体可以看官网介绍

本文例子基于Github地址:https://github.com/pf4j/pf4j

1
2
3
4
5
<dependency>
<groupId>org.pf4j</groupId>
<artifactId>pf4j</artifactId>
<version>3.0.1</version>
</dependency>
阅读全文 »

规约模式(Specification Pattern),简单来说就是就是约束条件,例如从数据库中获取到满足约束条件的数据(where条件之后即为约束条件):

1
select * from `book` where `name` like `%java%` and `price` < 50;

如果我们要用java实现类似约束,可能会想到:

1
2
3
4
5
for (Book book : books) {
if (book.name.contains("java") && book.price < 40) {
System.out.println(book);
}
}
阅读全文 »

摘自Environment注释:

Properties play an important role in almost all applications, and may originate from a variety of sources: properties files, JVM system properties, system environment variables, JNDI, servlet context parameters, ad-hoc Properties objects, Maps, and so on. The role of the environment object with relation to properties is to provide the user with a convenient service interface for configuring property sources and resolving properties from them.

阅读全文 »

摘自Environment注释:

A profile is a named, logical group of bean definitions to be registered with the container only if the given profile is active. Beans may be assigned to a profile whether defined in XML or via annotations; see the spring-beans 3.1 schema or the @Profile annotation for syntax details. The role of the Environment object with relation to profiles is in determining which profiles (if any) are currently active, and which profiles (if any) should be active by default.

profiles类似Maven中的profiles,Spring允许配置多个profile,但只加载激活的profile,通过profiles配置,可以实现不同环境使用不同的配置(例如不同环境使用不同的数据库配置)

阅读全文 »

Environment

Environment表示应用当前所运行的环境。

Interface representing the environment in which the current application is running. Models two key aspects of the application
environment: profiles and properties. Methods related to property access are exposed via the PropertyResolver superinterface.

A profile is a named, logical group of bean definitions to be registered with the container only if the given profile is active. Beans may be assigned to a profile whether defined in XML or via annotations; see the spring-beans 3.1 schema or the @Profile annotation for syntax details. The role of the Environment object with relation to profiles is in determining which profiles (if any) are currently active, and which profiles (if any) should be active by default.

Properties play an important role in almost all applications, and may originate from a variety of sources: properties files, JVM system properties, system environment variables, JNDI, servlet context parameters, ad-hoc Properties objects, Maps, and so on. The role of the environment object with relation to properties is to provide the user with a convenient service interface for configuring property sources and resolving properties from them

阅读全文 »

一致性Hash算法

需求

在使用n台缓存服务器时,一种常见的负载均衡的做法是:对资源o的请求做hash(o) mod n,来决定映射的到的服务器。当需要增加或减少缓存服务器时,最糟糕的情况是,因为所有hash(o) mod n值被改变,导致所有缓存失效。

一致性哈希要求在服务器数量变化时,也尽量使同一个资源的请求,落在同一台服务器上。

阅读全文 »

本文主要介绍springMVC中国际化的相关配置

MessageSource

示例:

1
2
3
4
5
6
7
8
@Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames("classpath:i18n/server");
messageSource.setDefaultEncoding("UTF-8");
messageSource.setFallbackToSystemLocale(false);
return messageSource;
}

SpringBean名称必须是messageSource

阅读全文 »

本文主要介绍国际化中比较重要的类LocaleResourceBundleMessageFormat

java.util.Locale

Locale表示地区,在国际化中是一个非常重要的类。

基本方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Locale;

public class LocaleTest {

public static void main(String[] args) {
Locale locale = Locale.getDefault();
System.out.println(locale); // zh_CN
System.out.println(locale.getCountry()); // CN
System.out.println(locale.getLanguage()); // zh
System.out.println(locale.getDisplayCountry()); // 中国
System.out.println(locale.getDisplayLanguage()); // 中文
}

}

可以看出Locale其实是由两部分组成,即{语言}_{地区}

阅读全文 »