1.不使用占位符,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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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">
<bean id="userDao" class="dao.impl.UserDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userInterceptor" class="aop.UserInterceptor"></bean>
<bean id="userService" class="service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring"/>
<property name="username" value="root"/>
<property name="password" value="****"/>
</bean>
<aop:config>
<aop:aspect id="userInterceptor" ref="userInterceptor">
<aop:pointcut expression="execution(public * dao..*.*(..))" id="myMethod"/>
<!-- aop pointcut也可以声明在config内,aspect外,这样可以让别的aspect使用 -->
<aop:before method="before" pointcut-ref="myMethod"/>
<!-- 也可以直接写<aop:before method="before" pointcut="execution(public * dao..*.*(..))"/> -->
<aop:after method="after" pointcut-ref="myMethod"/>
</aop:aspect>
</aop:config>
</beans>
2.测试时没有问题,而使用占位符,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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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">
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="userDao" class="dao.impl.UserDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userInterceptor" class="aop.UserInterceptor"></bean>
<bean id="userService" class="service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
3.在src目录下新建jdbc.properties,内容如下:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=****
4.运行时报如下异常:
org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver '
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
请各位看官解析一下,不胜感激!
问题已解决:将<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
改为<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 即可。
Thank you all all the same!
写的很明白了,这个class找不到,看下mysql的jar包在不在,不在的话加上去。
引用来自“qjf_Troy”的评论
你这个是数据库连接异常了,检查下mysql驱动包<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>