【发布时间】:2017-05-20 05:00:51
【问题描述】:
这似乎是一个明显的错误,但我无法找到它。以下是我的代码和配置:
应用类
@SpringBootApplication
public class Application {
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
}
SQL 配置类
@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 类
@Component
public class AnotherDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
.
.
JdbcTemplate 对象为 @Component 注释类填充。但是 @Repository 带注释的类的对象为空。我在将日志记录设置为 DEBUG 的情况下启动了 Spring Boot,但在创建 JdbcTemplate bean 时找不到任何错误。从日志中,我可以推断出方法被调用并创建了 bean。 JdbcTemplate 也引用了DataSource。
如果将@Repository 注释更改为@Component,则代码可以正常工作。
- 在使用spring jdbc时,我们可以用注解一个DAO类吗?
@Repository? - 为什么依赖注入与
@Component一起工作但失败了@Repository?
根据 M. Deinum 的建议,我做了以下更改。
- 删除了
PostgreSQLConfig类 - 添加了
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 应用程序加载。以下是日志消息:
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,我自己验证了两次。
-
我忘了在问题中提到,我使用的是 Spring 4.3.5.RELEASE。我正在开发 POC 产品。
-
删除你的
PostgreSQLConfig,只为application.properties添加一些属性,Spring Boot已经配置了一个数据源,JdbcTemplate不需要自己做。 dao/repo 应使用@Repository注释。 -
你有
spring-boot-starter-jdbc作为依赖吗?
标签: spring spring-boot spring-jdbc