spring-boot 几个工具类(七)
环境
- jdk 6
- tomcat 6.0.53
- sts 4.4.2
- maven 3.2.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;
}
}
分类:
Mysql
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!