一直報(bào)錯(cuò)找不到怎么解決的方法 跟著老師寫的,但是就是不行
報(bào)錯(cuò)內(nèi)容:Error creating bean with name 'account.aop.test.AccountService2Test': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'accountService2Proxy' is expected to be of type 'account.aop.service.AccountService2Impl' but was actually of type 'com.sun.proxy.$Proxy30'
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'accountService2Proxy' is expected to be of type 'account.aop.service.AccountService2Impl' but was actually of type 'com.sun.proxy.$Proxy30'
xml代碼如下:
<!-- 注入dao -->
?? ?<bean id="accountDao2" class="account.aop.dao.AccountDao2Impl">
?? ??? ?<property name="dataSource" ref="dataSource"/>
?? ?</bean>
<!-- 注入service -->
?? ?<bean id="accountService2" class="account.aop.service.AccountService2Impl">
?? ??? ?<property name="accountDao2" ref="accountDao2"/>
?? ?</bean>
?? ?
?? ?
<!-- 通過context標(biāo)簽引入外部文件 -->
?? ?<context:property-placeholder location="classpath:jdbc.properties"/>
??? ?
<!-- 配置是C30連接池-->
?? ?<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
??? ??? ?<property name="driverClass" value="${driverClassName}"/>
?? ??? ?<property name="jdbcUrl" value="${url}"/>
?? ??? ?<property name="user" value="${user}"/>
?? ??? ?<property name="password" value="${pass}"/>
??? ?</bean>
??? ?
??? ?
<!-- 配置平臺事務(wù)管理器============================= -->
?? ?<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
?? ??? ?<property ref="dataSource" name="dataSource"/>
?? ?</bean>
??? ?
??? ?
?<!-- 編寫代理類,通過aop的前置增強(qiáng)的方法實(shí)現(xiàn)事物轉(zhuǎn)賬 -->
? ??? ?<bean id="accountService2Proxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
??? ??? ?<!-- 注入事物管理器 -->
??? ??? ?<property name="transactionManager" ref="transactionManager"/>
??? ??? ?<!-- 配置目標(biāo)對象 -->
??? ??? ?<property name="target" ref="accountService2"/>
??? ??? ?<!-- 注入事物屬性 -->
??? ??? ?<property name="transactionAttributes">
??? ??? ??? ?<props>
??? ??? ??? ??? ?<prop key="transfer">PROPAGATION_REQUIRED</prop>
??? ??? ??? ?</props>
??? ??? ?</property>
??? ?</bean>???
測試類:package account.aop.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import account.aop.service.AccountService2Impl;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:dl.xml")
public class AccountService2Test {
?? ?//@Resource(name="accountService2")
?? ?@Resource(name="accountService2Proxy")
?? ?private AccountService2Impl accountService2;
?? ?@Test
?? ?public void test() {
?? ??? ?accountService2.transfer("zuo", "you", 300);
?? ?}
}
2019-05-18
可能是Spring使用代理機(jī)制導(dǎo)致的(只看到錯(cuò)誤信息和xml文件,大概猜測的原因)
????使用JDK動態(tài)代理不支持類注入,只支持接口方式注入
????如果想類注入可以使用cglib代理
你xml配置文件中注入service使用的是實(shí)現(xiàn)類而不是接口:
應(yīng)該改為:
注入dao部分應(yīng)該也要類似修改。