Leverage Spring Boot's autoconfiguration (#441)
* Leverage Spring Boot's autoconfiguration * Remove unnecessary @PropertySource
This commit is contained in:
parent
d8cd30d0fd
commit
632a1f613f
|
@ -1,62 +1,9 @@
|
||||||
package com.baeldung.jooq.springboot;
|
package com.baeldung.jooq.springboot;
|
||||||
|
|
||||||
import com.baeldung.jooq.introduction.ExceptionTranslator;
|
|
||||||
import org.jooq.impl.DataSourceConnectionProvider;
|
|
||||||
import org.jooq.impl.DefaultConfiguration;
|
|
||||||
import org.jooq.impl.DefaultDSLContext;
|
|
||||||
import org.jooq.impl.DefaultExecuteListenerProvider;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.core.env.Environment;
|
|
||||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
|
||||||
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
|
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableTransactionManagement
|
@EnableTransactionManagement
|
||||||
public class Application {
|
public class Application {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private Environment environment;
|
|
||||||
|
|
||||||
public DataSource dataSource() {
|
|
||||||
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
|
||||||
|
|
||||||
dataSource.setUrl(environment.getRequiredProperty("spring.datasource.url"));
|
|
||||||
dataSource.setUsername(environment.getRequiredProperty("spring.datasource.username"));
|
|
||||||
dataSource.setPassword(environment.getRequiredProperty("spring.datasource.password"));
|
|
||||||
|
|
||||||
return dataSource;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public TransactionAwareDataSourceProxy transactionAwareDataSource() {
|
|
||||||
return new TransactionAwareDataSourceProxy(dataSource());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public DataSourceConnectionProvider connectionProvider() {
|
|
||||||
return new DataSourceConnectionProvider(transactionAwareDataSource());
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExceptionTranslator exceptionTransformer() {
|
|
||||||
return new ExceptionTranslator();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public DefaultDSLContext dsl() {
|
|
||||||
return new DefaultDSLContext(configuration());
|
|
||||||
}
|
|
||||||
|
|
||||||
public DefaultConfiguration configuration() {
|
|
||||||
DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
|
|
||||||
|
|
||||||
jooqConfiguration.set(connectionProvider());
|
|
||||||
jooqConfiguration.set(new DefaultExecuteListenerProvider(exceptionTransformer()));
|
|
||||||
|
|
||||||
return jooqConfiguration;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
package com.baeldung.jooq.springboot;
|
||||||
|
|
||||||
|
import com.baeldung.jooq.introduction.ExceptionTranslator;
|
||||||
|
import org.jooq.impl.DataSourceConnectionProvider;
|
||||||
|
import org.jooq.impl.DefaultConfiguration;
|
||||||
|
import org.jooq.impl.DefaultDSLContext;
|
||||||
|
import org.jooq.impl.DefaultExecuteListenerProvider;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class InitialConfiguration {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DataSource dataSource;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public DataSourceConnectionProvider connectionProvider() {
|
||||||
|
return new DataSourceConnectionProvider(new TransactionAwareDataSourceProxy(dataSource));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public DefaultDSLContext dsl() {
|
||||||
|
return new DefaultDSLContext(configuration());
|
||||||
|
}
|
||||||
|
|
||||||
|
public DefaultConfiguration configuration() {
|
||||||
|
DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
|
||||||
|
|
||||||
|
jooqConfiguration.set(connectionProvider());
|
||||||
|
jooqConfiguration.set(new DefaultExecuteListenerProvider(new ExceptionTranslator()));
|
||||||
|
|
||||||
|
return jooqConfiguration;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue