Spring注解(环境)
以数据库为例:
引入 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注解(环境)的更多相关文章
- 最新版ssh hibernate spring struts2环境搭建
最新版ssh hibernate spring struts2环境搭建 最新版spring Framework下载地址:spring4.0.0RELEASE环境搭建 http://repo.sprin ...
- spring 注解的优点缺点
注解与XML配置的区别 注解:是一种分散式的元数据,与源代码耦合. xml :是一种集中式的元数据,与源代码解耦. 因此注解和XML的选择上可以从两个角度来看:分散还是集中,源代码耦合/解耦. 注解的 ...
- Spring MVC 环境搭建(二)
在Spring MVC 环境搭建(一)中我们知道 spring 的配置是通过 urlmapping 映射到控制器,然后通过实现Controller接口的handlerequest方法转向页面. 但这存 ...
- Annotation(三)——Spring注解开发
Spring框架的核心功能IoC(Inversion of Control),也就是通过Spring容器进行对象的管理,以及对象之间组合关系的映射.通常情况下我们会在xml配置文件中进行action, ...
- Spring注解与Spring与Struts2整合
@Component @Controller @Service @Repository 四大注解作用基本一样,只是表象在不同层面 @Resource @Scope Struts2与Spring整合:1 ...
- Spring3+SpingMVC+Hibernate4全注解环境配置
Spring3+SpingMVC+Hibernate4全注解环境配置 我没有使用maven,直接使用Eclipse创建动态Web项目,jar包复制在了lib下.这样做导致我马上概述的项目既依赖Ecli ...
- Maven搭建struts2+spring+hibernate环境
Maven搭建struts2+spring+hibernate环境(一) 本文简单的使用STS的自带的maven插件工具搭建ssh(struts2+spring+hibernate)开发环境,图文并茂 ...
- Spring 注解驱动(一)基本使用规则
Spring 注解驱动(一)基本使用规则 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.基本使用 @Configur ...
- Spring 注解方式引入配置文件
配置文件,我以两种为例,一种是引入Spring的XML文件,另外一种是.properties的键值对文件: 一.引入Spring XML的注解是@ImportResource @Retention(R ...
- [教程] Spring+Mybatis环境配置多数据源
一.简要概述 在做项目的时候遇到需要从两个数据源获取数据,项目使用的Spring + Mybatis环境,看到网上有一些关于多数据源的配置,自己也整理学习一下,然后自动切换实现从不同的数据源获取数据功 ...
随机推荐
- Android无线测试之—KEYCODE键值对应大全
KEYCODE列表电话键键名 描述 键值 KEYCODE_CALL 拨号键5 KEYCODE_ENDCALL 挂机键6 KEYCODE_HOME 按键Home3 KEYCODE_ME ...
- @RequestParam 注解
@RequestParam 注解org.springframework.web.bind.annotation.RequestParam注解类型用于将指定的请求参数赋给方法中的形参.使用@Reques ...
- javascript 上传文件相关 (5) Blob 对象
这一系列文章都讲述的是关于使用 JavaScript 操作文件相关的知识,其中最重要的是 File 对象,而实际上 file 对象只是 blob 对象的一个更具体的版本,blob 存储着大量的二进制数 ...
- iOS UITextView 展示html
NSString *htmlString = @"<h1>Header</h1><h2>Subheader</h2><p>Some ...
- 160726、jQuery常用操作
一.简介 定义 jQuery创始人是美国John Resig,是优秀的Javascript框架: jQuery是一个轻量级.快速简洁的javaScript库. jQuery对象 jQuery产 ...
- Understanding Tensorflow using Go
原文: https://pgaleone.eu/tensorflow/go/2017/05/29/understanding-tensorflow-using-go/ Tensorflow is no ...
- delphi -----(去掉窗口最大化,最小化、关闭),主窗口,和子窗口之间的设置
一.去掉窗口最大化,最小化.关闭 borderIcons:biSystemMenu:false borderStyle:bsSizeable 二.主子窗口 主main: //调用子窗体procedur ...
- word2007的配置进度怎么产生的?如何解决?
那么要怎么解决这个问题呢?既然是安装的,那么我们便道安装控制器文件夹下面去找原因.在WIN8操作系统下,文件夹位于:C:\Program Files (x86)\Common Files\Micros ...
- 翻页bug 在接口文档中应规范参数的取值区间
<?php$a=array("red","green","blue","yellow","brown&q ...
- Java 面向对象之 static 关键字
static 特点 static 是一个修饰符, 用于修饰成员 static 修饰的成员被所有的对象所共享 static 优先于对象存在, 因为 static 的成员随着类的加载就已经存在了 stat ...