IllegalArgumentException:通过使用Spring中的@Inject参数类型不匹配

时间:2018-10-21 13:28:40

标签: java spring exception

我的项目分为两个部分。第一部分是“ memberManagement-angular”,第二部分是“ memberManagemnt-service”。 对于memberManagement-angular,我有一个“ spring-mvc.xml”,对于mamberManagement-service,有一个“ sping-config.xml”。

spring-mvc.xml如下所示:

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- Enable annotation-driven configuration for Spring MVC -->
    <mvc:annotation-driven/>

    <!-- Enable annotation-based component scanning for REST controllers -->
    <context:component-scan base-package="de.nordakademie.iaa.memberManagement.controller"/>

    <bean id="session" class="de.nordakademie.iaa.memberManagement.controller.Session" scope="session">
        <aop:scoped-proxy/>
    </bean>

</beans>

spring-config.xml看起来像这样:

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

    <!-- Enable annotation config -->
    <context:annotation-config/>

    <!-- Data source -->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="org.h2.Driver"/>
        <property name="url" value="jdbc:h2:~/tmp/db/memberManagement"/>
        <property name="username" value="sa"/>
        <property name="password" value=""/>
    </bean>

    <!-- JPA config -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="de.nordakademie.iaa.memberManagement.model"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
    </bean>

    <!-- Transaction configuration -->

    <!-- Transaction manager -->
    <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <!-- Transaction advice -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <!-- AOP configuration with pointcut -->
    <aop:config>
        <aop:pointcut id="serviceMethods" expression="execution(* de.nordakademie.iaa.memberManagement.service.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
    </aop:config>

    <!-- Data access objects -->
    <bean id="roomDAO" class="de.nordakademie.iaa.memberManagement.dao.RoomDAO"/>
    <bean id="accountDAO" class="de.nordakademie.iaa.memberManagement.dao.AccountDAO"/>
    <bean id="addressDAO" class="de.nordakademie.iaa.memberManagement.dao.AddressDAO"/>
    <bean id="bankAccountDAO" class="de.nordakademie.iaa.memberManagement.dao.BankAccountDAO"/>
    <bean id="memberDAO" class="de.nordakademie.iaa.memberManagement.dao.MemberDAO"/>
    <bean id="membershipTypeDAO" class="de.nordakademie.iaa.memberManagement.dao.MembershipTypeDAO"/>
    <bean id="pseronDAO" class="de.nordakademie.iaa.memberManagement.dao.PersonDAO"/>
    <bean id="roleDAO" class="de.nordakademie.iaa.memberManagement.dao.RoleDAO"/>
    <bean id="statusMembershipDAO" class="de.nordakademie.iaa.memberManagement.dao.StatusMembershipDAO"/>
    <bean id="transactionDAO" class="de.nordakademie.iaa.memberManagement.dao.TransactionDAO"/>
    <bean id="userDAO" class="de.nordakademie.iaa.memberManagement.dao.UserDAO"/>

    <!-- Services -->
    <bean id="roomService" class="de.nordakademie.iaa.memberManagement.service.RoomServiceImpl"/>
    <bean id="accountService" class="de.nordakademie.iaa.memberManagement.service.AccountService"/>
    <bean id="bankAccountService" class="de.nordakademie.iaa.memberManagement.service.BankAccountService"/>
    <bean id="memberService" class="de.nordakademie.iaa.memberManagement.service.MemberService"/>
    <bean id="membershipTypeService" class="de.nordakademie.iaa.memberManagement.service.MembershipTypeService"/>
    <bean id="personService" class="de.nordakademie.iaa.memberManagement.service.PersonService"/>
    <bean id="roleService" class="de.nordakademie.iaa.memberManagement.service.RoleService"/>
    <bean id="statusMembershipService" class="de.nordakademie.iaa.memberManagement.service.StatusMembershipService"/>
    <bean id="transactionService" class="de.nordakademie.iaa.memberManagement.service.TransactionService"/>
    <bean id="userService" class="de.nordakademie.iaa.memberManagement.service.UserService"/>

</beans>

最后,我的memberManagement-service部分再次分为3个文件夹,分别为“ DAO”,“ Model”和“ Service”。

