Skip to content

Commit 275d794

Browse files
committed
Make sure JdbcTemplateConfiguration is imported once
Closes gh-13806
1 parent 2f244c8 commit 275d794

File tree

3 files changed

+97
-41
lines changed

3 files changed

+97
-41
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfiguration.java

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,11 @@
2121
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2222
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
24-
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2524
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
2625
import org.springframework.boot.context.properties.EnableConfigurationProperties;
27-
import org.springframework.context.annotation.Bean;
2826
import org.springframework.context.annotation.Configuration;
2927
import org.springframework.context.annotation.Import;
30-
import org.springframework.context.annotation.Primary;
31-
import org.springframework.jdbc.core.JdbcOperations;
3228
import org.springframework.jdbc.core.JdbcTemplate;
33-
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
3429
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
3530

3631
/**
@@ -48,42 +43,8 @@
4843
@ConditionalOnSingleCandidate(DataSource.class)
4944
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
5045
@EnableConfigurationProperties(JdbcProperties.class)
46+
@Import({ JdbcTemplateConfiguration.class,
47+
NamedParameterJdbcTemplateConfiguration.class })
5148
public class JdbcTemplateAutoConfiguration {
5249

53-
@Configuration(proxyBeanMethods = false)
54-
static class JdbcTemplateConfiguration {
55-
56-
@Bean
57-
@Primary
58-
@ConditionalOnMissingBean(JdbcOperations.class)
59-
public JdbcTemplate jdbcTemplate(DataSource dataSource,
60-
JdbcProperties properties) {
61-
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
62-
JdbcProperties.Template template = properties.getTemplate();
63-
jdbcTemplate.setFetchSize(template.getFetchSize());
64-
jdbcTemplate.setMaxRows(template.getMaxRows());
65-
if (template.getQueryTimeout() != null) {
66-
jdbcTemplate
67-
.setQueryTimeout((int) template.getQueryTimeout().getSeconds());
68-
}
69-
return jdbcTemplate;
70-
}
71-
72-
}
73-
74-
@Configuration(proxyBeanMethods = false)
75-
@Import(JdbcTemplateConfiguration.class)
76-
static class NamedParameterJdbcTemplateConfiguration {
77-
78-
@Bean
79-
@Primary
80-
@ConditionalOnSingleCandidate(JdbcTemplate.class)
81-
@ConditionalOnMissingBean(NamedParameterJdbcOperations.class)
82-
public NamedParameterJdbcTemplate namedParameterJdbcTemplate(
83-
JdbcTemplate jdbcTemplate) {
84-
return new NamedParameterJdbcTemplate(jdbcTemplate);
85-
}
86-
87-
}
88-
8950
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.jdbc;
18+
19+
import javax.sql.DataSource;
20+
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.context.annotation.Primary;
25+
import org.springframework.jdbc.core.JdbcOperations;
26+
import org.springframework.jdbc.core.JdbcTemplate;
27+
28+
/**
29+
* Configuration for {@link JdbcTemplateConfiguration}.
30+
*
31+
* @author Stephane Nicoll
32+
*/
33+
@Configuration(proxyBeanMethods = false)
34+
@ConditionalOnMissingBean(JdbcOperations.class)
35+
class JdbcTemplateConfiguration {
36+
37+
@Bean
38+
@Primary
39+
public JdbcTemplate jdbcTemplate(DataSource dataSource, JdbcProperties properties) {
40+
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
41+
JdbcProperties.Template template = properties.getTemplate();
42+
jdbcTemplate.setFetchSize(template.getFetchSize());
43+
jdbcTemplate.setMaxRows(template.getMaxRows());
44+
if (template.getQueryTimeout() != null) {
45+
jdbcTemplate.setQueryTimeout((int) template.getQueryTimeout().getSeconds());
46+
}
47+
return jdbcTemplate;
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.jdbc;
18+
19+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.context.annotation.Primary;
24+
import org.springframework.jdbc.core.JdbcTemplate;
25+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
26+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
27+
28+
/**
29+
* Configuration for {@link NamedParameterJdbcTemplate}.
30+
*
31+
* @author Stephane Nicoll
32+
*/
33+
@Configuration(proxyBeanMethods = false)
34+
@ConditionalOnSingleCandidate(JdbcTemplate.class)
35+
@ConditionalOnMissingBean(NamedParameterJdbcOperations.class)
36+
class NamedParameterJdbcTemplateConfiguration {
37+
38+
@Bean
39+
@Primary
40+
public NamedParameterJdbcTemplate namedParameterJdbcTemplate(
41+
JdbcTemplate jdbcTemplate) {
42+
return new NamedParameterJdbcTemplate(jdbcTemplate);
43+
}
44+
45+
}

0 commit comments

Comments
 (0)