Spring 容器启动完成后,执行初始化加载工作


springmvc 容器启动后,要执行一些工作。
有5种方法:

1、指定spring 的init-method方法

<-- 
scope 的值一定要是 singleton
inti-method 是指要执行的方法 
  -->
<bean id="startRun" class="com.aop8.springmvc2.initload.StartRun" scope="singleton" init-method="test"   />
package com.aop8.springmvc2.initload;

public class StartRun {
	
    public void test(){
        System.out.println("开始执行 init-method() ");
    }
}

2、@PostConstruct 注解

注解方式: @PostConstruct

注意:
1,把 @PostConstruct 放到要执行实始化的方法上面。
2,Service(如AppDicService) 必须注入到 Spring容器中才行。

package com.aop8.springmvc2.initload;
import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.aop8.springmvc2.service.UserService;

@Component
public class AppDicService{	

	@Autowired
	private UserService  xxxService;
	
	/**
	 * Spring 启动完成后,会自动调用此方法
	 */	
	@PostConstruct
	public void initAppDicCache(){
		//执行加载操作,		
		// 比如  xxxService.getList()
		System.out.println("@PostConstruct");
		xxxService.print();
	}	
}

3、实现 initiailzingBean 接口

package com.aop8.springmvc2.initload;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

@Component
public class ArraignedLogService implements InitializingBean {

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("通过实现接口 initializingBean来执行的");

	}
}

4、实现 ServletContextAware 接口

package com.aop8.springmvc2.initload;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

@Component
public class SystemInitStart implements ServletContextAware {

	private ServletContext application;
	
	@Override
	public void setServletContext(ServletContext servletContext) {
		this.application = servletContext;  
		
		System.out.println(" ServletContextAware() -------------- ");
		String ctxPath = servletContext.getContextPath();
		application.setAttribute("ctxPath", ctxPath);
	}
}

说明:
@Service 也可以改成使用 @Component 等 spring 注解。
如果不想使用@Service 注解,也可以在 applicationContext.xml 容器中配置bean 。

5、使用 定时器 quartz

InitLoadDataQuartzJob 类省略。

下面直接看配置:

<?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" >
        
     <bean id="initLoadData" class="xx.xx.xx.InitLoadDataQuartzJob"/>
     
     <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
         <property name="targetObject"  ref="initLoadDataDetail" />
         <property name="targetMethod"  value="loadMethod" />
         <property name="concurrent" value="false"  />
     </bean>
 
 
 		<!-- 项目启动后任务就执行一次 -->
     <bean id="initLoadDataDetailTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
         <property name="jobDetail" ref="initLoadDataDetail"/>
         <property name="startDelay" value="500"/>
         <property name="repeatInterval" value="0"/>
         <property name="repeatCount" value="0"/>
         <!-- 
         <property name="cronExpression" value="0 0,30 8-17 * * ?"/>
          -->
         
     </bean>
    
     <bean id="startQuertz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
         <property name="triggers">
             <list>
                 <ref bean="initLoadDataDetailTrigger"/>
             </list>
         </property>
     </bean>
     
 </beans>

6、执行顺序

上面的例子,就是按照 执行顺序 列举出来的。

init-method > @PostConstruct > InitializingBean > ServletContextAware

猜你喜欢

转载自blog.csdn.net/xiaojin21cen/article/details/83418470