
[英]Spring Boot not reading application.properties for DataSource
[英]How to config DataSource in Spring through beans in XML instead of application.properties?
我已经使用STS创建了新的Spring Starter项目。
在pom文件中,我添加了:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
然后,我创建了beans.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="databaseService" class="com.pckg.DatabaseService">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="username" value="postgres"/>
<property name="password" value="password"/>
<property name="url" value="jdbc:postgresql://localhost:5432/postgres"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
<qualifier value="transactionManager" />
</bean>
</beans>
并添加到我几乎为空的web.xml文件中:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans.xml</param-value>
</context-param>
我的DatabaseService类如下所示:
import org.apache.tomcat.jdbc.pool.DataSource;
...
@Service("databaseService")
@Transactional
public class DatabaseService {
private NamedParameterJdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
if (jdbcTemplate == null)
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
public DatabaseService(DataSource dataSource){
this.setDataSource(dataSource);
}
public DatabaseService(){
}
public String getData(){
//jdbcTemplate.query("SELECT 1",.....);
return null;
}
}
我想使此配置正常工作。 由于某些原因,这些bean未显示,因此我无法启动此应用程序。 我有一个例外:
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE.
那是因为application.properties不包含spring.datasource.driver-class-name=...
条目和我包含在beans.xml文件中的其他条目
当我输入该数据并启动应用程序时,beans.xml中提供的其他信息将被忽略,并且我相信这些Bean均不会被“执行”。 我希望transactionManager参与我的SQL查询,目前这是不可能的。
第一个问题:为什么在使用spring-boot时创建XML文件? 如果您不打算使用它,并自行引导容器:只需替换依赖项:
去掉:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
加:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
并检查tomcat-jdbc是否在您的类路径中:
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>8.5.6</version>
</dependency>
否则,如果您想超越与spring-boot相关的异常,文档建议将其添加到application.properties中spring.datasource.continue-on-error=false
#如果初始化数据库时发生错误,请不要停止。
但我不能向您保证一切都会奏效。 为了安全起见,请从项目中删除所有与spring-boot相关的依赖项,并继续简单的spring,或者不理会基于xml的配置,而使用Spring-boot“ magic”以非常好的默认值引导您的容器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.