报错信息一:
时间:学习spring的aop容器:xml形式配置aop功能
起因:测试xml形式配置好的aop功能
经过:在运行junit测试时出现以下报错信息:
警告:
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'calculator' defined in class path resource
[applicationContext.xml]:
BeanPostProcessor before instantiation of bean failed;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0':
Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.aop.aspectj.AspectJPointcutAdvisor]:
Constructor threw exception;
nested exception is java.lang.IllegalArgumentException:
warning no match for this type name: com.lixxkvSpringSeven.pojo.Calculator
[Xlint:invalidAbsoluteTypeName]
严重:
Caught exception while allowing TestExecutionListener
[org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1794d431]
to prepare test instance
[com.lixxkvSpringEight.test.springTestTwo@70325e14]
java.lang.IllegalStateException: Failed to load ApplicationContext
解决:
根据报错信息进行排查,可知在配置文件applicationContext.xml中存在以下异常情况:在配置目标对象时,配置情况如下:
<bean class="com.lixxkvSpringEight.pojo.Calculator" id="calculator"/>
在通知的切入表达式中,配置的情况如下(以前置通知为例):
<aop:before
method="logBefore"
pointcut="execution(public int com.lixxkvSpringSeven.pojo.Calculator.*(int,int))"
/>
目标对象所属包名与切入表达式中目标对象所属包名不一致。
解决结果:
将包名称统一,此bug得到解决。
报错信息二
时间:学习spring使用xml方式配置声明式事务
起因:进行spring使用xml方式配置声明式事务测试
经过:在运行junit测试时出现以下报错信息:
警告:
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'transactionManager' defined in class path resource [applicationContext.xml]:
Initialization of bean failed;
nested exception is org.springframework.beans.ConversionNotSupportedException:
Failed to convert property value of type 'java.lang.String' to required type 'javax.sql.DataSource' for property 'dataSource';
nested exception is java.lang.IllegalStateException:
Cannot convert value of type 'java.lang.String' to required type 'javax.sql.DataSource' for property 'dataSource':
no matching editors or conversion strategy found
严重:
Caught exception while allowing TestExecutionListener
[org.springframework.test.context.support.DependencyInjectionTestExecutionListener@31a5c39e]
to prepare test instance
[com.lixxkvSpringTen.test.TransactionTestOne@fe34b86]
java.lang.IllegalStateException: Failed to load ApplicationContext
解决:
根据报错信息进行排查,经过与学习资料综合分析,发现在spring的配置文件applicationContext.xml中:
存在已经引入数据源
<!--
加载jdbc.properties属性配置文件
-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--
配置数据库连接池
-->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
<property name="url" value="${url}"/>
<property name="driverClassName" value="${driver}"/>
<property name="initialSize" value="${initialSize}"/>
<property name="maxActive" value="${maxActive}"/>
</bean>
而在进行事务管理器配置时配置语句如下
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" value="dataSource"/>
</bean>
在property标签中引用的dataSource是以value标签引用,导致此处的数据源与整个spring中使用的数据源不一致,导致项目中无法加载applicationContext.xml文件。
解决结果:
在进行事务管理器配置时配置中进行标签修改
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
报错得以解决。
以上是本篇小节,不喜勿喷,感谢理解