|
| 1 | +/* |
| 2 | + * Copyright 2012-2015 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 | + * http://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.jooq; |
| 18 | + |
| 19 | +import javax.sql.DataSource; |
| 20 | + |
| 21 | +import org.jooq.ConnectionProvider; |
| 22 | +import org.jooq.DSLContext; |
| 23 | +import org.jooq.ExecuteListenerProvider; |
| 24 | +import org.jooq.RecordListenerProvider; |
| 25 | +import org.jooq.RecordMapperProvider; |
| 26 | +import org.jooq.SQLDialect; |
| 27 | +import org.jooq.TransactionProvider; |
| 28 | +import org.jooq.VisitListenerProvider; |
| 29 | +import org.jooq.conf.Settings; |
| 30 | +import org.jooq.impl.DataSourceConnectionProvider; |
| 31 | +import org.jooq.impl.DefaultConfiguration; |
| 32 | +import org.jooq.impl.DefaultDSLContext; |
| 33 | +import org.jooq.impl.DefaultExecuteListenerProvider; |
| 34 | +import org.springframework.beans.factory.annotation.Autowired; |
| 35 | +import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
| 36 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 37 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
| 38 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 39 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 40 | +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; |
| 41 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 42 | +import org.springframework.context.annotation.Bean; |
| 43 | +import org.springframework.context.annotation.Configuration; |
| 44 | +import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; |
| 45 | +import org.springframework.transaction.PlatformTransactionManager; |
| 46 | +import org.springframework.util.StringUtils; |
| 47 | + |
| 48 | +/** |
| 49 | + * {@link EnableAutoConfiguration Auto-configuration} for JOOQ. |
| 50 | + * |
| 51 | + * @author Andreas Ahlenstorf |
| 52 | + * @since 1.3.0 |
| 53 | + */ |
| 54 | +@Configuration |
| 55 | +@ConditionalOnClass(DSLContext.class) |
| 56 | +@ConditionalOnBean(DataSource.class) |
| 57 | +@AutoConfigureAfter(DataSourceAutoConfiguration.class) |
| 58 | +public class JooqAutoConfiguration { |
| 59 | + |
| 60 | + @Bean |
| 61 | + @ConditionalOnMissingBean(DataSourceConnectionProvider.class) |
| 62 | + public DataSourceConnectionProvider dataSourceConnectionProvider(DataSource dataSource) { |
| 63 | + return new DataSourceConnectionProvider(new TransactionAwareDataSourceProxy( |
| 64 | + dataSource)); |
| 65 | + } |
| 66 | + |
| 67 | + @Bean |
| 68 | + @ConditionalOnBean(PlatformTransactionManager.class) |
| 69 | + public TransactionProvider transactionProvider(PlatformTransactionManager txManager) { |
| 70 | + return new SpringTransactionProvider(txManager); |
| 71 | + } |
| 72 | + |
| 73 | + @Bean |
| 74 | + public ExecuteListenerProvider jooqExceptionTranslatorExecuteListenerProvider() { |
| 75 | + return new DefaultExecuteListenerProvider(new JooqExceptionTranslator()); |
| 76 | + } |
| 77 | + |
| 78 | + @Configuration |
| 79 | + @ConditionalOnMissingBean(DSLContext.class) |
| 80 | + @EnableConfigurationProperties(JooqProperties.class) |
| 81 | + public static class DslContextConfiguration { |
| 82 | + |
| 83 | + @Autowired |
| 84 | + private JooqProperties properties = new JooqProperties(); |
| 85 | + |
| 86 | + @Autowired |
| 87 | + private ConnectionProvider connectionProvider; |
| 88 | + |
| 89 | + @Autowired(required = false) |
| 90 | + private TransactionProvider transactionProvider; |
| 91 | + |
| 92 | + @Autowired(required = false) |
| 93 | + private RecordMapperProvider recordMapperProvider; |
| 94 | + |
| 95 | + @Autowired(required = false) |
| 96 | + private Settings settings; |
| 97 | + |
| 98 | + @Autowired(required = false) |
| 99 | + private RecordListenerProvider[] recordListenerProviders; |
| 100 | + |
| 101 | + @Autowired |
| 102 | + private ExecuteListenerProvider[] executeListenerProviders; |
| 103 | + |
| 104 | + @Autowired(required = false) |
| 105 | + private VisitListenerProvider[] visitListenerProviders; |
| 106 | + |
| 107 | + @Bean |
| 108 | + public DefaultDSLContext dslContext(org.jooq.Configuration configuration) { |
| 109 | + return new DefaultDSLContext(configuration); |
| 110 | + } |
| 111 | + |
| 112 | + @Bean |
| 113 | + @ConditionalOnMissingBean(org.jooq.Configuration.class) |
| 114 | + public DefaultConfiguration jooqConfiguration() { |
| 115 | + DefaultConfiguration configuration = new DefaultConfiguration(); |
| 116 | + if (!StringUtils.isEmpty(this.properties.getSqlDialect())) { |
| 117 | + configuration.set(SQLDialect.valueOf(this.properties.getSqlDialect())); |
| 118 | + } |
| 119 | + configuration.set(this.connectionProvider); |
| 120 | + if (this.transactionProvider != null) { |
| 121 | + configuration.set(this.transactionProvider); |
| 122 | + } |
| 123 | + if (this.recordMapperProvider != null) { |
| 124 | + configuration.set(this.recordMapperProvider); |
| 125 | + } |
| 126 | + if (this.settings != null) { |
| 127 | + configuration.set(this.settings); |
| 128 | + } |
| 129 | + configuration.set(this.recordListenerProviders); |
| 130 | + configuration.set(this.executeListenerProviders); |
| 131 | + configuration.set(this.visitListenerProviders); |
| 132 | + return configuration; |
| 133 | + } |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments