[Spring 학습 노트] profile 기반 고급 조립

3350 단어 스프링 베이스
일상적인 업무 에서 때때로 우 리 는 몇 가지 문제 에 직면 하 게 된다. 코드 를 서로 다른 환경 전에 왔다갔다 전환 해 야 한다. 예 를 들 어 개발 환경 에서 삽입 식 데이터베이스 인 Hypersonic 을 사용 할 수 있다. 이것 은 개발 환경 에 더 이상 적합 하지 않 지만 그 를 생산 에 두 면 안 된다.이 럴 때 스프링 이 제공 하 는 프로필 이 역할 을 발휘 할 수 있다.
다음은 나의 작은 demo:
package com.example.readingli.db;



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;
import org.springframework.jndi.JndiObjectFactoryBean;
/**
 * 
 * @author liugd
 * @company SI-TECH
 *
 */
@Configuration
//@Profile("production")  Spring3.2         
public class Dbcfg {
	@Profile("production")
	@Bean(destroyMethod="shutdown")
	public DataSource dataSource(){
		return new EmbeddedDatabaseBuilder()
		.setType(EmbeddedDatabaseType.H2)
		.addScript("classpath:schema.sql")
		.addScript("classpath:test-data.sql").build();
	}
	@Profile("dev")
	@Bean
	public DataSource productionDataSource(){
		JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean(); 
		jndiObjectFactoryBean.setJndiName("jdbc/myDS");
		jndiObjectFactoryBean.setResourceRef(true);
		jndiObjectFactoryBean.setProxyInterface(DataSource.class);
		return (DataSource)jndiObjectFactoryBean;
	}
}
bean 에 @ Profile 주 해 를 추가 하여 bean 을 활성화 하 는 데 사용 합 니 다.
활성화 방법:
Dispatcher Servlet 의 초기 화 매개 변수 로 서;웹 응용 컨 텍스트 매개 변수 로 서;JNDI 항목 으로 하기;환경 변수 로 서;JVM 으로서 의 시스템 속성;통합 테스트 클래스 에서 @ ActiveProfiles 주석 설정 을 사용 합 니 다.
JNDI 항목 으로 하기;환경 변수 로 서;JVM 으로서 의 시스템 속성;통합 테스트 클래스 에서 @ ActiveProfiles 주석 설정 을 사용 합 니 다.
저 는 개인 적 으로 웹. xml 에서 설정 하 는 것 을 좋아 합 니 다. 즉, Dispatcher Servlet 의 매개 변수 입 니 다.



	  demo

	
	
		contextConfigLocation
		classpath:/server-corecfg/applicationContext.xml,classpath:/server-corecfg/s*/*.xml,classpath:/server-corecfg/s*/*/*.xml,classpath:/server-corecfg/s*/*/*/*.xml
	
	
		spring.profiles.default
		production
	

	
		org.springframework.web.context.ContextLoaderListener
	

	
	
		CXFServlet
		org.springframework.web.servlet.DispatcherServlet
		
			spring.profiles.default
			production
		
		1
	
	
		CXFServlet
		/ws/*
	
	

좋은 웹페이지 즐겨찾기