`
空指针异常
  • 浏览: 21727 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

spring配置文件

阅读更多
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 一个错误说明:schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/aop/spring-aop-3.0.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>. 造成这个问题的原因是因为没有将aop的jar包导入工程中,所以报这个错,也说明spring会从本地去读取这个XSD-->
    
        <!-- 从属性配置文件中读取值,dataSource.properties文件位于src目录下-->
        <!-- 也可以写成下面这样
        <context:property-placeholder location="classpath:dataSource.properties"/> 
        -->
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location">
			<value>classpath:dataSource.properties</value>
		</property>
	</bean>
    
        <!-- 数据源配置 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${mysql.driverClassName}"></property>
		<property name="url" value="${mysql.url}"></property>
		<property name="username" value="${mysql.username}"></property>
		<property name="password" value="${mysql.password}"></property>
                <!-- 可以从对象池中取出的对象最大个数,为 0 则表示没有限制,默认为 8 -->
		<property name="maxActive" value="${mysql.maxActive}"></property>
                <!-- 最大等待连接中的数量 , 设 0 为没有限制 (对象池中对象最大个数) -->
		<property name="maxIdle" value="${mysql.maxIdle}"></property>
                <!-- 最大等待秒数 , 单位为 ms, 超过时间会丟出错误信息  -->
		<property name="maxWait" value="${mysql.maxWait}"></property>
	</bean>
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQL5Dialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">validate</prop>
			</props>
		</property>
                <!-- 自动搜索com.utstar.ivas.model包下所有的实体类(@Entity) -->
		<property name="packagesToScan">
			<list>
				<value>com.utstar.ivas.model</value>
			</list>
		</property>
                
                <!-- 和packagesToScan属性一样的功能,annotatedClasses需要将每个实体类都配置在文件中,比较麻烦 -->
		<!-- 
			<property name="annotatedClasses">
				<list>
					<value>com.utstar.jqueryportal.model.Privilege</value>
					<value>com.utstar.jqueryportal.model.RolePrivilege</value>
					<value>com.utstar.jqueryportal.model.UserRole</value>
					<value>com.utstar.jqueryportal.model.Role</value>
					<value>com.utstar.jqueryportal.model.User</value>
					<value>com.utstar.jqueryportal.model.SysUser</value>
				</list>
			</property>
			 -->
	</bean>
	
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="edit*" propagation="REQUIRED" />
			<tx:method name="modify*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="publish*" propagation="REQUIRED" />
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>
	
	<!-- 那些类的哪些方法参与事务 -->
	<aop:config>
                <!-- (* com.utstar.ivas.service.impl.*.*(..))的意思是任意修饰符 任意返回值 com.utstar.ivas.service.impl下的任何类的任意方法 任意参数,具体参考spring参考文档;如果(* com.utstar.ivas.service.impl..*.*(..))包后面有两个点,则代表包括任何子包 -->
		<aop:pointcut id="allServiceMethod" 
                 expression="execution(* com.utstar.ivas.service.impl.*.*(..))" />
		<aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice" />
	</aop:config>

	<!-- 使Spring关注Annotation -->
	<context:annotation-config />

	<!-- 让Spring通过自动扫描来查询和管理Bean,会自动激活上面的标签,因此上面的标签也可以不加 -->
	<context:component-scan base-package="com.utstar.ivas" />
</beans>


dataSource.properties:
mysql.driverClassName=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/ssh?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
mysql.username=root
mysql.password=root
mysql.maxActive=100
mysql.maxIdle=30
mysql.maxWait=500
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics