STSを使用して新しいSpring Starter Projectを作成しました。 POMファイルでSpringでDataSourceをapplication.propertiesの代わりにXMLで設定する方法はありますか?
私が追加しました:
<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ファイルにaddded
<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;
}
}
この設定を有効にしたいと思います。何らかの理由でそれらの豆は見られず、私はこのアプリケーションを起動できません。 私は例外持っている:
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私はbeans.xmlファイルに含めるspring.datasource.driver-class-name=...
エントリや他のエントリが含まれていないためです
私が入力し、そのデータとアプリケーションが起動beans.xmlで提供される他の情報は無視され、このBeanのどれも '実行されていない'と私は信じています。私は、トランザクションマネージャが私のSQLクエリに参加し、現在は不可能です。
なぜXMLを使用し、 'application.properties'を使用しないのですか?私は普通の使用では不可能なことは何も見ていないのですか? –
application.properties内のデータから作成されたDataSourceには、すべての設定がありませんでした。そのデータを持つBeanを自分で作成したいのですが、@Configurationアノテーションを使用してJavaクラスのBeanを作成しました。 – Kamil