有哪些方法 實現服務啓動以後,立刻執行相關操做?java
無,普通的java bean便可
例如:web
/*** * 執行完構造方法以後就會執行該方法 */ @PostConstruct public void init() { System.out.println("初始化字典"); refresh2(); }
類實例化以後spring
必須使用SpringMVC的註解@Configuration ,
實現org.springframework.context.ApplicationListener 的onApplicationEvent方法tomcat
例如:ide
/*** * Spring容器加載完成觸發,可用於初始化環境,準備測試數據、加載一些數據到內存 * @param contextRefreshedEvent */ @Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { configType=getProperty("ConfigType"); SpringMVCUtil.addCustomPropertySources(this.zookeeperSources, env); mkdirLogFolder(logFilePath); }
無,普通的java bean便可
例如:測試
/*** * 作一些初始化操做<br /> * 在服務啓動後立刻執行,並僅執行一次. */ public class ConfigInitSchedule { @Resource private DictionaryParam dictionaryParam; public void initDictionary() { System.out.println("refresh dictionary "); dictionaryParam.refresh2(); } }
web服務(tomcat 或jetty)啓動以後this
spring-quartz.xml的配置:code
<!-- 添加調度的任務bean 配置對應的class--> <bean id="myPrintSchedule" class="com.house.ujiayigou.config.ConfigInitSchedule"/> <!--配置調度具體執行的方法--> <bean id="myPrintDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="myPrintSchedule"/> <property name="targetMethod" value="initDictionary"/> <property name="concurrent" value="false"/> </bean> <!--配置調度執行的觸發的時間--> <bean id="myPrintTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> <property name="jobDetail" ref="myPrintDetail"/> <property name="repeatCount" value="0"/> </bean> <!-- quartz的調度工廠 調度工廠只能有一個,多個調度任務在list中添加 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <!-- 全部的調度列表--> <ref local="myPrintTrigger"/> </list> </property> </bean>