我正在使用Spring创建一个GAE项目,该项目也将使用云SQL。在本地测试这个应用程序我指的是我的本地MySQL环境但是当我将它部署到GAE时,它将指向云SQL实例。所以我想根据环境在datasource 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";
}
现在我想在applicationcontext.xml中使用Spring Expression语言实现相同的功能。我之前没有这样做过,也无法实现。请指导我。这就是我试过的
<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
答案 0 :(得分:2)
我相信你需要引用类名来使它们成为文字......
... 'com.mysql.jdbc.Driver' ...
然而,使用Spring Profiles(http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/beans.html#beans-definition-profiles-xml)更容易,并启用所需的配置文件。
答案 1 :(得分:1)
您可以使用Spring配置文件来更改具体的bean类。我们假设您有两个配置文件:&#34; prod&#34;和&#34; dev&#34;。你的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}