zoukankan      html  css  js  c++  java
  • SPRING IN ACTION 第4版笔记-第三章Advancing wiring-001-DataSource在应用和开发环境之间切换 profile

    一、

    DataSource在应用和开发环境的产生方式不同,可以用srping 的profile管理

    Spring’s solution for environment-specific beans isn’t much different from build-time
    solutions. Certainly, an environment-specific decision is made as to which beans will
    and won’t be created. But rather than make that decision at build time, Spring waits to
    make the decision at runtime. Consequently, the same deployment unit (perhaps a
    WAR file) will work in all environments without being rebuilt.
    In version 3.1, Spring introduced bean profiles. To use profiles, you must gather all

    二、

    1.java

     1 package com.myapp;
     2 
     3 import javax.sql.DataSource;
     4 
     5 import org.springframework.context.annotation.Bean;
     6 import org.springframework.context.annotation.Configuration;
     7 import org.springframework.context.annotation.Profile;
     8 import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
     9 import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
    10 import org.springframework.jndi.JndiObjectFactoryBean;
    11 
    12 @Configuration
    13 public class DataSourceConfig {
    14   
    15   @Bean(destroyMethod = "shutdown")
    16   @Profile("dev")
    17   public DataSource embeddedDataSource() {
    18     return new EmbeddedDatabaseBuilder()
    19         .setType(EmbeddedDatabaseType.H2)
    20         .addScript("classpath:schema.sql")
    21         .addScript("classpath:test-data.sql")
    22         .build();
    23   }
    24 
    25   @Bean
    26   @Profile("prod")
    27   public DataSource jndiDataSource() {
    28     JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
    29     jndiObjectFactoryBean.setJndiName("jdbc/myDS");
    30     jndiObjectFactoryBean.setResourceRef(true);
    31     jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class);
    32     return (DataSource) jndiObjectFactoryBean.getObject();
    33   }
    34 
    35 }

    2.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
     4   xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p"
     5   xsi:schemaLocation="
     6     http://www.springframework.org/schema/jee
     7     http://www.springframework.org/schema/jee/spring-jee.xsd
     8     http://www.springframework.org/schema/jdbc
     9     http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    10     http://www.springframework.org/schema/beans
    11     http://www.springframework.org/schema/beans/spring-beans.xsd">
    12 
    13   <beans profile="dev">
    14     <jdbc:embedded-database id="dataSource" type="H2">
    15       <jdbc:script location="classpath:schema.sql" />
    16       <jdbc:script location="classpath:test-data.sql" />
    17     </jdbc:embedded-database>
    18   </beans>
    19   
    20   <beans profile="prod">
    21     <jee:jndi-lookup id="dataSource"
    22       lazy-init="true"
    23       jndi-name="jdbc/myDatabase"
    24       resource-ref="true"
    25       proxy-interface="javax.sql.DataSource" />
    26   </beans>
    27 </beans>
  • 相关阅读:
    POJ
    UPC-5843: 摘樱桃(最优状态递推)
    BZOJ-1088 [SCOI2005]扫雷Mine(递推+思维)
    HDU-3065 病毒侵袭持续中(AC自动机)
    BZOJ-4236 JOIOJI (map查找+思维)
    HDU-2896 病毒侵袭(AC自动机)
    Hrbust-2060 截取方案数(KMP)
    UVALive 4490 压缩DP
    UVALive 5881
    UVALive 4168
  • 原文地址:https://www.cnblogs.com/shamgod/p/5235195.html
Copyright © 2011-2022 走看看