背景
一个项目应用可能在多个环境下运行,比如开发的dev环境,线上的prod生产环境,或者QA的test测试环境,每一种环境可能对应的配置也有可能不同,比如DataSource。我想没有哪个公司愿意把线上环境交给程序员做开发使用的。
profiles
在spring3引入的新功能,可以用于指定spring的运行环境,也就是设置一个全局变量:
spring.profiles.active
使用过springboot的就知道,application.yml中可以配置spring.profiles.active来指定加载application-[profile].yml来实现多环境。这种是非常好的,因为例如在配置DataSource的时候,只需要声明一次Bean,其余的就是改不同环境的数据库链接参数。而这种功能也不是万能的,因为每种选项都可以选,因此它依赖于大量的默认配置。
javaweb中指定profile
使用springmvc时,我们习惯将所有的请求转发给一个Dispatcher,它也是一个Servlet。既然是一个Servlet加载的时候,就可以初始化参数,如
<!--web.xml-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</servlet>
这里就将spring.profiles.active设置为dev,当然spring也提供默认的profile设置,也就是spring.profiles.default,当设置了active,default就会失效。
xml配置文件中根据profile配置bean
1.借鉴于springboot,我们将不同环境的配置文件用文件夹区分,比如
图中,我们有两个环境的jdbc配置文件,分别放在dev和prod两个目录下,接着要做的就是加载配置文件。
2.在<beans>标签中添加profile属性,表明该标签体内声明的所有bean都在此profile下装配
<!--applicationContext-dev.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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/context
https://www.springframework.org/schema/context/spring-context.xsd"
profile="dev">
<context:property-placeholder location="classpath*:config/dev/*.properties"/>
<beans>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${password}"/>
</bean>
</beans>
</beans>
注意
<context:property-placeholder/>在spring的bean上下文中只能存在一份,因此务必在一处将所有的properties全部引入,将公有的properties单独抽取出来,在location属性中使用' , ‘分隔。
3.一般讲,可以使用类似于springboot的命名结构
注意,注意,注意
spring中的bean,比如datasource,必须在非任何profile的情况下有一份,不然会抛如下异常。
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1716)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1272)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1226)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:521)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:497)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:650)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:239)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:318)
... 60 more
所以,根据项目实际情况,将常用的环境中的bean配置为默认的,而不受限于任何profile,当有profile激活时,他将默认失效。
同时,如果想在javaweb中使用多个spring的配置文件,需要在监听器中,使用通配符表示配置文件
<!--web.xml-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果有更好的多环境的配置方案,请在评论区评论或者私信me 🙂