java.lang.IllegalArgumentException:无法设置为com.sun.proxy。$ Proxy

时间:2016-04-11 18:25:02

标签: java spring autowired illegalargumentexception transactional

我正在使用Spring MVC开发一个Web应用程序。

这是我得到的例外:

Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: java.lang.IllegalArgumentException: Can not set com.onclave.papercraft.DAO.tenantDAO.Implementation.UsersDAOImplementation field com.onclave.papercraft.DAO.tenantDAO.Controller.UsersDAOController.usersDAOImplementation to com.sun.proxy.$Proxy301

我有一个控制器: StudentController.java

@Controller
public class StudentsController {

    @Autowired
    private UsersDAOController usersDAOController;

    . . .

    private final String USER_TYPE = "student";

    @Transactional
    @RequestMapping(value = "/PAuth/user/add/user/student", method = RequestMethod.POST)
    public long addNewStudent(HttpServletRequest request) {

        long usersID = -1;

        . . .

        usersID = usersDAOController.storeNewUser(request, USER_TYPE);

        . . .
    }

    . . .
}

这是UsersDAOController.java

@Controller
@Configurable
public class UsersDAOController {

    @Autowired
    private UsersDAOImplementation usersDAOImplementation;

    public long storeNewUser(HttpServletRequest request, final String USER_TYPE) {

        Users user = new Users();

        . . .

        return usersDAOImplementation.createNewUser(user);
    }

    . . .
}

最后,这是UsersDAOImplementation.java

@Service
public class UsersDAOImplementation implements IUsersDAO {

    private JdbcTemplate jdbcTemplate = null;

    @Autowired
    public UsersDAOImplementation(DataSource dataSource) {
        this.jdbcTemplate = (new JdbcTemplate(dataSource));
    }

    . . .

    @Override
    public long createNewUser(final Users user) {

        KeyHolder keyHolder = new GeneratedKeyHolder();

        final long success = this.jdbcTemplate.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement preparedStatement = connection.prepareStatement(QueryStatements.INSERT_NEW_USER_SQL, Statement.RETURN_GENERATED_KEYS);

                . . .

                return preparedStatement;
            }
        }, keyHolder);

        if(success == 1) {
            return keyHolder.getKey().longValue();
        }

        return -1;
    }

    . . .
}

除了StudentController.java之外,这些类中的每一个都被定义为application-context.xml中的bean。

<?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:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:aop="http://www.springframework.org/schema/aop"    
    xmlns:task="http://www.springframework.org/schema/task" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.1.xsd    
                        http://www.springframework.org/schema/util 
                        http://www.springframework.org/schema/util/spring-util-4.1.xsd   
                        http://www.springframework.org/schema/jdbc 
                        http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd             
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-4.1.xsd    
                        http://www.springframework.org/schema/task 
                        http://www.springframework.org/schema/task/spring-task-4.1.xsd 
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx.xsd 
                        http://www.springframework.org/schema/mvc 
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd" xmlns:cache="http://www.springframework.org/schema/cache">

    <!-- External Properties Placeholder Configuration -->
    <context:property-placeholder location="classpath*:properties/database-connectivity.properties" order="1" ignore-unresolvable="true" />

    <!-- Configuring Transaction Manager -->
    <tx:annotation-driven proxy-target-class="true" />

    <!--
        ===========================================================
                       SETTINGS/CONFIGURATION BEANS
        ===========================================================
    -->

    <!-- REST template configuration -->
    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>

    <!--
        ===========================================================
                     DATABASE CONNECTIVITY CONFIGURATION
        ===========================================================
    -->

    <!-- Database Connectivity Bean configuration -->
    <bean
        id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="${jdbc.mysql.driverClass}" />
        <property name="url" value="${jdbc.mysql.url}" />
        <property name="username" value="${jdbc.mysql.username}" />
        <property name="password" value="${jdbc.mysql.password}" />

    </bean>

    <!-- Transaction Manager Configuration -->
    <bean
        id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        scope="singleton">

        <property name="dataSource" ref="dataSource" />

    </bean>

    <!--
        ===========================================================
                            BEAN DEFINITIONS
        ===========================================================
    -->

    <!-- User Controller Beans -->

    <bean
        id="usersDAOControllerBean"
        class="com.onclave.papercraft.DAO.tenantDAO.Controller.UsersDAOController">

    </bean>

    <bean
        id="usersDAOImplementationBean"
        class="com.onclave.papercraft.DAO.tenantDAO.Implementation.UsersDAOImplementation">

        <constructor-arg ref="dataSource" />

    </bean>

</beans>

我为这些类编写了一些JUnit测试用例并且它们工作正常,但每当我尝试在GlassFish服务器上部署它时,它都会给我这个错误。完整的Stack Trace说它无法自动装入类中注入的依赖项。我不知道如何解决这个问题。一直试图解决它一整天。这SO post是我遇到的类似问题,但OP没有留下任何解决方案。

请帮忙。谢谢。

部分堆栈跟踪:

WebModule[/Papercraft-web]StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentsController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.onclave.papercraft.DAO.tenantDAO.Controller.UsersDAOController com.onclave.papercraft.controller.student.StudentsController.usersDAOController; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersDAOController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.onclave.papercraft.DAO.tenantDAO.Implementation.UsersDAOImplementation com.onclave.papercraft.DAO.tenantDAO.Controller.UsersDAOController.usersDAOImplementation; nested exception is java.lang.IllegalArgumentException: Can not set com.onclave.papercraft.DAO.tenantDAO.Implementation.UsersDAOImplementation field com.onclave.papercraft.DAO.tenantDAO.Controller.UsersDAOController.usersDAOImplementation to com.sun.proxy.$Proxy301
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:664)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:630)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:678)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:549)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:490)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:244)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1583)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1382)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5732)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:5977)
    at com.sun.enterprise.web.WebModule.start(WebModule.java:691)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1041)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:1024)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:747)
    at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2286)
    at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1932)
    at com.sun.enterprise.web.WebApplication.start(WebApplication.java:139)
    at org.glassfish.internal.data.EngineRef.start(EngineRef.java:122)
    at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:291)
    at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:352)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:500)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
    at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:539)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:535)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:360)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:534)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:565)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:557)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:360)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:556)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1464)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:109)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1846)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1722)
    at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
    at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
    at org.glassfish.grizzly.http.server.StaticHttpHandlerBase.service(StaticHttpHandlerBase.java:189)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
    at java.lang.Thread.run(Thread.java:745)
  

修改   完整的application-context.xml按要求添加。

1 个答案:

答案 0 :(得分:5)

请确保在弹簧环境中将proxy-target-class标记设置为true。

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