diff options
author | 2016-11-15 00:03:59 +0700 | |
---|---|---|
committer | 2016-11-15 09:49:27 +0300 | |
commit | 32a6feb195171e06d60117ad758cc8beb50e5ca3 (patch) | |
tree | 7e3c72fe408861e8d828f181a46816e1b200993f /server-core/src/main/java/com/juick/service/BaseJdbcService.java | |
parent | 73a2b1d4796ea18ef476f4ca30e2028c828d0ac1 (diff) |
Constructor injectection for service layer
Diffstat (limited to 'server-core/src/main/java/com/juick/service/BaseJdbcService.java')
-rw-r--r-- | server-core/src/main/java/com/juick/service/BaseJdbcService.java | 52 |
1 files changed, 26 insertions, 26 deletions
diff --git a/server-core/src/main/java/com/juick/service/BaseJdbcService.java b/server-core/src/main/java/com/juick/service/BaseJdbcService.java index 2f5669f5..fe544efe 100644 --- a/server-core/src/main/java/com/juick/service/BaseJdbcService.java +++ b/server-core/src/main/java/com/juick/service/BaseJdbcService.java @@ -2,24 +2,44 @@ package com.juick.service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.InitializingBean; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.support.TransactionTemplate; import org.springframework.util.Assert; -import javax.inject.Inject; +import javax.sql.DataSource; /** * Created by aalexeev on 11/13/16. */ -public abstract class BaseJdbcService implements InitializingBean { +public abstract class BaseJdbcService { protected final Logger logger = LoggerFactory.getLogger(getClass()); - private JdbcTemplate jdbcTemplate; - private TransactionTemplate transactionTemplate; - private NamedParameterJdbcTemplate namedParameterJdbcTemplate; + private final JdbcTemplate jdbcTemplate; + private final TransactionTemplate transactionTemplate; + private final NamedParameterJdbcTemplate namedParameterJdbcTemplate; + + + protected BaseJdbcService(JdbcTemplate jdbcTemplate, PlatformTransactionManager transactionManager) { + Assert.notNull(jdbcTemplate); + + this.jdbcTemplate = jdbcTemplate; + this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate); + this.transactionTemplate = (transactionManager == null) ? + null : new TransactionTemplate(transactionManager); + + } + + protected BaseJdbcService(DataSource dataSource, PlatformTransactionManager transactionManager) { + Assert.notNull(dataSource); + + this.jdbcTemplate = new JdbcTemplate(dataSource); + this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate); + this.transactionTemplate = (transactionManager == null) ? + null : new TransactionTemplate(transactionManager); + + } protected JdbcTemplate getJdbcTemplate() { return jdbcTemplate; @@ -32,24 +52,4 @@ public abstract class BaseJdbcService implements InitializingBean { protected TransactionTemplate getTransactionTemplate() { return transactionTemplate; } - - @Override - public void afterPropertiesSet() throws Exception { - logger.debug("Checking required beans"); - - Assert.notNull(jdbcTemplate); - Assert.notNull(namedParameterJdbcTemplate); - Assert.notNull(transactionTemplate); - } - - @Inject - public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { - this.jdbcTemplate = jdbcTemplate; - namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate); - } - - @Inject - public void setTransactionManager(PlatformTransactionManager transactionManager) { - transactionTemplate = new TransactionTemplate(transactionManager); - } } |