Spring 開發中用到幾個類,監聽器總結

MethodInvokingFactoryBeanjava

經過MethodInvokingFactoryBean工廠Bean,能夠將指定方法返回值注入成爲目標Bean的屬性值,MethodInvokingFactoryBean用來得到指定方法的返回值,該方法能夠是靜態方法 ,也能夠是實例方法。 
得到的方法返回值既能夠被注入到指定Bean實例的指定屬性,也能夠直接定義成Bean實例。 
mysql

<bean id="sessionHolder" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" lazy-init="false">
		<property name="staticMethod" value="com.huge.base.data.SessionHolder.setSessionFactory"/>
		<property name="arguments" ref="applicationSessionFactory"/>
	</bean>


ContextLoaderListener:web

ContextLoaderListener監聽器的做用就是啓動Web容器時,自動裝配ApplicationContext的配置信息。由於它實現了ServletContextListener這個接口,在web.xml配置這個監聽器,啓動容器時,就會默認執行它實現的方法。spring

若是在web.xml中不寫任何參數配置信息,默認的路徑是/WEB-INF/applicationContext.xmlsql

若是是要自定義文件名能夠在web.xml里加入contextConfigLocation這個context參數:
數據庫

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:/applicationContext.xml</param-value>
  </context-param>
<context-param>


IntrospectorCleanupListenertomcat

spring中的提供了一個名爲org.springframework.web.util.IntrospectorCleanupListener的監聽器。它主要負責處理由JavaBeans  Introspector的使用而引發的緩衝泄露。spring中對它的描述以下:session

 
它是一個在web應用關閉的時候,清除JavaBeans Introspector的監聽器.在web.xml中註冊這個listener.能夠保證在web 應用關閉的時候釋放與掉這個web 應用相關的class loader 和由它管理的類。
 
若是你使用了JavaBeans Introspector來分析應用中的類,Introspector 緩衝中會保留這些類的引用.結果在你的應用關閉的時候,這些類以及web 應用相關的class loader沒有被垃圾回收.
 
不幸的是,清除Introspector的惟一方式是刷新整個緩衝.這是由於咱們無法判斷哪些是屬於你的應用的引用.因此刪除被緩衝的introspection會致使把這臺電腦上的全部應用的introspection都刪掉.
 
須要注意的是,spring 託管的bean不須要使用這個監聽器.由於spring它本身的introspection所使用的緩衝在分析完一個類以後會被立刻從javaBeans Introspector緩衝中清除掉.
 
應用程序中的類歷來不直接使用JavaBeans Introspector.因此他們通常不會致使內部查看資源泄露.可是一些類庫和框架每每會產生這個問題.例如:Struts 和Quartz.
 
單個的內部查看泄漏會致使整個的web應用的類加載器不能進行垃圾回收.在web應用關閉以後,你會看到此應用的全部靜態類資源(例如單例).這個錯誤固然不是由這個類自身引發的。app

 <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>


OpenSessionInViewFilter框架

OpenSessionInViewFilter是Spring提供的一個針對Hibernate的一個支持類,其主要意思是在發起一個頁面請求時打開Hibernate的Session,一直保持這個Session,直到這個請求結束,具體是經過一個Filter來實現的。 

因爲Hibernate引入了Lazy Load特性,使得脫離Hibernate的Session週期的對象若是再想經過getter方法取到其關聯對象的值,Hibernate會拋出一個LazyLoad的Exception。因此爲了解決這個問題,Spring引入了這個Filter,使得Hibernate

的Session的生命週期變長。 

  <filter>
    <filter-name>hibernateOpenSessionInViewFilter</filter-name>
    <filter-class>
        org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    </filter-class>
    <init-param>
      <param-name>singleSession</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>sessionFactoryBeanName</param-name>
      <param-value>applicationSessionFactory</param-value>
    </init-param>
  </filter>

http://www.iteye.com/topic/32001


PropertyPlaceholderConfigurer

1. PropertyPlaceholderConfigurer是個bean工廠後置處理器的實現,也就是 BeanFactoryPostProcessor接口的一個實現。PropertyPlaceholderConfigurer能夠將上下文(配置文 件)中的屬性值放在另外一個單獨的標準java Properties文件中去。在XML文件中用${key}替換指定的properties文件中的值。這樣的話,只須要對properties文件進 行修改,而不用對xml配置文件進行修改。

2.在Spring中,使用PropertyPlaceholderConfigurer能夠在XML配置文件中加入外部屬性文件,固然也能夠指定外部文件的編碼,如:

    jdbc.properties的內容爲:

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost/mysqldb?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=round;jdbc.username=root

jdbc.password=123456


3.那麼在spring配置文件中,咱們就能夠這樣寫:

<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<property name="ignoreResourceNotFound" value="true" />
		<property name="locations">
			<list>
				<value>classpath*:/application.properties</value>
				<value>classpath*:/configuration.properties</value>
				<value>classpath*:/log4j.properties</value>
			</list>
		</property>
	</bean>
	
        <!--系統數據源配置-->
	<bean id="applicationDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
		<property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxPoolSize" value="${maxPoolSize}" />
        <property name="maxIdleTime" value ="${maxIdleTime}" /> 
        <property name="maxStatements" value="${maxStatements}" />
        <property name="maxStatementsPerConnection" value="200" />              
        <property name="checkoutTimeout" value="20000"/>
	</bean>


4.這樣,一個簡單的數據源就設置完畢了。能夠看出:PropertyPlaceholderConfigurer起的做用就是將佔位符指向的數據庫配置信息放在bean中定義的工具。

5.查看源代碼,能夠發現,locations屬性定義在PropertyPlaceholderConfigurer的祖父類 PropertiesLoaderSupport中,而location只有 setter方法。相似於這樣的配置,在spring的源程序中很常見的。

PropertyPlaceholderConfigurer若是在指定的Properties文件中找不到你想使用的屬性,它還會在Java的System類屬性中查找。

咱們能夠經過System.setProperty(key, value)或者java中經過-Dnamevalue來給Spring配置文件傳遞參數。


WebAppRootListener

<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param>

."webapp.root"這個字符串能夠隨便寫任何字符串。若是不配置默認值是"webapp.root"。能夠用System.getProperty("webapp.root")來動態獲項目的運行路徑。
通常返回結果例如:/usr/local/tomcat6/webapps/項目名

Spring經過org.springframework.web.util.WebAppRootListener 這個監聽器來運行時的項目路徑。

可是若是在web.xml中已經配置了 org.springframework.web.util.Log4jConfigListener這個監聽器,

則不須要配置WebAppRootListener了。由於Log4jConfigListener已經包含了WebAppRootListener的功能。

<listener>
        <listener-class> 
            org.springframework.web.util.WebAppRootListener
        </listener-class>
    </listener>
<context-param>  
    <param-name>log4jConfigLocation</param-name>  
    <param-value>WEB-INF/conf/log4j.properties</param-value>  
</context-param>     
  
<context-param>  
    <param-name>log4jRefreshInterval</param-name>  
      <param-value>3000</param-value>  
 </context-param>  
  
<listener>  
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
 </listener>
相關文章
相關標籤/搜索