zoukankan      html  css  js  c++  java
  • Maven+Spring Profile实现生产环境和开发环境的切换

    第一步 Maven Profile配置

    <profiles>
        <profile>
            <id>postgres</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <property>
                    <name>spring.profiles.active</name>
                    <value>postgres</value>
                </property>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>postgresql</groupId>
                    <artifactId>postgresql</artifactId>
                    <version>9.1-901.jdbc4</version>
                </dependency>
            </dependencies>
        </profile>
        <profile>
            <id>h2</id>
            <activation>
                <property>
                    <name>spring.profiles.active</name>
                    <value>h2</value>
                </property>
            </activation>           
            <dependencies>
                <dependency>
                    <groupId>com.h2database</groupId>
                    <artifactId>h2</artifactId>
                    <version>1.4.191</version>
                </dependency>
            </dependencies>
        </profile>
    </profiles>
    

    第二步 web.xml配置  

    <context-param>
       <param-name>spring.profiles.default</param-name>
       <param-value>postgres</param-value>
    </context-param>
    

    第三步 Spring配置

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="mainDataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties" ref="hibProps"/>
        <property name="packagesToScan">
            <list>
                <value>my.test.model</value>
            </list>
        </property>
    </bean>
    ...
    <beans profile="postgres">
        <bean name="mainDataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="org.postgresql.Driver" />
            <property name="url" value="jdbc:postgresql://127.0.0.1:5432/webchat" />
            <property name="username" value="postgres" />
            <property name="password" value="postgres" />
        </bean>
    </beans>
    
    <beans profile="h2">
        <bean name="mainDataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="org.h2.Driver" />
            <property name="url" value="jdbc:h2:file:./newsdb;INIT=RUNSCRIPT FROM 'classpath:init.sql';TRACE_LEVEL_FILE=0" />
            <property name="username" value="sa" />
            <property name="password" value="" />
        </bean>
    </beans>
    

    如何运行

    运行Postgres数据库,命令

    #运行默认配置
    mvn jetty:run #或者
    mvn jetty:run -Dspring.profiles.active=postgres commands

    运行H2数据库,命令  

    jetty:run -Dspring.profiles.active=h2
    
  • 相关阅读:
    每日一水 POJ8道水题
    编译和使用 MySQL C++ Connector
    j2ee model1模型完成分页逻辑的实现 详解!
    DB查询分析器访问EXCEL时,要在表名前后加上中括弧或双引号
    指向结构体变量的指针
    EOSS V3.0 企业运营支撑系统(基于RBAC原理的权限管理)
    MybatisGen1.0 Mybatis JavaBean Mapper生成工具
    The table name must be enclosed in double quotation marks or sqare bracket while accessing EXCEL by
    资源-Android:Android
    软件-开发软件:Android Studio
  • 原文地址:https://www.cnblogs.com/schbook/p/6149998.html
Copyright © 2011-2022 走看看