SPRING IN ACTION 第4版笔记-第三章Advancing wiring-001-DataSource在应用和开发环境之间切换 profile...

本文介绍了如何使用Spring的bean profiles特性来管理不同环境(如开发和生产)下的特定bean,通过配置不同的环境属性来决定哪些bean会被创建。

一、

DataSource在应用和开发环境的产生方式不同,可以用srping 的profile管理

Spring’s solution for environment-specific beans isn’t much different from build-time
solutions. Certainly, an environment-specific decision is made as to which beans will
and won’t be created. But rather than make that decision at build time, Spring waits to
make the decision at runtime. Consequently, the same deployment unit (perhaps a
WAR file) will work in all environments without being rebuilt.
In version 3.1, Spring introduced bean profiles. To use profiles, you must gather all

二、

1.java

 1 package com.myapp;
 2 
 3 import javax.sql.DataSource;
 4 
 5 import org.springframework.context.annotation.Bean;
 6 import org.springframework.context.annotation.Configuration;
 7 import org.springframework.context.annotation.Profile;
 8 import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
 9 import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
10 import org.springframework.jndi.JndiObjectFactoryBean;
11 
12 @Configuration
13 public class DataSourceConfig {
14   
15   @Bean(destroyMethod = "shutdown")
16   @Profile("dev")
17   public DataSource embeddedDataSource() {
18     return new EmbeddedDatabaseBuilder()
19         .setType(EmbeddedDatabaseType.H2)
20         .addScript("classpath:schema.sql")
21         .addScript("classpath:test-data.sql")
22         .build();
23   }
24 
25   @Bean
26   @Profile("prod")
27   public DataSource jndiDataSource() {
28     JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
29     jndiObjectFactoryBean.setJndiName("jdbc/myDS");
30     jndiObjectFactoryBean.setResourceRef(true);
31     jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class);
32     return (DataSource) jndiObjectFactoryBean.getObject();
33   }
34 
35 }

 

2.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 4   xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p"
 5   xsi:schemaLocation="
 6     http://www.springframework.org/schema/jee
 7     http://www.springframework.org/schema/jee/spring-jee.xsd
 8     http://www.springframework.org/schema/jdbc
 9     http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
10     http://www.springframework.org/schema/beans
11     http://www.springframework.org/schema/beans/spring-beans.xsd">
12 
13   <beans profile="dev">
14     <jdbc:embedded-database id="dataSource" type="H2">
15       <jdbc:script location="classpath:schema.sql" />
16       <jdbc:script location="classpath:test-data.sql" />
17     </jdbc:embedded-database>
18   </beans>
19   
20   <beans profile="prod">
21     <jee:jndi-lookup id="dataSource"
22       lazy-init="true"
23       jndi-name="jdbc/myDatabase"
24       resource-ref="true"
25       proxy-interface="javax.sql.DataSource" />
26   </beans>
27 </beans>

 

转载于:https://www.cnblogs.com/shamgod/p/5235195.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值