在作Spring相关开发时,时常要用到一些相关的Bean的声明,如数据库链接池,hibernate的SessionFactory声明等。一下是一些经常使用到的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的声明,你通常会用到的,你可使用IntelliJ的Live Template功能,能够设置某些参数,很快就完成了Bean的声明。