JAVA程序运行时就执行代码
2017-05-29 00:00
302 查看
我们在JAVA开发的过程中,往往需要在程序一启动时就执行一些操作,例如载入基本数据或初始化系统什么的。
这是用了监听器(Listener)实现的,参考书籍《轻量级Java EE》企业应用实战(第4版) 李刚编著
package com.cn.bwq.init;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class Startup implements ServletContextListener{
//@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("程序销毁了");
}
//@Override
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("程序启动了(首先执行)");
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
以下代码在程序启动时同样也会被执行,不过是先执行上面的,再执行下面的
package com.cn.bwq.init;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;
@Service
public class StartupBySpringMvc implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent evt) {
// TODO Auto-generated method stub
if(evt.getApplicationContext().getParent() != null){
System.out.println("系统启动了(然后执行)");
}
}
}
这是用了监听器(Listener)实现的,参考书籍《轻量级Java EE》企业应用实战(第4版) 李刚编著
package com.cn.bwq.init;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class Startup implements ServletContextListener{
//@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("程序销毁了");
}
//@Override
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("程序启动了(首先执行)");
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
以下代码在程序启动时同样也会被执行,不过是先执行上面的,再执行下面的
package com.cn.bwq.init;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;
@Service
public class StartupBySpringMvc implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent evt) {
// TODO Auto-generated method stub
if(evt.getApplicationContext().getParent() != null){
System.out.println("系统启动了(然后执行)");
}
}
}
相关文章推荐
- java执行命令或调用其他exe程序的代码范例
- 在java程序中执行dos命令或运行其他程序
- 正试图在 os 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码。
- 正试图在 OS 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起。
- 正试图在 os 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码
- 在自己的java代码中编译并运行程序
- 在JAVA程序中直接执行JAVASCRIPT代码(简单调用实例)
- Java精确测量代码运行时间 代码执行时间 纳秒 nanoTime
- 正试图在 os 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起
- BlackBerry手机上面开发Java程序,如果才能让代码运行效率高,占用系统资源少?
- 正试图在 os 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起。问题的解决方法!
- 检测到LoaderLock,正试图在OS加载程序锁内执行托管代码,不要尝试在DllMain或映像初始化函数内运行托管代码,这样会导致应用程序挂起。
- 正试图在 os 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起
- 关于计算Java程序运行时间 及Java代码--JUnit
- 在java程序中执行dos命令或运行其他程序
- 使用脚本引擎增加程序运行时动态执行能力(Java篇)
- 正试图在 OS 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起。
- JAVA 测试程序代码运行时间
- 正试图在 os 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起。
- 正试图在 os 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码...