2015-02-08 27 views
2

我正在使用Spring創建一個GAE項目,該項目也將使用雲SQL。在本地測試這個應用程序時,我指向我的本地MySQL環境,但是當我將它部署到GAE時,它將指向雲SQL實例。所以我想根據環境在數據源bean中配置我的driverName。對此,我們一般採用以下在我們的Java代碼如何在applicationContext.xml中有條件地使用數據源

if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) { 
    // Load the class that provides the new "jdbc:google:mysql://" prefix. 
    Class.forName("com.mysql.jdbc.GoogleDriver"); 
    url = "jdbc:google:mysql://<your-project-id>:<your-instance-name>/<your-database-name>?user=root"; 
} else { 
    // Local MySQL instance to use during development. 
    Class.forName("com.mysql.jdbc.Driver"); 
    url = "jdbc:mysql://127.0.0.1:3306/<your-database-name>?user=root"; 
} 

現在我想用Spring表達式語言來實現的applicationContext.xml一樣。我之前沒有這樣做過,也無法實現。請指導我。這是我試過

<bean id="isDev" class="java.lang.Boolean"> 
    <constructor-arg value="#{ systemProperties[environment.value] == SystemProperty.Environment.Value.Development ? true : false }" /> 
</bean> 

<bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="#{ isDev ? com.mysql.jdbc.Driver : com.mysql.jdbc.GoogleDriver }" /> 
. 
. 
. 

但我發現了異常,連接異常

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'feedbackFormController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.sandeepapplabs.custengage.services.FeedbackFormService com.sandeepapplabs.custengage.controllers.FeedbackFormController.feedbackFormService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'feedbackFormService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.sandeepapplabs.custengage.daos.FeedbackFormDAO com.sandeepapplabs.custengage.services.impl.FeedbackFormServiceImpl.feedbackFormDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'feedbackFormDAO' defined in ServletContext resource [/WEB-INF/custengage-servlet.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/custengage-servlet.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is java.lang.NullPointerException 

回答

1

您可以使用Spring配置文件來更改具體的bean類。假設你有2個配置文件:「prod」和「dev」。你的bean的方法應該有如下@profile註釋:

@Configuration 
public class AppConfig { 

... 

    @Bean 
    @Profile("prod") 
    public Object prodDataSource() { 

    return new ... 
    } 

    @Bean 
    @Profile("dev") 
    public Object getDataSource() throws Exception { 
    return new ... 
    } 

} 

如果您正在使用Maven,您可以通過選擇pom.xml的配置文件:

<profiles> 
    <profile> 
     <id>dev</id> 
     <activation> 
      <activeByDefault>true</activeByDefault> 
     </activation> 
     <properties> 
      <spring.profile>dev</spring.profile> 
     </properties> 
    </profile> 

    <profile> 
     <id>prod</id> 
     <activation> 
      <activeByDefault>false</activeByDefault> 
      <property> 
       <name>prod</name> 
      </property> 
     </activation> 
     <properties> 
      <spring.profile>prod</spring.profile> 
     </properties> 
    </profile> 

</profiles> 

您可以通過-D像通過配置文件選擇參數: mvn -Dprod clean compile test install

編輯:

application.properties以下屬性選擇配置文件:我不是在我的項目@Efe使用Maven

spring.profiles.active=${spring.profile} 
+0

:(我會嘗試使用Spring配置文件你。如果你不使用Maven提到 – 2015-02-10 17:32:16

+0

的方式,你可以簡單地改變'Spring.profiles.active'屬性位於Spring屬性文件中(通常爲'application.properties') – 2015-02-11 14:55:54

相關問題