问题描述
通过spring配置文件对dataSource属性进行注入时报以下错误:
Exception in thread "main" org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'dbSources': Bean with name 'dbSources' has been injected into other beans [txManager] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
解决思路
该问题的出现是因为对象循环引用形成闭环而造成的异常。此处是因为某一个属性在xml配置文件中被多次注入,应该修改代码避免这种状况
解决方案
此案例被多次注入属性名为dbSources,那么在使用dbSources的配置语句中我是
这样写的:
<bean name="dbSources" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/shop"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<aop:config proxy-target-class="true">
<aop:pointcut id="Transactional" expression="execution(* *.*(..))"/>
<aop:advisor advice-ref="tActive" pointcut-ref="Transactional"/>
</aop:config>
此处的Transactional中有dbSources属性,由于expression="execution(* .(…))的存在,会使我项目下所有包的中的java文件中dbSources都会被注释,报错xml文件。
将
<aop:pointcut id="Transactional" expression="execution(* *.*(..))"/>
修改为
<aop:pointcut id="Transactional" expression="execution(* com.cyw.dao.daoImpl.UserDapImplXml.*(..))"/>
也就是将execition中的位置修改成具体的类再指定方法