以数据库为例:

引入 c3p0数据源maven坐标

数据库驱动

@Configuration
@PropertySource("classpath:/db.config.properties")
public class ProfileConfig implements EmbeddedValueResolverAware {
//方法一
@Value("${db.user}")
private String user; //方法三 通过值解析器
private StringValueResolver valueResolver; private String driverClass; @Profile("test")
@Bean(name="testDataSource") //方法二
public ComboPooledDataSource dataSourceTest(@Value("${db.password}") String pwd) throws Exception{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser("root");
dataSource.setPassword(pwd);
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
dataSource.setDriverClass(driverClass);
return dataSource;
}
@Profile("dev")
@Bean(name="devDataSource") public ComboPooledDataSource dataSourceDev() throws Exception{ ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setUser(user); dataSource.setPassword("root"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/dev"); dataSource.setDriverClass(driverClass); return dataSource; } //重写方法 public void setEmbeddedValueResolver(StringValueResolver resolver) { this.valueResolver = resolver; driverClass = valueResolver.resolveStringValue("${db.driverClass}"); } }

这样就可以将数据源注册到容器中了

如何激活呢? 通过@Profile !

指定组件在哪个环境的情况下 才能被注册到容器中,不指定任何环境下都能注册。

只有当激活的bean 才能被注册进来

加了环境表示@Profile 的bean  只有当环境被激活的 才可以注册到容器  默认是default

激活方式:

使用参数方式激活

使用idea的界面激活

通过代码的方式激活:

public class test {
@Test
public void test01(){
//使用无参构造器 创建applicationContext (详情自己研究下有参构造器的方法)
AnnotationConfigApplicationContext applicationContext =new AnnotationConfigApplicationContext();
//设置需要激活的环境
applicationContext.getEnvironment().setActiveProfiles("dev","test"); //可以设置多个
//注册主配置类
applicationContext.register(Profile.class);
//启动刷新容器
applicationContext.refresh();
}
}

@Profile 还可以标注在类上: 写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能生效

没有标注@Profile的ben, 在任何环境下都是加载的

IOC小结:

Spring注解(环境)的更多相关文章

  1. 最新版ssh hibernate spring struts2环境搭建

    最新版ssh hibernate spring struts2环境搭建 最新版spring Framework下载地址:spring4.0.0RELEASE环境搭建 http://repo.sprin ...

  2. spring 注解的优点缺点

    注解与XML配置的区别 注解:是一种分散式的元数据,与源代码耦合. xml :是一种集中式的元数据,与源代码解耦. 因此注解和XML的选择上可以从两个角度来看:分散还是集中,源代码耦合/解耦. 注解的 ...

  3. Spring MVC 环境搭建(二)

    在Spring MVC 环境搭建(一)中我们知道 spring 的配置是通过 urlmapping 映射到控制器,然后通过实现Controller接口的handlerequest方法转向页面. 但这存 ...

  4. Annotation(三)——Spring注解开发

    Spring框架的核心功能IoC(Inversion of Control),也就是通过Spring容器进行对象的管理,以及对象之间组合关系的映射.通常情况下我们会在xml配置文件中进行action, ...

  5. Spring注解与Spring与Struts2整合

    @Component @Controller @Service @Repository 四大注解作用基本一样,只是表象在不同层面 @Resource @Scope Struts2与Spring整合:1 ...

  6. Spring3+SpingMVC+Hibernate4全注解环境配置

    Spring3+SpingMVC+Hibernate4全注解环境配置 我没有使用maven,直接使用Eclipse创建动态Web项目,jar包复制在了lib下.这样做导致我马上概述的项目既依赖Ecli ...

  7. Maven搭建struts2+spring+hibernate环境

    Maven搭建struts2+spring+hibernate环境(一) 本文简单的使用STS的自带的maven插件工具搭建ssh(struts2+spring+hibernate)开发环境,图文并茂 ...

  8. Spring 注解驱动(一)基本使用规则

    Spring 注解驱动(一)基本使用规则 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.基本使用 @Configur ...

  9. Spring 注解方式引入配置文件

    配置文件,我以两种为例,一种是引入Spring的XML文件,另外一种是.properties的键值对文件: 一.引入Spring XML的注解是@ImportResource @Retention(R ...

  10. [教程] Spring+Mybatis环境配置多数据源

    一.简要概述 在做项目的时候遇到需要从两个数据源获取数据,项目使用的Spring + Mybatis环境,看到网上有一些关于多数据源的配置,自己也整理学习一下,然后自动切换实现从不同的数据源获取数据功 ...

随机推荐

  1. Python 基础之列表去重的几种玩法

    列表去重 1.方法1 借助一个临时列表 ids = [1,2,3,3,4,2,3,4,5,6,1] news_ids = [] for id in ids: if id not in news_ids ...

  2. 延迟任务和循环任务ScheduledExecutorService

    public class ScheduledThreadPool { public static ScheduledExecutorService scheduledThreadPool = Exec ...

  3. getComputedStyle获取css属性与IE下的currentStyle获取到的值不同

    <!doctype html><html lang="en"> <head>  <meta charset="UTF-8&quo ...

  4. CodeForces 156B Suspects(枚举)

    B. Suspects time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  5. JavaWeb中servlet读取配置文件的方式

    我们在JavaWeb中常常要涉及到一些文件的操作,比如读取配置文件,下载图片等等操作.那我们能不能采用我们以前在Java工程中读取文件的方式呢?废话不多说我们来看看下我们以前在Java工程中读取文件是 ...

  6. IO流入门-第四章-FileReader

    FileReader基本用法和方法示例 /* java.io.Reader java.io.InputStreamReader 转换流(字节输入流---->字符输入流) java.io.File ...

  7. php中get_cfg_var()和ini_get()的用法及区别

    php里get_cfg_var()和ini_get()都是取得配置值的函数,当你需要获取php.ini里的某个选项的配置值时,这两个函数都都可以使用,得到的结果是一样的. 不过,get_cfg_var ...

  8. SpringMVC是单例的,高并发情况下,如何保证性能的?

    首先在大家的思考中,肯定有影响的,你想想,单例顾名思义:一个个排队过...  高访问量的时候,你能想象服务器的压力了... 而且用户体验也不怎么好,等待太久~ 实质上这种理解是错误的,Java里有个A ...

  9. Python高级教程-filter

    Python中的filter() Python内建的filter()函数用于过滤序列.和map()类似,filter()也接收一个函数和一个序列.和map()不同的是,filter()把传入的函数依次 ...

  10. ubuntu服务器无法运行chromedriver解决方法(转)

    无头浏览器 sudo apt-get install Xvfb sudo pip install pyvirtualdisplay from pyvirtualdisplay import Displ ...