From 1c02b374423eeca684334655c3dfaf0909f3c0bd Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 16 Jul 2023 03:28:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/xyz/zhouxy/plusone/jdbc/JdbcFactory.java | 8 +++++--- .../xyz/zhouxy/plusone/spring/SpringContextHolder.java | 10 ++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/JdbcFactory.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/JdbcFactory.java index aa27867..8a10056 100644 --- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/JdbcFactory.java +++ b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/JdbcFactory.java @@ -16,17 +16,19 @@ import xyz.zhouxy.plusone.spring.SpringContextHolder; */ public final class JdbcFactory { - private static final ApplicationContext CONTEXT = SpringContextHolder.getContext(); + private static final ApplicationContext SPRING_APPLICATION_CONTEXT = SpringContextHolder.getContext(); + private static final JdbcTemplate JDBC_TEMPLATE = SPRING_APPLICATION_CONTEXT.getBean(JdbcTemplate.class); + private static final NamedParameterJdbcTemplate NAMED_PARAMETER_JDBC_TEMPLATE = SPRING_APPLICATION_CONTEXT.getBean(NamedParameterJdbcTemplate.class); private JdbcFactory() { throw new IllegalStateException("Utility class"); } public static JdbcTemplate getJdbcTemplate() { - return CONTEXT.getBean(JdbcTemplate.class); + return JDBC_TEMPLATE; } public static NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() { - return CONTEXT.getBean(NamedParameterJdbcTemplate.class); + return NAMED_PARAMETER_JDBC_TEMPLATE; } } diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/spring/SpringContextHolder.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/spring/SpringContextHolder.java index 089cee5..b6d4046 100644 --- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/spring/SpringContextHolder.java +++ b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/spring/SpringContextHolder.java @@ -4,15 +4,13 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -public class SpringContextHolder { +public enum SpringContextHolder { + + INSTANCE + ; private ApplicationContext context; - private static final SpringContextHolder INSTANCE = new SpringContextHolder(); - - private SpringContextHolder() { - } - public static ApplicationContext getContext() { return INSTANCE.context; }