zoukankan      html  css  js  c++  java
  • 24、自动装配-@Profile环境搭建

    24、自动装配-@Profile环境搭建

    • Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能。
    • 开发环境、测试环境、正式环境 数据源切换

    24.1 添加 数据源和jdbc驱动 pom 依赖

    <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.13</version>
    </dependency>
    

    24.2 添加MainConfigOfProfile

    package com.hw.springannotation.config;
    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    import org.springframework.aop.target.CommonsPool2TargetSource;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.EmbeddedValueResolverAware;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    import org.springframework.util.StringValueResolver;
    
    import javax.annotation.Resource;
    import javax.sql.DataSource;
    import java.beans.PropertyVetoException;
    
    /**
     * @Description Profile
     * @Author hw
     * @Date 2018/11/29 19:25
     * @Version 1.0
     */
    @Component
    @PropertySource(value = {"classpath:/datasource.properties"})
    public class MainConfigOfProfile implements EmbeddedValueResolverAware {
    
        @Value("${db.username}")
        private String username;
    
        private String driveClassName;
    
        private StringValueResolver resolver;
    
        public void setEmbeddedValueResolver(StringValueResolver resolver) {
            this.resolver = resolver;
            this.driveClassName = this.resolver.resolveStringValue("${db.driveClassName}");
        }
    
        @Bean
        public DataSource dataSourceTest(@Value("${db.password}") String password) throws PropertyVetoException {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setUser(username);
            dataSource.setPassword(password);
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
            dataSource.setDriverClass(driveClassName);
            return dataSource;
        }
    
        @Bean
        public DataSource dataSourceDev(@Value("${db.password}") String password) throws PropertyVetoException {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setUser(username);
            dataSource.setPassword(password);
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mysql");
            dataSource.setDriverClass(driveClassName);
            return dataSource;
        }
    
        @Bean
        public DataSource dataSourceProd(@Value("${db.password}") String password) throws PropertyVetoException {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setUser(username);
            dataSource.setPassword(password);
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/qm_dmp");
            dataSource.setDriverClass(driveClassName);
            return dataSource;
        }
    }
    
    

    24.3 添加配置文件datasource.properties

    db.username=root
    db.password=root
    db.driveClassName=com.mysql.jdbc.Driver
    

    24.4 测试用例Test_ofProfile

    package com.hw.springannotation.test;
    
    import com.hw.springannotation.config.MainConfigOfProfile;
    import org.junit.jupiter.api.Test;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    /**
     * @Description TODO
     * @Author hw
     * @Date 2018/11/29 20:14
     * @Version 1.0
     */
    public class Test_ofProfile {
    
        @Test
        public void test() {
            AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfProfile.class);
            String[] definitionNames = applicationContext.getBeanDefinitionNames();
            for (String name : definitionNames) {
                System.out.println(name);
            }
    
        }
    }
    
    

  • 相关阅读:
    Delphi实战中讲解FormCreate,FormShow,FormActivate
    delphi Try except on e:Exception do
    Delphi处理数据网格DBGrid的编辑框 获取还没有提交到数据集的字段文本
    delphi dbgrid中如何自动生成序号
    DBDateTimePicker;
    Delphi控件开发浅入深出(八)
    delphi中日期类型TDateTime使用总结
    在DBGrid录数据时,如何判断光标位置是在数据的最左或最右,如果是最左或最右则在按左右光标键时光标跳到上一格或下一格,如果是在数据中
    请问如何按Enter键让DBGrid的光标向右移以及换行?(0分)
    tdbgrid中用enter仿真tab键盘_delphi教程
  • 原文地址:https://www.cnblogs.com/Grand-Jon/p/10041688.html
Copyright © 2011-2022 走看看