您的位置:首页 > 编程语言 > Java开发

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
相关文章推荐