1.问题:公司项目开发过程中主要三种环境:
a.本地开发运行调试
b.测试环境运行
c.生产运行
每次由部署到生产环境之前都需要改一堆的参数配置包括:
* 数据库连接配置参数
* zookeeper连接地址配置
* 其他对接公司的参数包括代理商号,签名key,请求地址等信息
2.解决:
为了解决上面的问题,一共进行两方面的设置:
* 对接上游的代码封装在了jar包中,参数原本是写在jar包中,每次切换环境都要重新打jar包
解决办法:将代码中读取参数的部分更改为:读取外部引用jar包的web项目的config.properties文件来完成的
* 将web项目中resources文件夹新建开发版,测试版,生产版不同的文件夹,通过spring-profile设置不同的启动选择不同的参数
参考:https://www.cnblogs.com/strugglion/p/7091021.html
3.按照上面的配置完成操作,开始运行程序发现对于我的项目还有些配置没有注意到:
其中比较明显的两出问题:
* profile指定无法指定xml格式的数据;在设置了xml类型的文件,会报错:Document root element “beans” must match DOCTYPE root “null”
开始以为是自己的xml文件中的配置错误,后来反复检查确实没有问题,最终将xml移除,只保留properties文件,profile 生效
* web.xml中不只要在context-param设置spring.profiles.default或spring.profiles.active;
在servlet dispatcherServlet中也要配置这个初始参数
<?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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"; id="WebApp_ID" version="3.0">
<display-name>OneCodePay_service_v1</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-config.xml;
</param-value>
</context-param>
<context-param>
<description>设置profile.default的值</description>
<param-name>spring.profiles.default</param-name>
<param-value>development</param-value>
</context-param>
<context-param>
<description>设置profile.active的值</description>
<param-name>spring.profiles.active</param-name>
<param-value>development</param-value>
</context-param
<listener>
<description>spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<description>Introspector缓存清除监听器</description>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<description>request监听器</description>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
<description>用于在非controller中获取response的监听器</description>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<filter>
<description>字符集过滤器</description>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<description>字符集编码</description>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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:spring-mvc.xml</param-value>
</init-param>
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>development</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>
<servlet>
<description></description>
<display-name>WechatCallbackApi</display-name>
<servlet-name>WechatCallbackApi</servlet-name>
<servlet-class>com.concomm.web.controller.WechatCallbackApi</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WechatCallbackApi</servlet-name>
<url-pattern>/WechatCallbackApi</url-pattern>
</servlet-mapping>
</web-app>
还有需要注意的地方:在spring.xml和spring mvc.xml的两个配置文件中,引用配置的beans配置文件profile.xml需要放在文件最下面,即网上查到的在所有beans配置最后面
018-01-31续:
上述配置完后,当时xml中应用的properties中的变量能够读取到,就以为没有问题了,结果在项目运行测试过程中,发现代码中读取还是不能读取到development包下的config.properties文件,报错找不到这个文件;
两个思路:1.修改读取配置的代码;2.修改配置
最终修改了读取配置的代码实现了正常的读取:
出现读取不到问题的代码:
ResourceBundle config = PropertyResourceBundle.getBundle(“config”, Locale.getDefault());
config.getString(“notifyurl”);//读取properties文件中key为notifyurl的数值
修改后使用spring的@Value注解实现配置文件中数据的读取
修改常量的读取和profile的配置xml中指定property读取路径
首先
1.代码中
@Value("${notify_pay_url_b}")
private String notify_pay_url_b;
@Value("${notify_pay_url_f}")
private String notify_pay_url_f;
在使用的类上要加注解@Component,spring可以扫描此类,并扫描定义的变量来完成赋值
2.profile的配置xml中也要指定对应的properties路径
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" 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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd">
<description>spring profile配置</description>
<beans profile="development">
<context:property-placeholder
location="classpath*:common/*.properties,
classpath*:development/*.properties" />
<bean id="propertyBean"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:development/*.properties</value>
</array>
</property>
</bean>
</beans>
<beans profile="test">
<context:property-placeholder
location="classpath*:common/*.properties,
classpath*:test/*.properties" />
<bean id="propertyBean"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:test/*.properties</value>
</array>
</property>
</bean>
</beans>
<beans profile="production">
<context:property-placeholder
location="classpath*:common/*.properties,
classpath*:production/*.properties" />
<bean id="propertyBean"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:production/*.properties</value>
</array>
</property>
</bean>
</beans>
</beans>
xml中class为
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
的bean定义 property的name为locations 值为对应路径
此bean的id可以根据需要自定义,并无要求
至此时,jar包中的代码读取配置文件还有问题,需要继续寻找解决方案