springmvc +mybatis 动态国际化(使用数据库保存国际化配置)

springMVC的动态国际化,是使用数据库保存国际化对应的value值,系统启动后加载所有国际化配置到缓存(也可以直接读取数据库,效率低,不推荐),修改数据库中对应的国际化value值后刷新。自定义国际化的解析类,从缓存中取得对应的value值

1.定义国际化对应的实体类,以及对应的mybatis mapper类

//entry
public class SysMessageResource implement Serializable {
	
	private static final long serialVersionUID = 1L;
	private String code;		// 键值
	private String lang;		// 国家区域
	private String label;		// 显示的标签
	
	//....
	
}

// mapper
public class SysMessageResourceMapper {

    List<SysMessageResource> findList(SysMessageResource resource);
    // insert
    // update
    // delete
}

2.定义SpringContextHolder:

可以根据class直接获取对应的spring容器管理的对象,(因为我是在Utils的static块中加载的国际化资源,不能使用@autowried注入,当然也可以不用该方法,可以将Utils标为spring对象,继承InitializingBean,在afterPropertiesSet方法中加载国际化资源)

@Service
@Lazy(false)
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {

	private static ApplicationContext applicationContext = null;

	private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class);

	/**
	 * 取得存储在静态变量中的ApplicationContext.
	 */
	public static ApplicationContext getApplicationContext() {
		assertContextInjected();
		return applicationContext;
	}

	/**
	 * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		assertContextInjected();
		return (T) applicationContext.getBean(name);
	}

	/**
	 * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
	 */
	public static <T> T getBean(Class<T> requiredType) {
		assertContextInjected();
		return applicationContext.getBean(requiredType);
	}

	/**
	 * 清除SpringContextHolder中的ApplicationContext为Null.
	 */
	public static void clearHolder() {
		if (logger.isDebugEnabled()){
			logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
		}
		applicationContext = null;
	}

	/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值