【Spring 核心】高级装配

本文介绍了Spring高级装配中的环境与Profile配置,详细解析了如何在开发和生产环境中进行软切换。重点讨论了spring.profiles.active和spring.profiles.default属性的作用,以及通过不同方式(如web.xml、初始化参数、JNDI、环境变量等)设置激活的Profile。
摘要由CSDN通过智能技术生成

高级装配用来适应开发和生产 不同环境下的软切换

一、环境与profile

1.开发环境下的profile

package com.bonc.config;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

/*
 * 一、注解开发profile
 * @Profile作用在了类上,告诉Spring这个配置类中的bean只有在dev profile激活时才创建
 * dev profile没有被激活的话,@Bean的注解都会被忽略掉。
 * 
 * Spring3.1中,只能在类级别上使用@Profile注解,在Spring3.2开始,
 * 你也可以在方法级别上使用@Profile注解,与@Bean注解一同使用,这样的话就能将两个bean放置到同一个配置类中。
 * 
 * 尽管只有当规定的profile被激活时,相应的bean才会被创建,但是可能会有其他的bean并没有声明在一个给定的profile中。
 * 没有指定profile的bean始终都会被创建,与激活哪个profile没有关系。
 * 
 * 
 * */

@Configuration
@Profile("dev")
public class DevelopmentProfileConfig {

	@Bean(destroyMethod="shutdown")
	public DataSource dataSource(){
		return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
				.addScript("classpath:schema.sql")
				.addScript("classpath:test.sql").build();
	}
}
二、生产环境下profile

package com.bonc.config;

import javax.sql.DataSource;

import o
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值