24、自动装配[email protected]环境搭建

24、自动装配[email protected]环境搭建

  • 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);
        }

    }
}

原文地址:https://www.cnblogs.com/Grand-Jon/p/10041688.html

时间: 2024-08-10 12:59:22

24、自动装配[email protected]环境搭建的相关文章

25、自动装配[email&#160;protected]根据环境注册bean

25.自动装配[email protected]根据环境注册bean 指定组件在哪个环境的情况下才能被注册到容器中 加了环境标识的,只有这个环境被激活才能注册到组件中 默认是default环境 写在类上,整个配置类的激活的时候才能生效 没有标注环境标识的bean,在任何环境下都是加载的 package org.springframework.context.annotation; import java.lang.annotation.Documented; import java.lang.a

21、自动装配[email&#160;protected]&amp;@Inject

21.自动装配[email protected]&@Inject Spring 还支持使用@Resource(JSR250)和@Inject(JSR330)[Java规范的注解] AutowiredAnnotationBeanPostProcessor 完成解析自动装配功能 21.1 @Resource 可以和@Autowired一样实现自动注入功能,默认是按照组件名称进行装配的. 没有能支持@Primary功能,没有支持@Autowired(required = false) 21.2 @In

HTML5移动开发之路(24)—— PhoneGap Android开发环境搭建

本文为 兄弟连IT教育 机构官方 HTML5培训 教程,主要介绍:HTML5移动开发之路(24)-- PhoneGap Android开发环境搭建 有关JDK及Android开发环境的搭建请看我前面的博文:http://blog.csdn.net/dawanganban/article/details/9748497 一.下载PhoneGap 下载地址:http://phonegap.com/install/  我下载的是最新的PhoneGap 2.9.1 将下载好的PhoneGap解压缩,可以

从头认识Spring-2.3 注解装配[email&#160;protected](3)-通过构造器方法注入

这一章节我们来讨论一下注解装配的@autowired是怎样通过set方法或者其他方法注入? 1.domain 蛋糕类:(不变) package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_8; public class Cake { private String name = ""; public String getName() { return name; } public void setName(String name

从头认识Spring-2.4 基于java的标准注解装配[email&#160;protected](2)-通过set方法或者其它方法注入

这一章节我们来讨论一下基于java的标准注解装配标签@Inject是如何通过通过set方法或者其它方法注入? 在使用@Inject标签之前.我们须要在pom文件中面增加以下的代码: <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency>

从头认识Spring-2.4 基于java的标准注解装配[email&#160;protected]限定器@Named

这一章节我们来讨论一下基于java的标准注解装配标签@Inject的限定器@Named. 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_16; import javax.inject.Named; @Named("myCake") public class Cake { private String name = ""; public String getName(

从头认识Spring-2.4 基于java的标准注解装配[email&#160;protected](3)-通过构造器方法注入

这一章节我们来讨论一下基于java的标准注解装配标签@Inject是怎样通过通过构造器方法注入? 在使用@Inject标签之前,我们需要在pom文件里面加入下面的代码: <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency> 上面是j

从头认识Spring-2.3 注解装配[email&#160;protected](4)-required(1)

这一章节我们来详细讨论一下@autowired里面的参数required. 1.domain(重点) 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_9; public class Cake { private String name = ""; public String getName() { return name; } public void setName(String name) { this

从头认识Spring-2.3 注解装配[email&#160;protected](5)-限定器@Qualifier(1)

这一章节我们来具体讨论一下配合@autowired一起使用的限定器@Qualifier. 1.domain(重点) 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_11; public class Cake { private String name = ""; public String getName() { return name; } public void setName(String name