spring配置详解

来源:互联网 发布:php excel 导出到桌面 编辑:程序博客网 时间:2024/05/22 12:55

*我目前使用的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






原创粉丝点击