优化代码。
parent
68f6ce5b8f
commit
1c02b37442
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue