diff options
author | Alexander Alexeev | 2016-11-15 00:03:59 +0700 |
---|---|---|
committer | Vitaly Takmazov | 2016-11-15 09:49:27 +0300 |
commit | 32a6feb195171e06d60117ad758cc8beb50e5ca3 (patch) | |
tree | 7e3c72fe408861e8d828f181a46816e1b200993f /server-core/src/main/java | |
parent | 73a2b1d4796ea18ef476f4ca30e2028c828d0ac1 (diff) |
Constructor injectection for service layer
Diffstat (limited to 'server-core/src/main/java')
12 files changed, 109 insertions, 31 deletions
diff --git a/server-core/src/main/java/com/juick/service/AdsServiceImpl.java b/server-core/src/main/java/com/juick/service/AdsServiceImpl.java index aeb2dd85..a6a5c85d 100644 --- a/server-core/src/main/java/com/juick/service/AdsServiceImpl.java +++ b/server-core/src/main/java/com/juick/service/AdsServiceImpl.java @@ -1,8 +1,11 @@ package com.juick.service; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; +import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.Transactional; +import javax.inject.Inject; import java.util.List; /** @@ -12,6 +15,11 @@ import java.util.List; @Transactional public class AdsServiceImpl extends BaseJdbcService implements AdsService { + @Inject + public AdsServiceImpl(JdbcTemplate jdbcTemplate) { + super(jdbcTemplate, null); + } + @Transactional(readOnly = true) @Override public int getAdMid(final int uid) { 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); - } } diff --git a/server-core/src/main/java/com/juick/service/CrosspostServiceImpl.java b/server-core/src/main/java/com/juick/service/CrosspostServiceImpl.java index 87e307d3..ed1cd676 100644 --- a/server-core/src/main/java/com/juick/service/CrosspostServiceImpl.java +++ b/server-core/src/main/java/com/juick/service/CrosspostServiceImpl.java @@ -2,9 +2,11 @@ package com.juick.service; import com.juick.server.helpers.ApplicationStatus; import org.apache.commons.lang3.tuple.Pair; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; +import javax.inject.Inject; import java.util.List; import java.util.Optional; @@ -15,6 +17,11 @@ import java.util.Optional; @Transactional(readOnly = true) public class CrosspostServiceImpl extends BaseJdbcService implements CrosspostService { + @Inject + public CrosspostServiceImpl(JdbcTemplate jdbcTemplate) { + super(jdbcTemplate, null); + } + @Override public Optional<Pair<String, String>> getTwitterTokens(final int uid) { List<Optional<Pair<String, String>>> list = getJdbcTemplate().query( diff --git a/server-core/src/main/java/com/juick/service/MessagesServiceImpl.java b/server-core/src/main/java/com/juick/service/MessagesServiceImpl.java index 924f9e03..dfbc4ade 100644 --- a/server-core/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/server-core/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -15,6 +15,7 @@ import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.Assert; import org.springframework.util.StringUtils; import javax.inject.Inject; @@ -28,8 +29,15 @@ import java.util.List; */ @Repository public class MessagesServiceImpl extends BaseJdbcService implements MessagesService { + private final UserService userService; + @Inject - private UserService userService; + public MessagesServiceImpl(JdbcTemplate jdbcTemplate, UserService userService) { + super(jdbcTemplate, null); + + Assert.notNull(userService); + this.userService = userService; + } private class MessageMapper implements RowMapper<Message> { @Override diff --git a/server-core/src/main/java/com/juick/service/PMQueriesServiceImpl.java b/server-core/src/main/java/com/juick/service/PMQueriesServiceImpl.java index 30231f98..34a07682 100644 --- a/server-core/src/main/java/com/juick/service/PMQueriesServiceImpl.java +++ b/server-core/src/main/java/com/juick/service/PMQueriesServiceImpl.java @@ -2,9 +2,11 @@ package com.juick.service; import com.juick.User; import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; +import javax.inject.Inject; import java.util.List; /** @@ -13,6 +15,11 @@ import java.util.List; @Repository public class PMQueriesServiceImpl extends BaseJdbcService implements PMQueriesService { + @Inject + public PMQueriesServiceImpl(JdbcTemplate jdbcTemplate) { + super(jdbcTemplate, null); + } + @Transactional @Override public boolean createPM(final int uid_from, final int uid_to, final String body) { diff --git a/server-core/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java b/server-core/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java index b83f61c1..18c789ec 100644 --- a/server-core/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java +++ b/server-core/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java @@ -2,9 +2,12 @@ package com.juick.service; import com.juick.Tag; import com.juick.User; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; +import javax.inject.Inject; + /** * Created by aalexeev on 11/13/16. */ @@ -12,6 +15,11 @@ import org.springframework.transaction.annotation.Transactional; @Transactional public class PrivacyQueriesServiceImpl extends BaseJdbcService implements PrivacyQueriesService { + @Inject + public PrivacyQueriesServiceImpl(JdbcTemplate jdbcTemplate) { + super(jdbcTemplate, null); + } + @Override public PrivacyResult blacklistUser(final User user, final User target) { int result = getJdbcTemplate().update( diff --git a/server-core/src/main/java/com/juick/service/PushQueriesServiceImpl.java b/server-core/src/main/java/com/juick/service/PushQueriesServiceImpl.java index 7c3f9d5b..91861bd9 100644 --- a/server-core/src/main/java/com/juick/service/PushQueriesServiceImpl.java +++ b/server-core/src/main/java/com/juick/service/PushQueriesServiceImpl.java @@ -1,9 +1,11 @@ package com.juick.service; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; +import javax.inject.Inject; import java.util.List; /** @@ -13,6 +15,11 @@ import java.util.List; @Transactional(readOnly = true) public class PushQueriesServiceImpl extends BaseJdbcService implements PushQueriesService { + @Inject + public PushQueriesServiceImpl(JdbcTemplate jdbcTemplate) { + super(jdbcTemplate, null); + } + @Override public List<String> getAndroidRegID(final int uid) { return getJdbcTemplate().queryForList( diff --git a/server-core/src/main/java/com/juick/service/ShowQueriesServiceImpl.java b/server-core/src/main/java/com/juick/service/ShowQueriesServiceImpl.java index 4f8d8ff2..a6504274 100644 --- a/server-core/src/main/java/com/juick/service/ShowQueriesServiceImpl.java +++ b/server-core/src/main/java/com/juick/service/ShowQueriesServiceImpl.java @@ -5,6 +5,7 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; +import javax.inject.Inject; import java.util.List; /** @@ -14,6 +15,11 @@ import java.util.List; @Transactional(readOnly = true) public class ShowQueriesServiceImpl extends BaseJdbcService implements ShowQueriesService { + @Inject + public ShowQueriesServiceImpl(JdbcTemplate jdbcTemplate) { + super(jdbcTemplate, null); + } + @Override public List<String> getRecommendedUsers(final User forUser) { return getJdbcTemplate().queryForList( diff --git a/server-core/src/main/java/com/juick/service/SubscriptionServiceImpl.java b/server-core/src/main/java/com/juick/service/SubscriptionServiceImpl.java index 57d52cbf..2bf05796 100644 --- a/server-core/src/main/java/com/juick/service/SubscriptionServiceImpl.java +++ b/server-core/src/main/java/com/juick/service/SubscriptionServiceImpl.java @@ -4,8 +4,10 @@ import com.juick.Tag; import com.juick.User; import com.juick.server.helpers.NotifyOpts; import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.Assert; import org.springframework.util.StringUtils; import javax.inject.Inject; @@ -17,10 +19,19 @@ import java.util.stream.Collectors; */ @Repository public class SubscriptionServiceImpl extends BaseJdbcService implements SubscriptionService { + private final UserService userService; + private final MessagesService messagesService; + @Inject - UserService userService; - @Inject - MessagesService messagesService; + public SubscriptionServiceImpl(JdbcTemplate jdbcTemplate, UserService userService, MessagesService messagesService) { + super(jdbcTemplate, null); + + Assert.notNull(userService); + this.userService = userService; + + Assert.notNull(messagesService); + this.messagesService = messagesService; + } @Transactional(readOnly = true) @Override diff --git a/server-core/src/main/java/com/juick/service/TagServiceImpl.java b/server-core/src/main/java/com/juick/service/TagServiceImpl.java index 3aed4b9b..d63159cf 100644 --- a/server-core/src/main/java/com/juick/service/TagServiceImpl.java +++ b/server-core/src/main/java/com/juick/service/TagServiceImpl.java @@ -3,10 +3,12 @@ package com.juick.service; import com.juick.Tag; import org.apache.commons.lang3.StringEscapeUtils; import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.Assert; import javax.inject.Inject; import java.sql.PreparedStatement; @@ -27,8 +29,14 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { private static final Pattern TAGS_PATTERN2 = Pattern.compile("^\\*([^ \\r\\n\\t]+)\\s+([\\s\\S]+)"); private static final Pattern TAG_PATTERN = Pattern.compile("\\*([^ \\r\\n\\t]+)"); + private final MessagesService messagesService; + @Inject - MessagesService messagesService; + public TagServiceImpl(JdbcTemplate jdbcTemplate, MessagesService messagesService) { + super(jdbcTemplate, null); + Assert.notNull(messagesService); + this.messagesService = messagesService; + } @Transactional(readOnly = true) @Override diff --git a/server-core/src/main/java/com/juick/service/UserServiceImpl.java b/server-core/src/main/java/com/juick/service/UserServiceImpl.java index 2740e52c..b681e3c8 100644 --- a/server-core/src/main/java/com/juick/service/UserServiceImpl.java +++ b/server-core/src/main/java/com/juick/service/UserServiceImpl.java @@ -7,6 +7,7 @@ import com.juick.server.helpers.UserInfo; import com.juick.util.UserUtils; import org.springframework.dao.DuplicateKeyException; import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder; @@ -14,6 +15,7 @@ import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; +import javax.inject.Inject; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -37,6 +39,11 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { } } + @Inject + public UserServiceImpl(JdbcTemplate jdbcTemplate) { + super(jdbcTemplate, null); + } + @Transactional @Override public String getSignUpHashByJID(final String jid) { diff --git a/server-core/src/main/java/com/juick/util/ThreadHelper.java b/server-core/src/main/java/com/juick/util/ThreadHelper.java index 7304d158..64a56f7c 100644 --- a/server-core/src/main/java/com/juick/util/ThreadHelper.java +++ b/server-core/src/main/java/com/juick/util/ThreadHelper.java @@ -21,6 +21,7 @@ public class ThreadHelper { try { // Wait a while for existing tasks to terminate if (!pool.awaitTermination(5, TimeUnit.SECONDS)) { + logger.info("Calls shutDownNow for pool"); pool.shutdownNow(); // Cancel currently executing tasks // Wait a while for tasks to respond to being cancelled if (!pool.awaitTermination(5, TimeUnit.SECONDS)) |