我确实有一个名为“ RoomController”的Controller类,其中包含一个“ RoomDAO”,“ Room”(模型类),“ RoomServiceImplementation”和“ RoomService”(RoomServiceImplementation的接口)。这完全可以正常工作。 因此,我为“成员”实现了具有相同内容(完全相同)的相同类。为此,我收到以下错误消息:

21-Oct-2018 15:07:22.636 WARNUNG [http-nio-8085-exec-3] org.springframework.web.context.support.XmlWebApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt
     org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'memberController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void de.nordakademie.iaa.memberManagement.controller.MemberController.setMemberService(de.nordakademie.iaa.memberManagement.service.MemberService); nested exception is java.lang.IllegalArgumentException: argument type mismatch
        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:305)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
        at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:633)
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:681)
        at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:552)
        at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
        at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
        at javax.servlet.GenericServlet.init(GenericServlet.java:158)
        at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1183)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)
        at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:779)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:133)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1410)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:748)
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void de.nordakademie.iaa.memberManagement.controller.MemberController.setMemberService(de.nordakademie.iaa.memberManagement.service.MemberService); nested exception is java.lang.IllegalArgumentException: argument type mismatch
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:659)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
        ... 37 more
    Caused by: java.lang.IllegalArgumentException: argument type mismatch
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:652)
        ... 39 more

    21-Oct-2018 15:07:22.654 INFORMATION [http-nio-8085-exec-3] org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.destroy Closing JPA EntityManagerFactory for persistence unit 'default'
    21-Oct-2018 15:07:22.686 SCHWERWIEGEND [http-nio-8085-exec-3] org.springframework.web.servlet.DispatcherServlet.initServletBean Context initialization failed
     org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'memberController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void de.nordakademie.iaa.memberManagement.controller.MemberController.setMemberService(de.nordakademie.iaa.memberManagement.service.MemberService); nested exception is java.lang.IllegalArgumentException: argument type mismatch
        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:305)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
        at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:633)
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:681)
        at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:552)
        at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
        at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
        at javax.servlet.GenericServlet.init(GenericServlet.java:158)
        at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1183)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)
        at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:779)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:133)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1410)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:748)
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void de.nordakademie.iaa.memberManagement.controller.MemberController.setMemberService(de.nordakademie.iaa.memberManagement.service.MemberService); nested exception is java.lang.IllegalArgumentException: argument type mismatch
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:659)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
        ... 37 more
    Caused by: java.lang.IllegalArgumentException: argument type mismatch
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:652)
        ... 39 more

MemberController:

package de.nordakademie.iaa.memberManagement.controller;

import de.nordakademie.iaa.memberManagement.model.Member;
import de.nordakademie.iaa.memberManagement.service.MemberService;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.util.List;

/**
 * REST controller for the member service.
 */
@RestController
public class MemberController {

    /**
     * The member service.
     */
    @Inject
    public void setMemberService(MemberService memberService) {
        this.memberService = memberService;
    }

    private MemberService memberService;

    @Inject
    public void setSession(Session session) {
        this.session = session;
    }

    private Session session;

    /**
     * Loads and returns a single member by given identifier.
     *
     * @param id The member's identifier.
     * @return a member or {@code null} if no member was found.
     */
    @RequestMapping(value = "/member/{id}", method = RequestMethod.GET)
    public Member loadMember(@PathVariable Long id) {
        return memberService.loadMember(id);
    }

    /**
     * List all existing members.
     *
     * @return the list of members.
     */
    @RequestMapping(value = "/members", method = RequestMethod.GET)
    public List<Member> listMembers() {
        System.out.println(session.isTouched());
        session.setTouched(true);

        return memberService.loadAllMembers();
    }

    /**
     * Saves the given member.
     *
     * @param member The member to be saved.
     * @return the updated member object.
     */
    @RequestMapping(value = "/members", method = RequestMethod.PUT)
    public Member saveMember(@RequestBody Member member) {
        memberService.saveMember(member);
        return member;
    }

    /**
     * Deletes the member with the given identifier.
     *
     * @param id The member's identifier.
     */
    @RequestMapping(value = "/members/{id}", method = RequestMethod.DELETE)
    public void deleteMember(@PathVariable Long id) {
        memberService.deleteMember(id);
    }


}

0 个答案:

没有答案