• spring-boot 几个工具类(七)


    环境

    1. jdk 6
    2. tomcat 6.0.53
    3. sts 4.4.2
    4. maven 3.2.5
    5. mysql 5.7

    SpringContextHolder

    SpringContextHolder 可以很方便地获取 spring 的环境信息。

    package jiangbo.demo.core;
    
    import javax.servlet.ServletContext;
    
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.core.env.Environment;
    import org.springframework.stereotype.Component;
    import org.springframework.web.context.WebApplicationContext;
    
    @Component
    public final class SpringContextHolder implements BeanFactoryPostProcessor, ApplicationContextAware {
    
        private static ApplicationContext context;
    
        private static ServletContext servletContext;
    
        private static Environment environment;
    
        private SpringContextHolder() {
        }
    
        public ApplicationContext getContext() {
    
            return context;
        }
    
        public static <T> T getBean(Class<T> requiredType) {
    
            return context.getBean(requiredType);
        }
    
        public static <T> T getBean(String name, Class<T> requiredType) {
    
            return context.getBean(name, requiredType);
        }
    
        public static Environment getEnviroment() {
    
            return environment;
        }
    
        public static ServletContext getServletContext() {
    
            return servletContext;
        }
    
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    
            // 实现BeanFactoryPostProcessor使其提前初始化,因为在其它bean初始化的时候,可能会使用SpringContextHolder
            LoggerFactory.getLogger(getClass()).info("Spring context holder initialized successful");
        }
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) {
    
            init(applicationContext);
        }
    
        private void init(ApplicationContext applicationContext) {
    
            context = applicationContext;
            environment = applicationContext.getEnvironment();
    
            if (applicationContext instanceof WebApplicationContext) {
    
                servletContext = ((WebApplicationContext) applicationContext).getServletContext();
            }
        }
    }
    

    AbstractJdbcDao

    AbstractJdbcDao 是 sql 操作时的支持类。

    package jiangbo.demo.core;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.jdbc.core.JdbcTemplate;
    
    public abstract class AbstractJdbcDao {
    
        protected final Logger logger = LoggerFactory.getLogger(getClass());
    
        protected final JdbcTemplate jdbcTemplate = SpringContextHolder.getBean(JdbcTemplate.class);
    }
    
    

    AbstractJdbcInsertDao

    AbstractJdbcInsertDao 在插入数据的时候比较有帮助。

    package jiangbo.demo.core;
    
    import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
    
    public abstract class AbstractJdbcInsertDao extends AbstractJdbcDao {
    
        private static final String SQL_TEMPLATE_FIND_ALL = "SELECT * FROM ";
    
        protected final String findAllSql;
    
        protected final SimpleJdbcInsert jdbcInsert;
    
        public AbstractJdbcInsertDao(String tableName) {
    
            jdbcInsert = new SimpleJdbcInsert(jdbcTemplate).withTableName(tableName);
            findAllSql = SQL_TEMPLATE_FIND_ALL + tableName;
        }
    }
    
    
  • 相关阅读:
    NOIp前做题记录
    长链剖分学习笔记
    Java可重入锁AQS 和 CAS原理
    Shiro企业级实战详解,统一的Session管理。
    Jdk动态代理
    NIO实现的客户端和服务端
    Java编写生成mybatis xml文件、Dao文件、实体类和DTO
    [FreeRTOS入门] 1.CubeMX中FreeRTOS配置参数及理解
    Linux系统手动安装Firefox浏览器
    各种版本的firefox 下载
  • 原文地址:https://www.cnblogs.com/jiangbo44/p/12002513.html
Copyright © 2020-2023  润新知