1. 程式人生 > >Spring容器初始化後執行某個指定的方法

Spring容器初始化後執行某個指定的方法

1.配置springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
	
    <!-- 註冊元件掃描器 -->
	<context:component-scan base-package="com.dataflow.controller" />
	<!-- <context:component-scan base-package="com.dataflow.dao" /> -->
	<context:component-scan base-package="com.dataflow.service.impl" />
	
	<!-- 可以用mvc的註解驅動 --> 
    <mvc:annotation-driven />	
    
	<!-- 配置檢視解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置檢視名的預設字首 -->
		<property name="prefix" value="/"></property>
		<!-- <property name="prefix" value="/WEB-INF/"></property> -->
		<!-- 配置檢視名的預設字尾 -->
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

2.配置spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:config/db.properties</value>
            </list>
        </property>
    </bean>

    <!-- 插入資料來源配置資訊 -->
	<import resource="spring_datasources.xml"/>
	
	<!-- 解除servlet對靜態資原始檔訪問的限制 -->  
	<mvc:default-servlet-handler/> 
 
	<!-- 啟動Spring MVC的註解功能,完成請求和註解POJO的對映 -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<!--json轉換器 -->
				<ref bean="mappingJacksonHttpMessageConverter" />
			</list>
		</property>	
	</bean>
	<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJacksonHttpMessageConverter" />
			</list>
		</property>
	</bean>

	<bean id="mappingJacksonHttpMessageConverter"
		class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<bean class="org.springframework.http.MediaType">
					<constructor-arg index="0" value="text" />
					<constructor-arg index="1" value="plain" />
					<constructor-arg index="2" value="UTF-8" />
				</bean>
				<bean class="org.springframework.http.MediaType">
					<constructor-arg index="0" value="*" />
					<constructor-arg index="1" value="*" />
					<constructor-arg index="2" value="UTF-8" />
				</bean>
				<bean class="org.springframework.http.MediaType">
					<constructor-arg index="0" value="text" />
					<constructor-arg index="1" value="*" />
					<constructor-arg index="2" value="UTF-8" />
				</bean>
				<bean class="org.springframework.http.MediaType">
					<constructor-arg index="0" value="application" />
					<constructor-arg index="1" value="json" />
					<constructor-arg index="2" value="UTF-8" />
				</bean>
			</list>
		</property>
	</bean>

	<!-- 儲存當前springContext物件 -->
	<!-- 
	<bean id="springContextUtil" class="com.dataflow.utils.SpringContextUtil" scope="true" />
	 -->
</beans>

3.新建一個controller:ApplicationController


import javax.servlet.ServletContext;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.ServletContextAware;
@Controller
public class ApplicationController  implements InitializingBean,ServletContextAware{

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("-> inited");
		// 獲取所有datasource_instances的資料並且建立執行緒池
		
	}
	
	/* Administrator
	 * @see org.springframework.web.context.ServletContextAware#setServletContext(javax.servlet.ServletContext)
	 * 可用獲得servletcontext
	 */
	@Override
	public void setServletContext(ServletContext servletContext) {
		// TODO Auto-generated method stub
//		ServletContext applicationContext = request.getSession().getServletContext();
	}
	
}