Spring注解@profile

2019-10-25  本文已影响0人  yuhan_sining

Spring中@profile与maven中的profile很相似,通过配置来改变参数。

例如在开发环境与生产环境使用不同的参数,可以配置两套配置文件,通过@profile来激活需要的环境,但维护两套配置文件不如maven中维护一套配置文件,在pom中通过profile来修改配置文件的参数来的实惠。

比如在开发中需要mock数据,所以可以写一个mock参数的接口调用类,在开发环境中就使用这个类,测试环境与生产环境则使用正常的接口调用类,这样就不用每次开发的时候去手动改一些代码。

注:@profile在3.2以后的版本支持方法级别和类级别,3.1版本只支持类级别。

一、注解配置

/** 配置生产环境调用类  **/
@service("productRpc")
@profile("prop")
public class ProductRpcImpl implements ProductRpc
    public String productBaseInfo(Long sku){
        return productResource.queryBaseInfo(Long sku);
    }
}
 
 
/** 配置开发环境调用类  **/
@service("productRpc")
@profile("dev")
public class MockProductRpcImpl implements ProductRpc
    public String productBaseInfo(Long sku){
        return “iphone11”;
    }
}
 
/** 调用类  **/
public class Demo(){
    @Resource(name="productRpc")
    private ProductRpc productRpc;
 
    public void demo(){
        String skuInfo = productRpc.productBaseInfo(123123L);
        logger.info(skuInfo);
    }
}

这样就完成了基于注解的profile配置。当配置为生产环境的时候会正常调用接口,当为开发环境的时候回调用mock接口。

二、XML配置

<!-- 开发环境 -->
<beans profile="dev">
    <bean id="beanname" class="com.pz.demo.ProductRPC"/>
</beans>
 
<!-- 生产环境 -->
<beans profile="dev">
    <bean id="beanname" class="com.pz.demo.MockProductRPC"/>
</beans>

三、激活profile

注:spring在确定那个profile处于激活状态的时,需要依赖两个独立的属性:spring.profiles.active和spring.profile.default。如果设置了spring.profiles.actives属性,那么它的值就会用来确定那个profile是激活的。如果没有设置spring.profiles.active属性的话,那spring将会查找spring.profiles.default的值。

1.在servlet上下文中进行配置(web.xml)
<context-param>
    <param-name>spring.profiles.default</param-name>
    <param-value>dev</param-value>
</context-param>
2.作为DispatcherServlet的初始化参数
<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-servlet.xml</param-value>
    </init-param>
    <init-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>dev</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
3.spring-junit使用@ActiveProfiles进行激活
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/spring-config.xml")
@ActiveProfiles("dev")
public class MainTest {
    ...
}
4.作为JNDI条目;

在程序中,也可以通过 @Profile("...") 对某些资源进行注解,这样只有当选择对应的环境时,才会产生对应的bean,如:

@Bean
@Profile("production")    
    public DataSource jndiDataSource(){
        JndiObjectFactoryBean jofb=new JndiObjectFactoryBean();
        jofb.setJndiName("jndi/iDS");
        jofb.setResourceRef(true);
        jofb.setProxyInterface(xxx.class);
        return (DataSource) jofb.getObject();
    }
}
5.作为环境变量;
6.作为JVM的系统属性;

激活指定的环境,也可以通过JVM参数来设置,可以在tomcat的启动脚本中加入以下JVM参数来激活:

-Dspring.profiles.active="production"

举例:

首先可以在resources文件夹下分别为每个环境建立单独的文件夹(也可以额外建立一个common文件夹,用于存放公共的参数配置文件),每个文件夹下面存放对应的环境所需的配置文件,就像这样子:



在resources文件夹下建立applicationContext-profile.xml文件,用来定义不同的profile:

<?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" />
    </beans>

    <!-- 测试环境配置文件 -->
    <beans profile="test">
        <context:property-placeholder
            location="classpath*:common/*.properties, classpath*:test/*.properties" />
    </beans>
    
    <!-- 生产环境配置文件 -->
    <beans profile="production">
        <context:property-placeholder
            location="classpath*:common/*.properties, classpath*:production/*.properties" />
    </beans>

</beans>

这样就实现了通过profile标记不同的环境,接下来就可以通过设置spring.profiles.default和spring.profiles.active这两个属性来激活和使用对应的配置文件。default为默认,如果没有通过active来指定,那么就默认使用default定义的环境。

在web.xml文件中设置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <display-name>Archetype Created Web Application</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:/applicationContext*.xml
        </param-value>
    </context-param>

    <!-- 在上下文context-param中设置profile.default的默认值 -->
    <context-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>development</param-value>
    </context-param>

    <!-- 在上下文context-param中设置profile.active的默认值 -->
    <!-- 设置active后default失效,web启动时会加载对应的环境信息 -->
    <context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>development</param-value>
    </context-param>

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 在DispatcherServlet参数中设置profile的默认值,active同理 -->
        <init-param>
            <param-name>spring.profiles.default</param-name>
            <param-value>development</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
上一篇下一篇

猜你喜欢

热点阅读