springMVC,启动tomcat的时候向web.xml中注入值,如jdbc和根据操作系统不同加载不同的配置,特别是切换日志目录(2)

3 篇文章 0 订阅

2019-07-23 1514 星期二
web项目,springMVC通过启动tomcat的时候向web.xml中注入值,如jdbc和根据操作系统不同加载不同的配置(2)
参考: https://blog.csdn.net/peaceForEveryOne/article/details/78126344#commentBox
关键点:
监听器: System.setProperty(str_dubbo_registry_application, dubbo_registry_application);
配置文件内容:<dubbo:application name="#{systemProperties[‘dubbo.registry.application’]}" />
属性文件内容: dubbo.catalina.home=dubbo缓存文件位置
1.鉴听器

package com.ecommerce.platform.listener;

import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.ecommerce.platform.common.exception.CommonConfigException;
import com.ecommerce.platform.common.util.LoadProperties;

/**
 * 自定义监听器,加载属性文件中定义的变量值,在web.xml中使用,特别是配置log4j的值
 * @author enAn,2019-07-22
 * 参考:https://blog.csdn.net/liufunan/article/details/50715167
 */
public class InitSystemConfigListener implements ServletContextListener {
	private static final String str_dubbo_registry_application 	= "dubbo.registry.application";//dubbo注册的应用名字
	private static final String str_dubbo_registry_address 		= "dubbo.registry.address";//dubbo注册的地址
	private static final String str_dubbo_catalina_home 		= "dubbo.catalina.home";//dubbo缓存文件位置
	private static final String str_log_home_dir 				= "log.home.dir";//log日志文件位置
	private static final String str_dubbo_catalina_home_win 	= "dubbo.catalina.home.win";
	private static final String str_dubbo_catalina_home_linux 	= "dubbo.catalina.home.linux";
	private static final String str_log_dir_win 				= "log.dir.win";
	private static final String str_log_dir_linux 				= "log.dir.linux";
	private static final String str_win							= "win";
	private static final String str_linux 						= "linux";
	
	 public void contextDestroyed(ServletContextEvent sce) {  
	        System.out.println("web exit ... ");  
	    }
	    public void contextInitialized(ServletContextEvent sce) {
	        try {
	        	String log_dir = "";
				String dubbo_catalina_home = "";
				System.out.println("system init .......");
				Properties props= System.getProperties(); //获得系统属性集   
				String os = props.getProperty("os.name");  
				String osVersion = props.getProperty("os.version"); //操作系统版本 
				String javaVersion = props.getProperty("java.version"); //Java 运行时环境版本
				//加载web.xml中的配置信息
				//将Map通过ServletContext存储到全局作用域中
				ServletContext sct = sce.getServletContext(); 
				System.out.println("项目名称:" + sct.getInitParameter("MakroProject"));
				String classpath = this.getClass().getResource("/").getPath().replaceFirst("/", "");
				//System.out.println("classpath:" + classpath);
				String webappRoot = classpath.replaceAll("WEB-INF/classes/", "");
				//System.out.println("webappRoot:" + webappRoot);
				String configPath = webappRoot + "WEB-INF/properties/init.properties";
				System.out.println("配置文件全路径:" + configPath);
				String dubbo_registry_application = LoadProperties.getProperty(configPath, str_dubbo_registry_application, "");
				String dubbo_registry_address = LoadProperties.getProperty(configPath, str_dubbo_registry_address, "");
				if ( os != null && os.toLowerCase().startsWith(str_win)) { //window操作系统
					dubbo_catalina_home = LoadProperties.getProperty(configPath, str_dubbo_catalina_home_win, "");
					log_dir = LoadProperties.getProperty(configPath, str_log_dir_win,"");
				}else if(os != null && os.toLowerCase().startsWith(str_linux)) {//linux操作系统
					dubbo_catalina_home = LoadProperties.getProperty(configPath, str_dubbo_catalina_home_linux, "");
					log_dir = LoadProperties.getProperty(configPath, str_log_dir_linux,"");
				}
				String altMsg = "配置属性文件有误,请检查:";
				if(null == dubbo_registry_application || dubbo_registry_application.equals("")) {
					throw new CommonConfigException(altMsg + "dubbo注册的应用名字");
				}
				if(null == dubbo_registry_address || dubbo_registry_address.equals("")) {
					throw new CommonConfigException(altMsg + "dubbo注册的地址");
				}
				if(null == dubbo_catalina_home || dubbo_catalina_home.equals("")) {
					throw new CommonConfigException(altMsg + "dubbo缓存文件位置");
				}
				if(null == log_dir || log_dir.equals("")) {
					throw new CommonConfigException(altMsg + "log日志文件位置");
				}
				props.setProperty(str_dubbo_registry_application, dubbo_registry_application);//dubbo注册的应用名字
				props.setProperty(str_dubbo_registry_address, dubbo_registry_address);//dubbo注册的地址
				props.setProperty(str_dubbo_catalina_home, dubbo_catalina_home);//dubbo缓存文件位置
				props.setProperty(str_log_home_dir, log_dir);//log日志文件位置
				
				sct.setAttribute(str_dubbo_registry_application, dubbo_registry_application);
				sct.setAttribute(str_dubbo_registry_address, dubbo_registry_address);
				sct.setAttribute(str_dubbo_catalina_home, dubbo_catalina_home);
				sct.setAttribute(str_log_home_dir, log_dir);
				
				//***********设置到系统里面**********************
				System.setProperty(str_dubbo_registry_application, dubbo_registry_application);
				System.setProperty(str_dubbo_registry_address, dubbo_registry_address);
				System.setProperty(str_dubbo_catalina_home, dubbo_catalina_home);
				System.setProperty(str_log_home_dir, log_dir);
				System.out.println("当前操作系统是 :" +  os);
				System.out.println("操作系统的版本 :" + osVersion  );
				System.out.println("java版本 :" + javaVersion );//当前操作系统是:Windows 7
				System.out.println("dubbo注册的应用名字:" + dubbo_registry_application);
				System.out.println("dubbo注册的地址:" + dubbo_registry_address);
				System.out.println("dubbo缓存文件位置:" + dubbo_catalina_home);
				System.out.println("log日志文件位置:" + log_dir);
				System.out.println(sct.getAttribute(str_dubbo_registry_application));
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException(e.getMessage());
			}
	    }
}

