依赖Spring的情况下,Java Web项目如何在启动时加载数据库中的数据?
原文:https://blog.csdn.net/u012345283/article/details/39558537
原文:https://blog.csdn.net/wandrong/article/details/44782627
我的尝试:
1、继承HttpServlet,在web.xml中配置servlet的<load-on-startup>1</load-on-startup>,项目启动时globalParaService被注入了,但是跟踪到init方法里面,globalParaService为null。(失败)
public class InitSysServlet extends HttpServlet {
private SysGlobalParaServiceI globalParaService;
@Autowired
public void setGlobalParaService(SysGlobalParaServiceI globalParaService) {
this.globalParaService = globalParaService;
}
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
SysGlobalPara codeRule = globalParaService.getCodeRule();
if (null != codeRule) {
SystemConfig.IS_SET_WIZADED = "true".equals(codeRule.getSysValue());
} else {
SystemConfig.IS_SET_WIZADED = false;
}
}
}
然后,我试着手动创建globalParaService,代码调整为
public class InitSysServlet extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
SysGlobalParaServiceI globalParaService = new SysGlobalParaServiceImpl();
SysGlobalPara codeRule = globalParaService.getCodeRule();
if (null != codeRule) {
SystemConfig.IS_SET_WIZADED = "true".equals(codeRule.getSysValue());
} else {
SystemConfig.IS_SET_WIZADED = false;
}
}
}
然而,SysGlobalParaServiceImpl里面依赖的其他由Spring注入的对象为null。
搞不定了,百度到以上两篇文章,根据上面文章进行尝试都成功了。
2、直接在init方法中获取Spring的bean (成功)
首先在Spring配置文件中配置SysGlobalParaServiceImpl类对应的bean,然后在代码中获取,最终代码如下:
public class InitSysServlet extends HttpServlet {
private SysGlobalParaServiceI globalParaService;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
globalParaService = (SysGlobalParaServiceI) wac.getBean("gpservice");
SysGlobalPara codeRule = globalParaService.getCodeRule();
if (null != codeRule) {
SystemConfig.IS_SET_WIZADED = "true".equals(codeRule.getSysValue());
} else {
SystemConfig.IS_SET_WIZADED = false;
}
}
}
以下两种则与servlet无关,而是继承spring提供的接口。
3、实现Spring的接口InitializingBean,则实现类在被Sping初始化之后,会调用其中的afterPropertiesSet方法,代码如下:
@Component
public class InitSysServlet implements InitializingBean { private SysGlobalParaServiceI globalParaService; @Autowired
public void setGlobalParaService(SysGlobalParaServiceI globalParaService) {
this.globalParaService = globalParaService;
} @Override
public void afterPropertiesSet() throws Exception {
SysGlobalPara codeRule = globalParaService.getCodeRule();
if (null != codeRule) {
SystemConfig.IS_SET_WIZADED = "true".equals(codeRule.getSysValue());
} else {
SystemConfig.IS_SET_WIZADED = false;
}
} }
4、实现Spring的接口ApplicationListener<ContextRefreshedEvent>,在所有的bean初始化之后会调用其中的方法onApplicationEvent,代码如下:
@Component
public class InitSysServlet implements ApplicationListener<ContextRefreshedEvent> { private SysGlobalParaServiceI globalParaService; @Autowired
public void setGlobalParaService(SysGlobalParaServiceI globalParaService) {
this.globalParaService = globalParaService;
} @Override
public void onApplicationEvent(ContextRefreshedEvent arg0) {
SysGlobalPara codeRule = globalParaService.getCodeRule();
if (null != codeRule) {
SystemConfig.IS_SET_WIZADED = "true".equals(codeRule.getSysValue());
} else {
SystemConfig.IS_SET_WIZADED = false;
}
} }
最后,我采用的是第4中,实现Spring的接口ApplicationListener<ContextRefreshedEvent>。
依赖Spring的情况下,Java Web项目如何在启动时加载数据库中的数据?的更多相关文章
- EasyUI Datagrid 分页的情况下实现点击表头的小三角图标对数据库中所有数据重新排序
说明一下: 当点击 datagrid 表头某一列的小三角图标时,easyui 本身是有排序的,但是在当我们对 datagrid 进行了分页的情况下,点击排序只是对当前页的数据进行排序,而需求需要我对数 ...
- SQLite.dll混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。
其他信息: V5.7.4.4 Can't find the System.Data.SQLite.dll more info : 混合模式程序集是针对"v2.0.50727"版的运 ...
- VS报错之混合模式程序集是针对“v1.1.4322”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。
看到一个kinect大牛编写的一个水果忍者的体感游戏版本,让我为自己一直以来只用现有的网页游戏来模拟kinect体感游戏控制感到惭愧,没办法,我还是菜鸟.学习一段后自己模仿星际大战这个游戏,自己写了一 ...
- 【转】Sqlite 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该...
开发环境: vs2010+.net framework 4.0+ System.Data.SQLite.DLL (2.0)今天在做Sqlite数据库测试,一运行程序在一处方法调用时报出了一个异常 混合 ...
- <VS2010>混合模式程序集是针对“v2.0”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集
在把以前写的代码生成工具从原来的.NET3.5升级到.NET4.0时,将程序集都更新后,一运行程序在一处方法调用时报出了一个异常: 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有 ...
- C#连接Sqlite 出现:混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。的解决方案
C#连接Sqlite 出现: 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集.的解决方案 C#连接sqlite数据库代码 ...
- c# .netframwork 4.0 调用 2.0时报错 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。
“System.IO.FileLoadException”类型的未经处理的异常在 XXX.dll 中发生 其他信息: 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的 ...
- web.xml中配置启动时加载的servlet,load-on-starup
web.xml中配置启动时加载的servlet,load-on-starup 使用servlet来初始化配置文件数据: 在servlet的配置当中,<load-on-startup>1&l ...
- 使用Spring Boot来加速Java web项目的开发
我想,现在企业级的Java web项目应该或多或少都会使用到Spring框架的. 回首我们以前使用Spring框架的时候,我们需要首先在(如果你使用Maven的话)pom文件中增加对相关的的依赖(使用 ...
随机推荐
- Ubuntu 14中,Foxmail关联163邮箱账号时,总提示“密码错误”的解决方案
不知道在什么时候,网易邮箱搞了个“客户端授权密码”功能,也就是说,原先输入自己设置的邮箱密码即可完成登录,但是现在需要输入官方产生的“授权密码”,方可完成登录授权! 相关路径: 设置 -> PO ...
- 【spring boot】spring cloud下spring boot微服务启动没有报错,但是访问访问不到
spring cloud下spring boot微服务启动没有报错,但是访问访问不到 解决方法: 可能是端口被占用了,但是依旧启用成功了. 更改一下项目启用的端口号,再重新启动查看是否可以正常访问.
- 手把手教你调试Entity Framework 6源码
0 摘要 本文讲述在Visual Studio 2013(VS 2013)下调试Entity Framework 6(EF 6)源码的配置过程.原则上,VS 2012也适用. 之前打算编写<E ...
- 【angularJS】三个学习angulaJS的链接
1.官方文档:https://code.angularjs.org/1.5.7/docs/api 2.A Better Way to Learn AngularJS:https://thinkster ...
- Android Volley框架的使用(四)图片的三级缓存策略(内存LruCache+磁盘DiskLruCache+网络Volley)
在开发安卓应用中避免不了要使用到网络图片,获取网络图片很简单,但是需要付出一定的代价——流量.对于少数的图片而言问题不大,但如果手机应用中包含大量的图片,这势必会耗费用户的一定流量,如果我们不加以处理 ...
- oracle 数据库 基础操作
一.oracle基本常用的数据类型 varchar(长度) 字符串 char(长度) 字符 number(x,y) x表示总位数 y表示保留小数点后几位数 eg面试题:number(5,3)最大的数是 ...
- 挖一挖C#中那些我们不经常使用的东西之系列(1)——ToDictionary,ToLookup
这个系列我们看看C#中有哪些我们知道.可是又不知道怎么用.又或者懒得去了解的东西,比方这篇我们要介绍的toDictionary 和ToLookup. 从图中我们看到有四个ToXXX的方法,当中ToAr ...
- DB2解锁
1.登录数据库 db2 connect to 数据库名字 user 用户名 using 密码 2.进入db2top db2top -d 数据库名 进入到如下界面: 3.按下shift+u(图中U-L ...
- Tomcat 服务器只能存有一个正在运行的项目
即使新建了一个new project (在同一个工作空间),启动Tomcat 还是会出现先前(工程名)一样的问题/异常. [原因]: 在底下Server 那里——Tomcat 7.X 底下会有很多工程 ...
- silverlight RadGridView总结三(转载)
在RadGridView中进行分组以及导出 分组 主要是在前台进行分组的定义: 前台代码: <telerik:RadGridView x:Name="RadGridView1" ...