iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >@Profile注解详解
  • 778
分享到

@Profile注解详解

java教程注解 2018-01-19 08:01:10 778人浏览 猪猪侠
摘要

@Profile:spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能;开发环境develop、测试环境test、生产环境master数据源:(/dev) (/test) (/master)@Profile:指定组件在

@Profile:spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能;

开发环境develop、测试环境test、生产环境master

数据源:(/dev) (/test) (/master)

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

1) 加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境

2) 写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效

package com.spring.config;
 
import java.beans.PropertyVetoException;
 
import javax.sql.DataSource;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringValueResolver;
 
import com.mchange.v2.c3p0.ComboPooledDataSource;
 

@PropertySource("classpath:/dbconfig.properties")
@Configuration
public class MainConfiGofProfile implements EmbeddedValueResolverAware{
	
	@Value("${db.user}")
	private String user;
	
	private String driverClass;
	
	@Profile("default")
	@Bean("test")
	public DataSource testDataSource(@Value("${db.passWord}")String password) throws PropertyVetoException {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setUser(user);
		dataSource.setPassword(password);
		dataSource.setDriverClass(driverClass);
		return dataSource;
	}
	
	@Profile("dev")
	@Bean("dev")
	public DataSource devDataSource(@Value("${db.password}")String password) throws PropertyVetoException {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setUser(user);
		dataSource.setPassword(password);
		dataSource.setDriverClass(driverClass);
		return dataSource;
	}
	
	@Profile("master")
	@Bean("master")
	public DataSource masterDataSource(@Value("${db.password}")String password) throws PropertyVetoException {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setUser(user);
		dataSource.setPassword(password);
		dataSource.setDriverClass(driverClass);
		return dataSource;
	}
 
	public void setEmbeddedValueResolver(StringValueResolver resolver) {
		String driverClass = resolver.resolveStringValue("${db.driverClass}");
		this.driverClass = driverClass;
	}
 
}
package com.spring.test;
 
import java.util.Arrays;
 
import javax.sql.DataSource;
 
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
import com.spring.config.MainConfigOfProfile;
 
 
public class iocTestProfile {
	//1. 使用命令行动态参数:在虚拟机参数位置加载 -Dspring.profiles.active=test
	//2. 使用代码的方式激活某种环境;
	@Test
	public void test01() {
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfProfile.class);
		//1. 创建一个applicationContext
		//2. 设置需要激活的环境
		applicationContext.getEnvironment().setActiveProfiles("dev","master");
		//3. 注册主配置类
		applicationContext.reGISter(MainConfigOfProfile.class);
		//4. 启动刷新容器
		applicationContext.refresh();
		
		String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
		System.out.println(Arrays.toString(beanNamesForType));
		
		applicationContext.close();
	}
 
 
        @Test
	public void test02() {
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfProfile.class);
		
		String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
		System.out.println(Arrays.toString(beanNamesForType));
		
		applicationContext.close();
	}
}

--结束END--

本文标题: @Profile注解详解

