*我目前使用的spring配置方法,采用c3p0的方法

 1.导入各种包2.web.xml中配置spring监听和spring配置文件的位置<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!-- spring配置文件的位置 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:beans.xml,/WEB-INF/xfire-servlet.xml</param-value></context-param><!-- 配置spring监听 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>3.beans.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:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><!-- 配置数据源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass"> <value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value></property><property name="jdbcUrl"><value>jdbc:sqlserver://localhost:1433;databaseName=huyao_soccer</value></property><property name="user"><value>sa</value></property><property name="password"><value>8265758hy</value></property><property name="maxPoolSize"> <value>20</value></property><property name="minPoolSize"><value>2</value></property><property name="initialPoolSize"> <value>2</value></property><property name="maxIdleTime"> <value>2</value></property></bean><!-- 配置数据库会话工厂 --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource"><ref bean="dataSource"></ref></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop><prop key="show_sql">true</prop><prop key="hibernate.hbm2ddl.auto">update</prop></props></property><property name="mappingLocations"><list><value>classpath:/com/huyao/*/entity/*.hbm.xml</value></list></property></bean><!-- 配置事物管理器 --><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><!-- 配置事物通知 --><tx:advice id="transAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="*" propagation="REQUIRED"/></tx:attributes></tx:advice><!-- 配置切面 --><aop:config><aop:pointcut expression="execution(* com.huyao.biz.*Biz.*(..))" id="transPointCut"/><aop:advisor advice-ref="transAdvice" pointcut-ref="transPointCut"/></aop:config><import resource="springconfig/beans_base.xml"/><import resource="springconfig/beans_info.xml"/>

*.spring配置的三种方法

1、使用org.springframework.jdbc.datasource.DriverManagerDataSource
说明:DriverManagerDataSource建立连接是只要有连接就新建一个connection,根本没有连接池的作用。 (1).在applicationContext.xml中配置DataSource<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">       <property name="driverClassName" value="${jdbc.driver}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean>(2).配置PropertyConfiger配置driverClassName,username等的值<bean id="propertyConfiger" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:jdbc.properties"></property></bean>(3).配置sessionFactory<bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource"><ref local="dataSource"></ref></property><property name="lobHandler" ref="lobHandler"/><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop><prop key="hibernate.show_sql">true</prop></props></property>//配置多个mapping<property name="mappingResources"><list><value>classpath:/com/huyao/*/*/model/ *.hbm.xml</value></list></property></bean>(4).配置jdbc.properties的内容jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriverjdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=FaceLookjdbc.username=huyaojdbc.password=8265758hy(5).配置lobHandle<bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler"><property name="nativeJdbcExtractor" ref="nativeJdbcExtractor" /></bean><bean id="nativeJdbcExtractor"class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor" /><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <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> 2、使用org.apache.commons.dbcp.BasicDataSource
说明:这是一种推荐说明的数据源配置方式,它真正使用了连接池技术
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>oracle.jdbc.driver.OracleDriver</value> </property> <property name="url"> <value>jdbc:oracle:thin:@localhost:1521:orcl</value> </property> <property name="username"> <value>test</value> </property> <property name="password"> <value>test</value> </property> <property name="maxActive"> <value>255</value> </property> <property name="maxIdle"> <value>2</value> </property> <property name="maxWait"> <value>120000</value> </property> </bean>
3、使用org.springframework.jndi.JndiObjectFactoryBean
说明:JndiObjectFactoryBean 能够通过JNDI获取DataSource (1).Tomcat6.0的conf-->context.xml中配置数据源<Context><Resource name="jdbc/facelook" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000"username="huyao" password="123"driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"url="jdbc:sqlserver://localhost:1433;DatabaseName=FaceLook" /></Context>  (2).配置DataSource<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"><property name="jndiName" value="java:comp/env/jdbc/facelook"></property>        </bean>
总结:3种方式中的第一种没有使用连接池,故少在项目中用到,第三种方式需要在web server中配置数据源,不方便于部署,本人推荐使用每二种方式进行数据源的配置。

*配置事务的时候aop:pointcut expression="execution()"表达式详细说明

     举例说明: 任意公共方法的执行:execution(public * *(..)) 任何一个以“set”开始的方法的执行:execution(* set*(..)) AccountService 接口的任意方法的执行:execution(* com.xyz.service.AccountService.*(..)) 定义在service包里的任意方法的执行:execution(* com.xyz.service.*.*(..)) 定义在service包和所有子包里的任意类的任意方法的执行:execution(* com.xyz.service..*.*(..)) 定义在pointcutexp包和所有子包里的JoinPointObjP2类的任意方法的执行:execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))") 定义在huyao包下任意以biz结尾的类中的任意方法:execution(* com.szsx.huyao..*Biz.*(..))

