明らかな間違いのように見えるかもしれませんが、私はそれを見つけることができません。 DAOに@Repositoryがあるとき@AutowiredがJdbcTemplateで失敗する
@Configuration
public class PostgreSQLConfig {
@Bean
public DataSource getDBDataSource() {
final DriverManagerDataSource driverManagerDataSource
= new DriverManagerDataSource(
"jdbc:postgresql://localhost:5432/applicationdb",
"username",
"password"
);
driverManagerDataSource.setDriverClassName("org.postgresql.Driver");
return driverManagerDataSource;
}
@Bean
public JdbcTemplate getJdbcTemplate(final DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
@Repository DAOのクラス
@Repository
public class CustomDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
.
.
@Component DAOクラス
クラスアプリケーションクラス
@SpringBootApplication
public class Application {
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
}
SQL設定:以下は、私のコードと構成です
JdbcTemplate
オブジェクトには、@Component
注釈付きクラスが設定されています。しかし、オブジェクトは@Repository
注釈付きクラスではnullです。私はDEBUGに設定されたロギングを使用して春ブートを開始しました。JdbcTemplate
beanを作成する際にエラーを検出できませんでした。ログから、メソッドが呼び出され、Beanが作成されたと推測できます。 JdbcTemplate
もDataSource
を参照しています。
@Repository
注釈が@Component
に変更された場合、コードは正常に動作します。
- 春のJDBCを使用して、我々は
@Repository
とDAOクラスに注釈を付けることができますか? - なぜ依存性注入は
@Component
で動作しますが、@Repository
で失敗しますか?
M.デイナムからの示唆によれば、以下の変更を加えました。
- 追加
application.properties
ファイル
application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/applicationdb
spring.datasource.user=username
spring.datasource.password=password
spring.datasource.driver-class-name=org.postgresql.Driver
プロパティは、SpringアプリケーションによってロードされているPostgreSQLConfig
クラス
2017-01-06 15:03:02 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Found key 'spring.datasource.driver-class-name' in [applicationConfigurationProperties] with type [String]
2017-01-06 15:03:02 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Found key 'spring.datasource.password' in [applicationConfigurationProperties] with type [String]
2017-01-06 15:03:02 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Found key 'spring.datasource.user' in [applicationConfigurationProperties] with type [String]
2017-01-06 15:03:02 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Found key 'spring.datasource.url' in [applicationConfigurationProperties] with type [String]
私は例外を受け取ります。次の詳細は以下のとおりです。
2017-01-06 15:03:05 [main] DEBUG o.s.b.d.LoggingFailureAnalysisReporter - Application failed to start due to an exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1474)
DAOクラスが@Repository
注釈が付いています。
build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.4.3.RELEASE'
}
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'idea'
apply plugin: 'application'
apply plugin: 'org.springframework.boot'
def applicationVersion = '0.0.1-SNAPSHOT'
def dependencyVersions = [
slf4j: '1.7.22',
logback: '1.1.8',
postgresql: '9.4.1212',
junit: '4.12'
]
group = 'com.appl'
version = applicationVersion
mainClassName = 'com.appl.Application'
war {
baseName = 'my-custom-appl'
version = applicationVersion
}
war.dependsOn test
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
configurations.all {
exclude group: 'commons-logging', module: 'commons-logging'
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-jdbc'
compile 'org.springframework:spring-jdbc'
compile "org.slf4j:jcl-over-slf4j:${dependencyVersions.slf4j}"
compile "org.postgresql:postgresql:${dependencyVersions.postgresql}"
compile "ch.qos.logback:logback-classic:${dependencyVersions.logback}"
testCompile "junit:junit:${dependencyVersions.junit}"
testCompile 'org.springframework.boot:spring-boot-starter-test'
}
あなたの輸入明細書を確認しましたか? –
すべての注釈が正しく読み込まれます。 JdbcTemplateの場合、私は自分自身を2回検証しました。 –
私は質問に言及するのを忘れて、私は春4.3.5を使用しています。私はPOC製品のために開発していました。 –