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

 



    com.mchange
    c3p0
    0.9.5.2




    mysql
    mysql-connector-java
    5.1.44
package com.lei.study_09_18.config;

import com.lei.study_09_17.bean.Boss;
import com.lei.study_09_17.bean.Car;
import com.lei.study_09_17.dao.BookDao;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.EmbeddedValueResolver;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.*;
import org.springframework.util.StringValueResolver;

/**
 *
 *
 * @author LeiLei
 * @date 2019/9/16
 */
@Configuration
@ComponentScan({"com.lei.study_09_18.bean"})
@PropertySource(value = "classpath:/db.properties",encoding = "utf-8")
public class BeanConfig implements EmbeddedValueResolverAware {
    
    @Value("${db.user}")
    private String user;

    private String url;

    private StringValueResolver resolver;

    @Profile("test")
    @Bean
    public ComboPooledDataSource dataSourceTest(@Value("${db.password}")String pwd) throws Exception {
         ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setUser(user);
            dataSource.setPassword(pwd);

            dataSource.setJdbcUrl(url);
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            return dataSource;
    }

    @Profile("dev")
    @Bean
    public ComboPooledDataSource dataSourceDev(@Value("${db.password}")String pwd) throws Exception {
         ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setUser(user);
            dataSource.setPassword(pwd);
            dataSource.setJdbcUrl(url);
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            return dataSource;
    }

    @Profile("prod")
    @Bean
    public ComboPooledDataSource dataSourceProd(@Value("${db.password}")String pwd) throws Exception {
         ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setUser(user);
            dataSource.setPassword(pwd);
            dataSource.setJdbcUrl(url);
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            return dataSource;
    }

    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        this.resolver = resolver;
        url = resolver.resolveStringValue("${db.url}");
    }
}

 

@Test
public void testProfile2() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

    context.getEnvironment().setActiveProfiles("test","prod");
    context.register(com.lei.study_09_18.config.BeanConfig.class);
    context.refresh();

    String[] db = context.getBeanNamesForType(ComboPooledDataSource.class);
    for (String s : db) {
        System.out.println(s);

    }
}

@Test
public void testProfile() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(com.lei.study_09_18.config.BeanConfig.class);
    String[] db = context.getBeanNamesForType(ComboPooledDataSource.class);
    for (String s : db) {
        System.out.println(s);

    }
}

你可能感兴趣的:(spring,spring)