2.springmvc配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 
          
     <mvc:annotation-driven />  
<!-- 	<context:property-placeholder location="classpath:../properties/init.properties"/> -->
	
    <!-- 启用spring mvc 注解 -->
    <context:annotation-config />
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
	<dubbo:application name="#{systemProperties['dubbo.registry.application']}" />
	
	<!-- 使用zookeeper注册中心暴露服务地址 --> 
	<dubbo:registry protocol="zookeeper" address="#{systemProperties['dubbo.registry.address']}" file="#{systemProperties['dubbo.registry.address.file']}"/>

	<!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
	<!-- 样例服务,begin -->
	<!-- dubbo:reference 增加属性check="false"表示,启动的时候不检查提供者是否已经启动 -->
	<dubbo:reference id="interface_demoService" 								interface="com.ecommerce.platform.demo.server.IDemoService" 						check="false"	generic="false"/>
	<!-- 样例服务,end -->
	<!-- 接口服务 -->
	<dubbo:reference id="interface_interfaceService" 							interface="com.ecommerce.platform.server.IInterfaceService" 						check="false"	generic="false"/>
	<dubbo:reference id="interface_orghierService" 								interface="com.ecommerce.platform.server.IOrghierService" 							check="false"	generic="false"/>
	<dubbo:reference id="interface_goodsCategoryService" 						interface="com.ecommerce.platform.server.IGoodsCategoryService" 					check="false"	generic="false"/>
	<dubbo:reference id="interface_goodsService" 								interface="com.ecommerce.platform.server.IGoodsService" 							check="false"	generic="false"/>
	<dubbo:reference id="interface_authLogService" 								interface="com.ecommerce.platform.server.IAuthLogService" 							check="false"	generic="false"/>
	<dubbo:reference id="interface_membersService" 								interface="com.ecommerce.platform.server.IMembersService" 							check="false"	generic="false"/>
	<dubbo:reference id="interface_mktGoodsRelationService" 					interface="com.ecommerce.platform.server.IMktGoodsRelationService" 					check="false"	generic="false"/>
	<dubbo:reference id="interface_sysDataService" 								interface="com.ecommerce.platform.server.ISysDataService" 							check="false"	generic="false"/>
	<dubbo:reference id="interface_brandService" 								interface="com.ecommerce.platform.server.IBrandService" 				 			check="false"	generic="false"/>
	<dubbo:reference id="interface_orderService" 								interface="com.ecommerce.platform.server.IOrderService" 							check="false"	generic="false"/>
	<dubbo:reference id="interface_huiGoService" 								interface="com.ecommerce.platform.server.IHuiGoService" 							check="false"	generic="false"/>
	<dubbo:reference id="interface_commonService" 								interface="com.ecommerce.platform.server.ICommonService" 							check="false"	generic="false"/>
	
	<!-- 计划任务 -->
	<dubbo:reference id="plnadapter_plnAdapterLogService" 						interface="com.ecommerce.platform.plnadapter.service.IPlnAdapterLogService" 		check="false"	generic="false"/>
	<dubbo:reference id="plnadapter_plnAdapterTaskService" 						interface="com.ecommerce.platform.plnadapter.service.IPlnAdapterTaskService" 		check="false"	generic="false"/>
	<dubbo:reference id="plnadapter_plnAdapterTaskConfigService" 				interface="com.ecommerce.platform.plnadapter.service.IPlnAdapterTaskConfigService" 	check="false"	generic="false"/>
	<dubbo:reference id="plnadapter_procedureService" 							interface="com.ecommerce.platform.plnadapter.procedure.server.IProcedureService" 	check="false"	generic="false"/>
	
	<!-- 上面的是ok的 -->
	
    <!-- 设置使用注解的类所在的包 -->
    <!-- 扫包1 -->
	<context:component-scan base-package="com.ecommerce.platform.web.controller" />
    <!-- 扫包2 -->
	<context:component-scan base-package="com.ecommerce.platform.demo.web.controller"/>
	 <!-- 扫包3 -->
	<context:component-scan base-package="com.ecommerce.platform.web.test" />
	
	
    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->  
    <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />  
        <property name="prefix" value="/WEB-INF/pages/" />  
        <property name="suffix" value=".jsp" />  
    </bean>
    
	<!-- 避免IE在ajax请求时,返回json出现下载 -->
	<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>application/json;charset=UTF-8</value>
			</list>
		</property>
	</bean>
	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJacksonHttpMessageConverter" />
			</list>
		</property>
	</bean>
	
    
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 设置上传文件的最大尺寸为5MB -->
		<property name="maxUploadSize">
			<value>5242880</value>
		</property>
	</bean>
	
	<!-- SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException -->  
    <!-- 该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 -->  
    <bean id="exceptionResolver"  
        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
        <property name="exceptionMappings">  
            <props>  
                <!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到/WEB-INF/jsp/error_fileupload.jsp页面 -->  
                <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error_fileupload</prop>  
            </props>  
        </property>  
    </bean>  
    
