当前位置: 代码迷 >> Web前端 >> spring 环境配备应用
  详细解决方案

spring 环境配备应用

热度:128   发布时间:2012-11-22 00:16:41.0
spring 环境配置应用

?

复制代码
<!--

 app-config.xml 

-->


<
beans 
xmlns
="http://www.springframework.org/schema/beans"

xmlns:xs
="http://www.w3.org/2001/XMLSchema-instance"

xmlns:jdbc
="http://www.springframework.org/schema/jdbc"

xmlns:jee
="http://www.springframework.org/schema/jee"

xsi:schemaLocation
="..."
>


    <
bean 
id
="transferService"
 
class
="com.bank.service.internal.DefaultTransferService"
>

        <
constructor-arg 
ref
="accountRepository"
/>

        <
constructor-arg 
ref
="feePolicy"
/>

    </
bean
>


    <
bean 
id
="accountRepository"
 class
="com.bank.repository.internal.JdbcAccountRepository"
>

        <
constructor-arg 
ref
="dataSource"
/>

    </
bean
>


    <
bean 
id
="feePolicy"
 class
="com.bank.service.internal.ZeroFeePolicy"
/>


    <
beans 
profile
="dev"
>

        <
jdbc:embedded-database 
id
="dataSource"
>

            <
jdbc:script 
location
="classpath:com/bank/config/sql/schema.sql"
 />

            <
jdbc:script 
location
="classpath:com/bank/config/sql/test-data.sql"
 />

        </
jdbc:embedded-database
>

    </
beans
>


    <
beans 
profile
="production"
>

        <
jee:jndi-lookup 
id
="dataSource"
 
jndi-name
="java:comp/env/jdbc/datasource"
 />

    </
beans
>


</
beans
>

复制代码
复制代码
package

 com.bank.service;

@RunWith(SpringJUnit4ClassRunner.class

)
//

 ApplicationContext will be loaded from "classpath:/app-config.xml"




@ContextConfiguration("/app-config.xml")
@ActiveProfiles("dev")
public

 class

 TransferServiceTest {
    @Autowired
    private

 TransferService transferService;

    @Test
    public

 void

 testTransferService() {
        //

 test the transferService




    }
}
复制代码

这个除了可以切换开发、部署环境,也可以方便地切换不同的数据库。实战中发现这个TestCase还可以被继承,其配置也会被继承,所以现在的 做法是写一个基础TestCase,配置好ContextConfiguration、ActiveProfiles,其他TestCase继承该基础 TestCase。

但是集成到web环境中时,却很久找不到如何在web.xml中切换这个环境配置,找了很久总算找到了,在web.xml中加入:

<
context-param
>


    <
param-name
>
spring.profiles.active</
param-name
>


    <
param-value
>
dev</
param-value
>


</
context-param
>

经测试已成功,特此记录分享。

?

以上转自:http://www.cnblogs.com/chanedi/archive/2012/01/12/2320837.html

?

?

下为另一种应用在多语言支持

?

web.xml片段

<!-- 韵定Spring Context的Profile -->
??? <context-param>
??? ??? <param-name>spring.profiles.active</param-name>
??? ??? <param-value>rbac

</param-value>
??? </context-param>

?

资源文件片段(*.properties)

login.password
=Password

?

页面片段(fmt为jstl标签)

<td align="right"? width="56" height="33"><fmt:message key="rbac
.login.password
"/></td>
<td><input name="password" type="password" class="input_txt"? id="login-password"/></td>

l绿色与红色字体

?

?

?

  相关解决方案