After the spring to open the transaction report but was actually of type 'com.sun.proxy. $ Proxy40 abnormal

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/xiaohe73/article/details/80033450

This problem occurs on my personal website projects, always looking for the reason, a lot of investigation possible, the problem is not found, and troubled me as much as half a year. Later, because can not solve, the problem will be shelved. Recently because the site will be done in preparation for the line, will study the issue again, plagued by the problem has finally been resolved six months.

The following (posted only focus on code and configuration):

Framework: Spring-springMVC-+ 4.3 + 4.3 + MyBatis shrio + Maven

JDK:1.8

Server : tomcat8

Database: MySql5.7

Test Controller:

@Controller
public class TestController {

    @Autowired
    private TestService testService;

    @RequestMapping("/tx/test1")
    @ResponseBody
    public String test(){
        return testService.test1();
    }
}

Test Service:

public class TestService {

    @Autowired
    private TestDao testDao;

    @Transactional(rollbackFor = Exception.class)
    public String test1(){
        return "hello tx!";
    }
}

spring-context.xml

<aop:aspectj-autoproxy proxy-target-class="true"/>

    <context:component-scan base-package="com.xiaohe66.web.**.service"/>
    <context:component-scan base-package="com.xiaohe66.web.**.controller"/>
    <context:component-scan base-package="com.xiaohe66.web.aop"/>
    <context:component-scan base-package="com.xiaohe66.web.spring"/>

    <!-- 开启注解 -->
    <context:annotation-config/>
    <!-- 导入配置 -->
    <context:property-placeholder location="classpath:*.properties"/>

    <bean id="springUtils" class="com.xiaohe66.web.common.util.SpringUtils"/>

    <import resource="spring-mybatis.xml"/>
    <import resource="spring-shrio.xml"/>

spring-mabatis.xml

<!-- 配置数据源 -->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${datasource.driverClass}"/>
        <property name="jdbcUrl" value="${datasource.url}"/>
        <property name="user" value="${datasource.user}"/>
        <property name="password" value="${datasource.password}"/>
    </bean>

    <!-- 开启注解事务 -->
    <!--<tx:annotation-driven proxy-target-class="true" mode="aspectj"/>-->
    <tx:annotation-driven proxy-target-class="true"/>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>


    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置MyBaties全局配置文件:mybaconfnfig.xml -->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
        <!--
        给包中的类注册别名,注册后可以直接使用类名
        属性可以配置多个,可以用,;\t\n进行分割。但是不支持Ant风格的路径。如:com.xiaohe66.web.**.po
         -->
        <property name="typeAliasesPackage" value="com.xiaohe66.web.comm.po
                                                    com.xiaohe66.web.org.po
                                                    com.xiaohe66.web.security.po
                                                    com.xiaohe66.web.sys.po
                                                    com.xiaohe66.web.text.po
                                                    com.xiaohe66.web.home.po" />
        <!-- 自动扫描mapper目录 -->
        <property name="mapperLocations" value="classpath:com/xiaohe66/web/mapper/**/*.xml"/>
    </bean>

    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg ref="sqlSessionFactory" />
    </bean>

    <!--自动扫描dao接口,并注入sqlsession-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xiaohe66.web.**.dao"/>
        <property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate"/>
    </bean>

spring-shrio.xml

<!--  自定义Realm -->
    <bean id="realmService" class="com.xiaohe66.web.security.RealmService"/>

    <!-- 安全管理器 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="realmService"/>
    </bean>

    <!-- 配置 Bean 后置处理器: 会自动的调用和 Spring 整合后各个组件的生命周期方法. -->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

    <!-- 开启shiro注解 -->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>

    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"/>
    </bean>

Error message (key part):

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController': 
Unsatisfied dependency expressed through field 'testService'; 
nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: 
Bean named 'testService' is expected to be of type 'com.xiaohe66.web.security.service.TestService' 
but was actually of type 'com.sun.proxy.$Proxy40'

---------------------------Dividing line--------------------- -------------------------------------

Depending on the error message, I looked up information on the parties, know that this problem is due to the dynamic proxy using the proxy method Standard jdk-based interface .

And my controller and service is not implement any interfaces, so I need to use agent-based way classes are.

Check their configuration:

1. Open a comment

<context:annotation-config/>

2. Turn on the aop, together with the proxy-target-class = "true "

<aop:aspectj-autoproxy proxy-target-class="true"/>

3. Turn on the transaction, together with the proxy-target-class = "true "

<tx:annotation-driven proxy-target-class="true"/>

4. Add the cglib packet (after the picture is packaged maven war package)



It was found that some configuration has the added plus package, Baidu Google search are spit. Leng Shimo resolved, and evil wrong, or there has been.

--------------------------------------Dividing line---------- ----------------------------------

About to despair, I began to think it will not be imported spring of "<import resource =" issues spring-mybatis.xml "/>" in this way.

So I will spirng-mybatis.xml configuration information directly into the inside of the spring-context.xml in the imported configuration and commented as follows

spring-context.xml:

<!--<import resource="spring-mybatis.xml"/>-->
    <!--<import resource="spring-shrio.xml"/>--> 

Then start


Wath? ? ? ? Started? ? ? ? ? ? ? ? ? ? ? Not being given? ? ? ? ? ? ?

Access it



Access to, and success of the project started. I am happy to jump up. But things are not quite that simple.

--------------------------Dividing line---------------------- ----------------

After the successful start of the project, test it, the success of the transaction is applied after an exception is thrown, the transaction is successful rollback. Specific code and the process will not say.

Then I again spring-shrio.xml configuration file, and put that in the spring-context.xml. start up

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController': 
Unsatisfied dependency expressed through field 'testService'; 
nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: 
Bean named 'testService' is expected to be of type 'com.xiaohe66.web.security.service.TestService' 
but was actually of type 'com.sun.proxy.$Proxy40'

Yes, I saw that part of the damn error. wath? What? Is not import configuration problem of such an approach?

Then I'll spring-mybatis.xml configuration restore, and delete spring-context.xml configuration. And commenting the configuration of the spring-shrio.xml.

start up.

The result is successfully launched. wath? Shrio configuration problem is caused? Find the beginning of the Internet: shiro lead to a transaction can not be opened.

Then read this article on https://zhuanlan.zhihu.com/p/29161098 know almost, which is the focus of a sentence:

bean been repeatedly proxy when, jdk agent based interface, so in the end this turned into a type of bean types of proxy agent interface

Then, the article also explains the cause of the problem:




Press article said, I removed the configuration of the bean shrio the project really be able to start successfully. I tried it, effective transaction management. At this point, the question that bothers me more than six months has finally been successfully resolved.

---------------------------------------------------------------------------------------

to sum up:

Cause of the problem: bean lead to multiple agents into a type of bean proxy agent interface type

Solution: delete the extra agents, leaving only a

Thoughts: a lot of things, that may not surface, to see the deeper reasons.


Reference: https: //zhuanlan.zhihu.com/p/29161098


Guess you like

Origin blog.csdn.net/xiaohe73/article/details/80033450