Spring,启动程序后顺序执行指定操作
2016-08-08 17:32
393 查看
启动执行类接口:
启动执行类注解:
启动类处理:
测试例子:
第一个
第二个
package test.core.startup; public interface RunAfterStartToDo { Object startup(); }
启动执行类注解:
package test.core.startup; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface RunAfterStart { public int priority() default 7; }
启动类处理:
package test.core.startup; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; import test.core.spring.ApplicationContextFactory; @Component public class RunAfterStartProcessor implements ApplicationListener<ContextRefreshedEvent> { static { System.out.println("auto loaded"); } @Override public void onApplicationEvent(ContextRefreshedEvent event) { if (event.getApplicationContext().getParent() == null) { ApplicationContext context = ApplicationContextFactory .getApplicationContext(); Map<String, Object> classMap = context .getBeansWithAnnotation(RunAfterStart.class); TreeMap<Integer, List<RunAfterStartToDo>> map = new TreeMap<Integer, List<RunAfterStartToDo>>(); for (Entry<String, Object> entry : classMap.entrySet()) { if (entry.getValue() != null) { try { RunAfterStartToDo startToDo = (RunAfterStartToDo) entry .getValue(); RunAfterStart runAfterStart = startToDo.getClass() .getAnnotation(RunAfterStart.class); List<RunAfterStartToDo> list = map.get(runAfterStart .priority()); if (list == null) { list = new ArrayList<RunAfterStartToDo>(); map.put(runAfterStart.priority(), list); } list.add(startToDo); } catch (Exception e) { continue; } } } for (Entry<Integer, List<RunAfterStartToDo>> entry : map.entrySet()) { System.out.println("Start to do priority " + entry.getKey()); for (RunAfterStartToDo startToDo : entry.getValue()) { startToDo.startup(); } } } } }
测试例子:
第一个
package test; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.stereotype.Component; import test.core.startup.RunAfterStart; import test.core.startup.RunAfterStartToDo; @RunAfterStart @Component public class DoLoop implements RunAfterStartToDo { private static final Log log = LogFactory.getLog("SS"); @Override public Object startup() { for (int i = 0; i < 10; i++) { log.info("hello loop at " + i); try { Thread.sleep(200l); } catch (InterruptedException e) { e.printStackTrace(); break; } } return null; } }
第二个
package test; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.stereotype.Component; import test.core.startup.RunAfterStart; import test.core.startup.RunAfterStartToDo; @RunAfterStart(priority = 5) @Component public class PrintMessage implements RunAfterStartToDo { private static final Log log = LogFactory.getLog("TT"); @Override public Object startup() { log.info("TT is in."); return null; } }
相关文章推荐
- Spring,启动程序后顺序执行指定操作
- 该操作未能执行,因为 OLE DB 提供程序 'SQLOLEDB' 无法启动分布式事务。新事务不能登记到指定的事务处理器中。
- 该操作未能执行,因为 OLE DB 提供程序SQLOLEDB无法启动分布式事务
- MFC程序的启动过程与相关函数执行顺序
- Spring-Spring容器启动各项操作窥探(AbstractApplicationContext#refesh()执行逻辑分析)
- 使用spring等框架的web程序在Tomcat下的启动顺序及思路理清
- MFC程序的启动过程与相关函数执行顺序
- MFC程序的启动过程与相关函数执行顺序
- >02 (协议代理)、自定义视图、程序执行顺序(iOS程序启动流程 )、视图控制器、(MVC的理解)
- ios 程序启动执行顺序
- iOS程序的启动执行顺序 AppDelegate 及 UIViewController 的生命周期
- 该操作未能执行,因为 OLE DB 提供程序SQLOLEDB无法启动分布式事务(转)
- 〖Linux〗以后台方式启动/结束指定程序/命令(不受 exit 或点击窗口关闭按钮等终端退出操作的影响)
- 该操作未能执行,因为 OLE DB 提供程序SQLOLEDB无法启动分布式事务
- vs执行程序时提示无法启动程序,系统找不到指定的文件
- linux操作之:设置系统启动时,自动启动一些程序,或者执行一些命令
- MFC程序的启动过程与相关函数执行顺序
- MFC程序的启动过程与相关函数的执行顺序
- MFC程序的启动过程与相关函数执行顺序
- 使用spring等框架的web程序在Tomcat下的启动顺序及思路理清