常用Spring配置项

在做Spring相关开发时,时常要用到一些相关的Bean的声明,如数据库连接池,hibernateSessionFactory声明等。一下是一些常用到的Bean声明。



1 Message source的声明,重要用于系统的信息提示。

 

< bean  id ="messageSource"  class ="org.springframework.context.support.ResourceBundleMessageSource" >  

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

</ bean >

 

2 属性值的声明,主要为Bean声明文件中使用:

 

< bean  id ="propertyConfigurer"  class ="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >  

< property  name ="locations" >  

< list >  

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

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

</ list >  

</ property >  

</ bean >

 

3 Custom Editor的注册,以下是日期的注册:

 

< bean  id ="customEditorConfigurer"  class ="org.springframework.beans.factory.config.CustomEditorConfigurer" >  

< property  name ="customEditors" >  

< map >  

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

< bean  class ="org.springframework.beans.propertyeditors.CustomDateEditor" >  

< constructor-arg  index ="0" >  

< bean  class ="java.text.SimpleDateFormat" >  

< 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"  class ="org.apache.commons.dbcp.BasicDataSource"  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"  class ="org.springframework.orm.hibernate.LocalSessionFactoryBean" >  

< 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"  class ="org.springframework.transaction.jta.JotmFactoryBean" />  

< bean  id ="transactionManager"  class ="org.springframework.transaction.jta.JtaTransactionManager" >  

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

</ bean >

 

7 Hibernate的事务设置:

 

< bean  id ="hibernateTransactionManager"  class ="org.springframework.orm.hibernate.HibernateTransactionManager" >  

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

</ bean >

 

8 Bean的事务声明:

 

< bean  id ="clinic"  class ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >  

< 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"  class ="org.springframework.mail.javamail.JavaMailSenderImpl" >  

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

</ bean >

 

10 基本Url Mapping的设置:

 

< bean  id ="DemoController"  class ="cn.edu.bit82.DemoController" />  

< bean  id ="urlMapping"  class ="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >  

< property  name ="mappings" >  

< props >  

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

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

</ props >  

</ property >  

</ bean >

 

总结: 以上是一些常用的 Bean 的声明,你一般会用到的,你可以使用 IntelliJ Live Template 功能,可以设置某些参数,很快就完成了 Bean 的声明。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值