Spring的幾個經常使用的Bean聲明

在作Spring相關開發時,時常要用到一些相關的Bean的聲明,如數據庫鏈接池,hibernateSessionFactory聲明等。一下是一些經常使用到的Bean聲明。html



1 Message source的聲明,重要用於系統的信息提示。 java

<bean id="messageSource"數據庫

<property name="basename"><value>messages</value></property>session

</bean>app

2 屬性值的聲明,主要爲Bean聲明文件中使用: url

<bean id="propertyConfigurer"spa

<property name="locations">hibernate

<list>xml

<value>WEB-INF/mail.properties</value>htm

<value>WEB-INF/jdbc.properties</value>

</list>

</property>

</bean>

3 Custom Editor的註冊,如下是日期的註冊:

<bean id="customEditorConfigurer"

<property name="customEditors">

<map>

<entry key="java.util.Date">

<bean

<constructor-arg index="0">

<bean

<constructor-arg><value>yyyy-MM-dd</value></constructor-arg>

</bean>

</constructor-arg>

<constructor-arg index="1"><value>false</value></constructor-arg>

</bean>

</entry>

</map>

</property>

</bean>

4 數據庫鏈接池的設置:

<bean id="dataSource"  destroy-method="close">

<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>

<property name="url"><value>${jdbc.url}</value></property>

<property name="username"><value>${jdbc.username}</value></property>

<property name="password"><value>${jdbc.password}</value></property>

</bean>

5 hibernate的設置:

<bean id="sessionFactory"

<property name="dataSource"><ref local="dataSource"/></property>

<property name="mappingResources">

<value>mapping.xml</value>

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">${hibernate.dialect}</prop>

</props>

</property>

</bean>

6 Jotm的事務設置:

<bean id="jotm"

<bean id="transactionManager"

<property name="userTransaction"><ref local="jotm"/></property>

</bean>

7 Hibernate的事務設置:

<bean id="hibernateTransactionManager"

<property name="sessionFactory"><ref local="sessionFactory"/></property>

</bean>

8 Bean的事務聲明:

<bean id="clinic"

<property name="transactionManager"><ref local="hibernateTransactionManager"/></property>

<property name="target"><ref local="clinicTarget"/></property>

<property name="transactionAttributes">

<props>

<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>

<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>

<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>

<prop key="store*">PROPAGATION_REQUIRED</prop>

</props>

</property>

</bean>

9 Email的發送者聲明:

<bean id="mailSender"

<property name="host"><value>${mail.host}</value></property>

</bean>

10 基本Url Mapping的設置:

<bean id="DemoController"

<bean id="urlMapping"

<property name="mappings">

<props>

<prop key="/hello.html">DemoController</prop>

<prop key="*">SecondController</prop>

</props>

</property>

</bean>

總結: 以上是一些經常使用的Bean的聲明,你通常會用到的,你可使用IntelliJLive Template功能,能夠設置某些參數,很快就完成了Bean的聲明。

相關文章
相關標籤/搜索