[英]How to use datasource conditionally in applicationContext.xml
我正在使用Spring創建一個GAE項目,該項目也將使用雲SQL。 在本地測試此應用程序時,我指的是本地MySQL環境,但是當我將其部署到GAE時,它將指向雲SQL實例。 所以我想根據環境在數據源bean中配置我的driverName。 為此,我們通常在Java代碼中使用以下代碼
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 Expression語言在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
我相信您需要引用類名以使其成為文字。
... 'com.mysql.jdbc.Driver' ...
但是,使用Spring Profiles]( http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/beans.html#beans-definition-profiles-xml )並啟用它會更容易您想要的個人資料。
您可以使用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
以下屬性選擇配置文件:
spring.profiles.active=${spring.profile}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.