关闭

[置顶] commons configuration 自动 reload 配置文件

774人阅读 评论(0) 收藏 举报
分类:
<web.xml>

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>reload</display-name>
  
  <!-- spring -->	
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationConfig.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
	
  <!-- springmvc -->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springController.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

<applicationConfig.xml>

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	
	
	<context:annotation-config/>
	
	<context:component-scan base-package="com.jetair"/>
		
</beans>

<springController.xml>

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xmlns:mvc="http://www.springframework.org/schema/mvc"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
			       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
			       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	
	<!--开启mvc注解-->
	<mvc:annotation-driven/>
	
	<!--自动扫描包-->
	<context:component-scan base-package="com.jetair.controller"/>
						
</beans>

<jdbc.properties>

jdbc.driver = com.mysql.jdbc.Driver
jdbc.username = root
jdbc.password = root

<ConfigurationReloadDao.java>

package com.*.dao;
import java.io.File;
import java.io.IOException;
import javax.annotation.PostConstruct;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Repository;

@Repository
public class ConfigurationReloadDao {
	
	@PostConstruct  //用了这个注解,spring初始化后就会执行该方法。
	public String getData() {

		PropertiesConfiguration configuration = null;
		String string = null;

		try {
			
			if (configuration == null) {

				//我们如果填写的不是绝对路径configuration会伴我们自动搜索文件。
				//里面也有重载的方法,参数还可以为file或者url
				configuration = new PropertiesConfiguration("jdbc.properties");

				//自动reload的主要方法
				configuration.setReloadingStrategy(new FileChangedReloadingStrategy());

				System.out.println("初始化数据成功");

				//获取配置文件的jdbc.driver参数
				string = configuration.getString("jdbc.driver");
			}
		} catch (ConfigurationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//返回我们取到的数据
		return string;
	}
}

<ConfigurationReloadService.java>


package com.jetair.service;

public interface ConfigurationReloadService {
	
	public String getData();
}

<ConfigurationReloadServiceImpl.java>

package com.jetair.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jetair.dao.ConfigurationReloadDao;

@Service("configurationReloadService")
public class ConfigurationReloadServiceImpl implements ConfigurationReloadService{
	
	@Autowired  //spring为我们自动装载ConfigurationReloadDao 
	private ConfigurationReloadDao reloadDa;

	@Override
	public String getData() {
		return reloadDa.getData();
	}
}

<ConfigurationReloadController.java>

package com.*.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.jetair.service.ConfigurationReloadService;

@Controller
public class ConfigurationReloadController {

	@Autowired
	private ConfigurationReloadService reloadService;
	
	
	@RequestMapping("/handle")
	public ModelAndView executeMethod(){
	    ModelAndView model = new ModelAndView("index.jsp");
       	    String string = reloadService.getData();
	    model.addObject("msg", string);
	    return model;
	}
}

<index.jsp>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <h2>${msg}</h2>	
  </body>
</html>



我们请求http://localhost:8080/reload/handle 就会跳转到jsp页面。显示我们配置文件的数据。我们在项目运行期间改变我们获取对应配置文件的属性,保存
之后,刷新我们的jsp页面,将改变我们页面的显示,显示数据为我们修改后的数据。configuration自动加载配置文件,底层是用了文件对象的lastmodified
(文件的修改时间),其实就是一个监听器,开始监听文件的时候在内存中保存了一份文件的修改时间,监听器会隔一段时间检查一下,获取的时间是否和我们内
存中保存的时间是否相等。不想等,则会更新内存中的数据。这个包不止能操作配置文件,还可以操作xml,也可自动reloadxml。如果想进一步了解请参考我另一篇文章。
上面是项目的总结构,大家有什么问题请私信我。
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8083次
    • 积分:413
    • 等级:
    • 排名:千里之外
    • 原创:32篇
    • 转载:3篇
    • 译文:1篇
    • 评论:1条
    文章分类
    最新评论