本文链接: https://www.lsjlt.com/news/2988.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 详解Spring @Profile注解的使用和源码解析
    目录介绍使用通过Environment设置profile通过JVM参数设置SpringBoot通过yml进行配置源码解析BeanDefinition注册shouldSkip源码Pro...
    99+
    2023-05-15
    Spring @Profile注解使用 Spring @Profile注解 Spring @Profile
  • Spring @Profile注解如何使用
    这篇文章主要介绍“Spring @Profile注解如何使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Spring @Profile注解如何使用”文章能帮助大家解决问题。使用...
    99+
    2023-07-06
  • Maven管理SpringBoot Profile详解
    1. Spring ProfileSpring可使用Profile绝对程序在不同环境下执行情况,包含配置、加载Bean、依赖等。 Spring的Profile一般项目包含:dev(开发), test(单元测试), qa(集成测试), pro...
    99+
    2023-05-30
    maven spring profile
  • Spring Boot的Profile配置详解
    Profile 是Spring Boot用来针对不同的环境对不同的配置提供的支持,全局Profile配置使用application-{profile}.properties,如: application-dev.properties 可以表...
    99+
    2023-05-31
    spring boot profile
  • spring注解之@profile的示例分析
    这篇文章给大家分享的是有关spring注解之@profile的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。spring中@profile与maven中的profile很相似,通过配置来改变参数。例如在开...
    99+
    2023-05-31
    spring profile
  • @profile注解如何在spring中使用
    本篇文章给大家分享的是有关@profile注解如何在spring中使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。首先是新建maven工程mvn archetype:gene...
    99+
    2023-05-30
    spring profile
  • Spring Boot常用功能Profile详解
    入口 相关逻辑的入口是listener类:ConfigFileApplicationListener,当容器广播器触发ApplicationEnvironmentPreparedEv...
    99+
    2024-04-02
  • SpringBoot中获取profile的方法详解
    目录spring boot与profile静态获取方式autowire ProfileConfigspring boot与profile spring boot 的项目中不再使用xm...
    99+
    2024-04-02
  • @TableField注解详解
    @TableField(value = "email")//指定数据库表中字段名 如果数据库和实体类的字段名不一致,可以使用@TableField注解指定数据库表中字段名。  2、@TableField(exist = "false")/...
    99+
    2023-09-02
    数据库 sql java
  • @PreAuthorize注解详解
    @PreAuthorize注解会在方法执行前进行权限验证,支持Spring EL表达式,它是基于方法注解的权限解决方案。只有当@EnableGlobalMethodSecurity(prePostEnabled=true)的时候,@PreA...
    99+
    2023-09-14
    spring java mybatis
  • Spring注解详解
    概述 注释配置相对于 XML 配置具有很多的优势:它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作。如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO 的属性名、类型等信息,如果关系表字段和...
    99+
    2023-06-03
  • Java注解详解
    目录 一、发现注解二、注解是什么1. 注解的本质2. 注解是针对Java编译器的说明 三、为什么要使用注解四、Java中常用的注解4.1 基础注解(spring-context、spring-we...
    99+
    2023-08-22
    java spring 软件测试
  • 详解@Override注解
    目录 1.是什么 2.为什么用 3.举例说明 1)示例一 2)示例二 3)示例三 1.是什么 @Override注解是伪代码,用于表示被标注的方法是一个重写方法。 @Override注解,只能用于标记方法,并且它只在编译期生效,不会保留...
    99+
    2023-09-24
    java
  • 详解Spring注解@Configuration
    目录@Configuration 注解的概述底层原理与 Spring IoC 容器的集成Bean 的定义和装配的实现条件化配置的实现配置类的加载和实例化过程总结Spring 提供了丰...
    99+
    2023-05-16
    Spring注解@Configuration介绍 Spring注解@Configuration Spring注解
  • SpringMVC-@RequestMapping注解详解
    目录1、@RequestMapping注解的作用2、@RequestMapping注解的位置3、value属性(1)基础用法(2)路径中的占位符(重点)4、method属性5、par...
    99+
    2023-05-17
    SpringMVC @RequestMapping SpringMVC @RequestMapping注解属性
  • SpringMVC @RequestMapping注解详解
    目录一、@RequestMapping1.@RequestMapping注解的功能2.@RequestMapping注解的位置二、@RequestMapping注解的属性1.valu...
    99+
    2024-04-02
  • Java注解Annotaton详解
    目录1、三种基本的Annotaton@Override解读细节@Deprecated解读效果细节可以修饰方法,类,包,参数等等@SuppressWarnings解读效果细节元注解Re...
    99+
    2024-04-02
  • @DateTimeFormat 和 @JsonFormat 注解详解
    这一篇文章足以让你对Java当中Date时间上的理解更上一层楼,本篇文章主要通过代码的形式来进行试验,彻彻底底搞明白日期传参,以及日期返回参数的格式相关问题,每一个步骤都会记得特别详细! 本篇文...
    99+
    2023-10-08
    mybatis java spring boot 日期
  • 详解 SpringMVC 的 @RequestMapping 注解
    文章目录 1、@RequestMapping注解的功能2、@RequestMapping注解的位置3、@RequestMapping注解的value属性4、@RequestMapping注解的...
    99+
    2023-09-07
    spring springMVC RequestMapping
  • SpringBoot各种注解详解
    目录一、注解列表二、注解详解一、注解列表 @SpringBootApplication:包含了@ComponentScan、@Configuration和@EnableAutoCon...
    99+
    2022-12-27
    SpringBoot注解 SpringBoot注解的作用
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作