[Spring 학습 노트] profile 기반 고급 조립
3350 단어 스프링 베이스
다음은 나의 작은 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/*