It may seem an obvious mistake, but I am not able to find it. Following is my code and configuration:
Application Class
@SpringBootApplication
public class Application {
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
}
SQL Config Class
@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 CLASS
@Repository
public class CustomDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
.
.
@Component DAO Class
@Component
public class AnotherDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
.
.
The JdbcTemplate object gets populated for @Component annotated class. But the object is null for @Repository annotated class. I started spring boot with logging set to DEBUG, and couldn't find any error while creating JdbcTemplate bean. From logs, I can infer the method is called and the bean is created. The JdbcTemplate also has reference to DataSource.
If the @Repository annotation is changed to @Component the code works fine.
- When using spring jdbc, can we annotate a DAO class with
@Repository? - Why the dependency injection works with
@Componentbut fails for@Repository?
As per the suggestion from M. Deinum, I have made following changes.
- Removed
PostgreSQLConfigclass - Added
application.propertiesfile
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
Properties are loaded by spring application. Following are log messages:
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]
I receive an exception. Following are the details:
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 classes are marked with @Repository annotation.
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'
}
PostgreSQLConfigand just add some properties toapplication.properties, Spring Boot already configures a datasource and JdbcTemplate no need to do that yourself. dao/repo should be annotated with@Repository. - M. Deinumspring-boot-starter-jdbcas a dependency? - M. Deinum