*如何在控制台调用spring

     public static void main(String[] args) {ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");Printer p=(Printer)context.getBean("printer");p.print("321432432432432432432432432432243");}

*spring抛出异常session is closed

 org.springframework.orm.hibernate3.HibernateSystemException: Session is closed; nested exception is org.hibernate.SessionException: Session is closed通过Session session=super.getSession();的方法获取session后执行结束之后不会自动关闭连接,也就是说必须通过session.close()的方法关闭,如果对上述操作进行事物控制的时候,spring框架会为我们自动关闭session,此时session.close()会抛出异常可以通过super.releaseSession(session);来关闭session

spring配置详解相关推荐

  1. struts2+hibernate+spring配置详解

    #struts2+hibernate+spring配置详解 struts2+hibernate+spring配置详解 哎 ,当初一个人做好难,现在终于弄好了,希望自学这个的能少走些弯路. 以下是自己配 ...

  2. Spring配置详解,Spring配置元信息详解,Spring配置大全及源码分析

    文章目录 一.Spring都可以配置哪些元信息 二.Spring Bean 配置元信息 1.GenericBeanDefinition 2.RootBeanDefinition 3.Annotated ...

  3. spring配置详解-复杂类型注入

    复杂类型注入,刚才都是属性,这个叫注入方式,下面再来一个复杂类型注入,来看一下复杂类型注入行为,复杂类型注入呢,指的是,咱们刚才的注入类型,无非就是值或者对象,假设遇到数组,List,Map,Prop ...

  4. spring配置详解-属性注入(p名称空间SPEL表达式)

    所谓了解的话讲了一般是不会用的,看一下了解的注入方式,还有一种叫p名称空间注入方式,是Spring最近发出来的,因为Bean注入方式已经深入人心了,所以后面这两种注入方式,哪怕是好,也不愿意去用,因为 ...

  5. spring配置详解-属性注入(构造函数)

    再来看一下构造函数注入,构造函数注入呢,我还是在这个配置文件里演示,构造函数了,我分割一下,华丽的分割线下方,来演示一下构造函数注入,构造函数注入,现在不走set方法了,我是不是要走构造,那走构造的前 ...

  6. spring配置详解-属性注入(set方式)

    Spring当中属性注入,关键的部分了,Spring中的属性注入,这个属性注入其实也算配置,Spring中的属性注入,那Spring的属性注入呢,一共有三种方式,我今天感觉方式有点多,Spring注入 ...

  7. spring配置详解-模块化配置

    再介绍一个配置,再介绍一下Spring的分模块配置,当然这个配置很简单,我估计一句话就说完了,啥叫分模块配置,就是将来随着项目的开发,对象越来越多,把很多对象都放在一个文件里面,这个太多了,于是我们在 ...

  8. spring配置详解-初始化销毁方法

    关于生命周期属性,生命周期属性,啥意思呢,假设咱们的bean,生命周期属性指的是bean创建的时候,如果希望有一些初始化的工作,你可以指定一个bean的方法,为他的初始化方法,那这样的话Spring每 ...

  9. spring配置详解-scope属性

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://ww ...

最新文章

  1. 面试四连问:API 接口如何设计?安全如何保证?防重如何实现?签名如何实现?...
  2. 线程中start()与run()的区别
  3. XAMPP——Apache不能启动解决方案
  4. mysql 面试知识点笔记(七)RR如何避免幻读及非阻塞读、范式
  5. 在 .NET Core 中的并发编程
  6. P1989 无向图三元环计数 思维 + 建图
  7. catalog java,Java Connection getCatalog()方法与示例
  8. 2021百万年薪AI职位趋势:数据科学、Python、自动驾驶、AIOps你关注了么?
  9. vSphere Web Client使用指南之安装配置
  10. 等效低通信号=带通信号的复包络
  11. .net通用CMS快速开发框架——问题:Dapper通用的多表联合分页查询怎么破?
  12. 树莓派上使用 LCD1602 显示状态
  13. 织梦采集插件-简单好用织梦采集插件
  14. Brocade博科光纤交换机之 常用命令
  15. 前端手册-CSS3 属性手册
  16. A Survey on Conversational Recommender Systems
  17. matlab显示hsi,matlab实现RGB与HSI的相互转换
  18. 华清远见重庆中心——HTML和CSS基础阶段技术总汇
  19. 华为防火墙的策略路由
  20. 【历史】 apache catalina servlet tomcat 命名的由来

热门文章

  1. JS身份证号码校验,JS根据身份证号码获取出生年月日,JS根据出生年月日获取年龄,JS根据身份证号码获取性别
  2. error: cannot connect to daemon
  3. [经验分享] 覃超线上直播课-模拟面试
  4. QIIME2进阶二_元数据及数据导入QIIME2
  5. 深入理解JVM虚拟机13:JVM面试题,看这篇就足够了(87题详解)
  6. PXC 配置笔记-从MySQL直接转成PXC集群
  7. Ubuntu使用git更新本地代码到github
  8. 关于Protel 2004 绘制电路原理图——元件库的建立
  9. 二叉树非递归后序遍历的三种办法
  10. 机器学习、数据挖掘、数据分析岗面试总结