优化代码。

dev
ZhouXY108 2023-07-16 03:28:17 +08:00
parent 68f6ce5b8f
commit 1c02b37442
2 changed files with 9 additions and 9 deletions

View File

@ -16,17 +16,19 @@ import xyz.zhouxy.plusone.spring.SpringContextHolder;
*/ */
public final class JdbcFactory { 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() { private JdbcFactory() {
throw new IllegalStateException("Utility class"); throw new IllegalStateException("Utility class");
} }
public static JdbcTemplate getJdbcTemplate() { public static JdbcTemplate getJdbcTemplate() {
return CONTEXT.getBean(JdbcTemplate.class); return JDBC_TEMPLATE;
} }
public static NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() { public static NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
return CONTEXT.getBean(NamedParameterJdbcTemplate.class); return NAMED_PARAMETER_JDBC_TEMPLATE;
} }
} }

View File

@ -4,15 +4,13 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
public class SpringContextHolder { public enum SpringContextHolder {
INSTANCE
;
private ApplicationContext context; private ApplicationContext context;
private static final SpringContextHolder INSTANCE = new SpringContextHolder();
private SpringContextHolder() {
}
public static ApplicationContext getContext() { public static ApplicationContext getContext() {
return INSTANCE.context; return INSTANCE.context;
} }