通过构造函数注入创建bean时出错

时间:2018-02-13 13:44:50

标签: java spring illegalargumentexception constructor-injection

我正在学习Spring并尝试构造函数注入。但是这样做时我得到了IllegalArgumentException。

这是我的项目结构 -

enter image description here

applicationContext.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"
         xsi:schemaLocation = "http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="userDataService" class="com.test.service.UserDataServiceImpl">
        <constructor-arg index="0" ref="userData" />
    </bean>

    <bean id="userData" class="com.test.repository.UserDataImpl" />

</beans>

我的类使用main方法加载applicationContext并创建bean。

package com.test.main;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.model.User;
import com.test.service.UserDataService;
import com.test.service.UserDataServiceImpl;


public class ApplicationDriver {
    public static void main(String args[])
    {
        ApplicationContext context = new 
              ClassPathXmlApplicationContext("applicationContext.xml");
        UserDataService userService = 
              context.getBean("userDataService",UserDataServiceImpl.class);

        List<User> users = userService.findAll();
        for(User user : users)
             System.out.println(user);
      }
}

UserDataServiceImpl类 -

package com.test.service;

import java.util.List;

import com.test.model.User;
import com.test.repository.UserData;

public class UserDataServiceImpl implements UserDataService {
     private UserData userData;

     public UserDataServiceImpl(UserData userData){
            this.userData=userData;
            System.out.println("Creating UserDataServiceImpl");

     }

     public UserDataServiceImpl(){
          System.out.println("**** Created ");
     }

    //  public void setUserData(UserData userData){
    //      this.userData = userData;
    //  }
    //  
   @Override
   public User findById(int id){
       return this.userData.findById(id);
   }

    @Override
    public List<User> findAll(){
        return this.userData.findAll();
    }
 }

UserDataImpl类 -

   package com.test.repository;

   import java.util.ArrayList;
   import java.util.List;
   import com.test.model.User;
   import com.test.customexception.*;

   public class UserDataImpl implements UserData {
         List<User> users;

         public UserDataImpl(){
             System.out.println("Creating UserDataImpl");
             this.users = new ArrayList<User>();
             User u1 = new User(1,"A","B","C","D",12331221);
             User u2 = new User(2,"AA","BB","CC","DD",12331221);
             User u3 = new User(3,"a","b","c","d",12331221);
             User u4 = new User(4,"aa","bb","cc","dd",12331221);
             User u5 = new User(5,"Aa","Bb","Cc","Dd",12331221);
             User u6 = new User(6,"aA","bB","cC","dD",12331221);

             users.add(u1);
             users.add(u2);
             users.add(u3);
             users.add(u4);
             users.add(u5);
             users.add(u6);

      }



    @Override
    public User findById(int id){
         for(User user : users)
             if(user.getId() == id)
                 return user;
         return null;
    }

    @Override
    public List<User> findAll(){
        return users;
    }
}

例外 -

Feb 13, 2018 6:50:03 PM 
org.springframework.context.support.AbstractApplicationContext 
prepareRefresh
INFO: Refreshing 
org.springframework.context.support.ClassPathXmlApplicationContext@4b85612c: 
startup date [Tue Feb 13 18:50:03 IST 2018]; root of context hierarchy
Feb 13, 2018 6:50:03 PM 
org.springframework.beans.factory.xml.XmlBeanDefinitionReader 
loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource 
[applicationContext.xml]
Feb 13, 2018 6:50:03 PM 
org.springframework.beans.factory.support.DefaultListableBeanFactory 
preInstantiateSingletons
INFO: Pre-instantiating singletons in 
org.springframework.beans.factory.support. 
DefaultListableBeanFactory@3fb6a447: defining beans 
[userDataService,userData]; root of factory hierarchy
Creating UserDataImpl
Exception in thread "main" java.lang.IllegalArgumentException
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.core. 
LocalVariableTableParameterNameDiscoverer. 
inspectClass(LocalVariableTableParameterNameDiscoverer.java:110)
at org.springframework.core.LocalVariableTableParameterNameDiscoverer. 
getParameterNames(LocalVariableTableParameterNameDiscoverer.java:85)
at org.springframework.beans.factory.support.ConstructorResolver. 
autowireConstructor(ConstructorResolver.java:193)
at 
org.springframework.beans.factory.support. 
AbstractAutowireCapableBeanFactory. 
autowireConstructor(AbstractAutowireCapableBeanFactory.java:1051)
at 
org.springframework.beans.factory.support. 
AbstractAutowireCapableBeanFactory. 
createBeanInstance(AbstractAutowireCapableBeanFactory.java:955)
at 
org.springframework.beans.factory.support. 
AbstractAutowireCapableBeanFactory. 
doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
at 
org.springframework.beans.factory.support. 
AbstractAutowireCapableBeanFactory. 
createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1. 
getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry. 
getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory. 
doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory. 
getBean(AbstractBeanFactory.java:194)
at 
org.springframework.beans.factory.support.DefaultListableBeanFactory. 
preInstantiateSingletons(DefaultListableBeanFactory.java:626)
at org.springframework.context.support.AbstractApplicationContext. 
finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext. 
refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>
(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>
(ClassPathXmlApplicationContext.java:83)
at com.test.main.ApplicationDriver.main(ApplicationDriver.java:16)

ApplicationDriver类中的第16行是 -

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

接口和用户模型类 - 用户数据接口 -

package com.test.repository;

import java.util.List;

import com.test.model.User;

public interface UserData {

     User findById(int id);

     List<User> findAll();

}

UserDataService接口 -

package com.test.service;

import java.util.List;

import com.test.model.User;

public interface UserDataService {

    User findById(int id);

    List<User> findAll();

}

数据模型类 -

package com.test.model;

public class User {
private String address;
private String email;
private String firstName;
private int id;
private String lastName;
private long phone;

public User(int id, String firstName, String lastName, String address, 
            String email, long phone) {
    super();
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.address = address;
    this.email = email;
    this.phone = phone;
}

public String getAddress() {
    return address;
}

public String getEmail() {
    return email;
}

public String getFirstName() {
    return firstName;
}

public int getId() {
    return id;
}

public String getLastName() {
    return lastName;
}

public long getPhone() {
    return phone;
}

public void setAddress(String address) {
    this.address = address;
}

public void setEmail(String email) {
    this.email = email;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public void setId(int id) {
    this.id = id;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public void setPhone(long phone) {
    this.phone = phone;
}

@Override
public String toString(){
    return new String("User : "+this.getId()+" "+this.getFirstName()+" 
    "+this.getLastName()+" "+this.getEmail());
 }

}

0 个答案:

没有答案