【Spring学习笔记】基于profile的高级装配
2017-06-10 15:40
615 查看
日常工作中有时候我们会面临一些问题,需要将代码在不同环境之前来回切换,比如在开发环境可以使用嵌入式数据库Hypersonic,这个在开发环境再适合不过了,但是要将他放在生产则就是行不通了。这个时候Spring提供的profile就可以发挥作用了。
下面是我的一个小demo:
作为Web应用的上下文参数;作为JNDI条目;
作为环境变量;
作为JVM的系统属性;
在集成测试类上,使用@ActiveProfiles注解设置。
作为JNDI条目;
作为环境变量;
作为JVM的系统属性;
在集成测试类上,使用@ActiveProfiles注解设置。
我个人比较喜欢在web.xml中配置,也就是作为DispatcherServlet的参数
下面是我的一个小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使用。
激活方法:
作为DispatcherServlet的初始化参数;作为Web应用的上下文参数;作为JNDI条目;
作为环境变量;
作为JVM的系统属性;
在集成测试类上,使用@ActiveProfiles注解设置。
作为JNDI条目;
作为环境变量;
作为JVM的系统属性;
在集成测试类上,使用@ActiveProfiles注解设置。
我个人比较喜欢在web.xml中配置,也就是作为DispatcherServlet的参数
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>测试demo</display-name> <!-- Spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/server-corecfg/applicationContext.xml,classpath:/server-corecfg/s*/*.xml,classpath:/server-corecfg/s*/*/*.xml,classpath:/server-corecfg/s*/*/*/*.xml</param-value> </context-param> <context-param><!-- 上下文默认生产 --> <param-name>spring.profiles.default</param-name> <param-value>production</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- CXF_BEGIN --> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param><!-- servlet默认生产 --> <param-name>spring.profiles.default</param-name> <param-value>production</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping> <!-- CXF_END --> </web-app>
相关文章推荐
- Spring学习笔记(六) --- 装配Bean之高级装配
- Spring学习笔记(二) Bean的高级装配
- [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)
- Spring学习笔记-C3-Spring装配Bean高级篇
- Spring学习笔记二:高级装配
- SpringInAction 学习笔记:高级装配
- [学习笔记]基于注解的spring3.0.x MVC学习笔记(八)
- spring学习笔记-2bean装配
- spring学习笔记:ioc容器高级特性
- [转]spring学习笔记7.自动装配(补充)
- [学习笔记]基于注解的spring3.0.x MVC学习笔记(五)
- ITCAST视频-Spring学习笔记(编码剖析Spring装配基本属性的原理)
- ITCAST视频-Spring学习笔记(Spring如何装配各种集合类型的属性)
- 基于注解的spring3.0.x MVC学习笔记(二)
- 基于spring source tools suite注解的spring3.0.x MVC学习笔记(二)
- 基于注解的spring3.0.x MVC学习笔记
- [原]spring学习笔记7.自动装配
- ITCAST视频-Spring学习笔记(@Autowire注解与自动装配)
- [学习笔记]基于注解的spring3.0.x MVC学习笔记(二)
- [学习笔记]基于注解的spring3.0.x MVC学习笔记(三)