如何通过 XML 中的 beans 而不是 application.properties 中的 beans 配置 Spring 中的数据源?
How to config DataSource in Spring through beans in XML instead of application.properties?
我使用 STS 创建了新的 Spring 入门项目。
我在 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>
我的数据库服务 class 看起来像这样:
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 的 none 是 'executed'。我希望 transactionManager 参与我的 SQL 查询,目前这是不可能的。
第一个问题:为什么在使用spring-boot 时创建XML 文件?如果您不打算使用它,并且bootstrap您自己的容器:只需替换依赖项:
删除:
<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相关的异常the documentation suggests 添加到application.properties
spring.datasource.continue-on-error=false
# 初始化数据库出错不停止
但我不能向您保证一切都会奏效。为了安全起见,从项目中删除任何与 spring-boot 相关的依赖项并继续使用简单的 spring,或者忽略基于 xml 的配置以支持 Spring-boot "magic" 到 bootstrap 您的容器具有非常好的默认值。
我使用 STS 创建了新的 Spring 入门项目。
我在 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>
我的数据库服务 class 看起来像这样:
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 的 none 是 'executed'。我希望 transactionManager 参与我的 SQL 查询,目前这是不可能的。
第一个问题:为什么在使用spring-boot 时创建XML 文件?如果您不打算使用它,并且bootstrap您自己的容器:只需替换依赖项:
删除:
<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相关的异常the documentation suggests 添加到application.properties
spring.datasource.continue-on-error=false
# 初始化数据库出错不停止
但我不能向您保证一切都会奏效。为了安全起见,从项目中删除任何与 spring-boot 相关的依赖项并继续使用简单的 spring,或者忽略基于 xml 的配置以支持 Spring-boot "magic" 到 bootstrap 您的容器具有非常好的默认值。