</beans>

3.属性文件 init.properties

##[dubbo安装在哪台机器上,必需指向到哪台机器,与提供者注册的机器一致]##

##[dubbo服务注册地址:服务器上]##
#dubbo.registry.address=119.29.179.201:2181
dubbo.registry.application=ecommerce-web
##[dubbo服务注册地址:开发本地机器]##
dubbo.registry.address=127.0.0.1:2181

##等找到参考文档,在启动tomcat的时候,可以向web.xml注入值,再启用 ##
##[dubbo服务缓存文件位置]##
dubbo.catalina.home=dubbo缓存文件位置
dubbo.catalina.home.win=C:/ecom/dubbo-registry-catalina/ecommerce-web/dubbo-registry.properties
dubbo.catalina.home.linux=/home/ecom/dubbo-registry-catalina/ecommerce-web/dubbo-registry.properties

##[log日志文件位置]##
log.dir=log日志文件位置
log.dir.win=C:/ecom/log/ecommerce-web/web.log
log.dir.linux=/home/ecom/log/ecommerce-web/web.log

## 需要改变的地址,打包的时候,以下二选一 ##
## 打包到win系统  ##
dubbo.registry.address.file=C:/ecom/dubbo-registry-catalina/ecommerce-web/dubbo-registry.properties
log.home.dir=C:/ecom/log/ecommerce-web/web.log
## 打包到linux系统  ##
#dubbo.registry.address.file=/home/ecom/dubbo-registry-catalina/ecommerce-web/dubbo-registry.properties
#log.home.dir=/home/ecom/log/ecommerce-web/web.log


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值