Spring配置
新建项目
目录
applicationContext.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:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 引入db.properties -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置C3P0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置 Spring 的 jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置DAO的类 -->
<bean id="accountDao" class="cn.mooc.trans.AccountDaoImpl"></bean>
<!-- 配置业务层类 -->
<bean id="accountService" class="cn.mooc.trans.AccountServiceImpl"></bean>
</beans>
完成过程中遇到的问题
1.Could not load driverClass "com.mysql.jdbc.Driver
缺少jar mysql-connector-java包
2.Caused by: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 75; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'context:property-placeholder'
- 确定引入文件头
xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation缺少相关解析文件
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
3.Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
导入的Junit.jar包版本太高的时候,需要同时导入hamcrest-core-1.3.jar