如何在Spring MVC中实现DI。

时间:2017-04-13 13:33:02

标签: java spring spring-mvc

我对Spring MVC中的依赖注入感到困惑。

第一个代码: -

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private Userdao udo;



    @Override
    @Transactional
    public List<Positions> plist() {
        return udo.plist();
    }
}

工作正常。 我想在这个类中实现依赖注入。是做对还是错?

第二个代码: -

`@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private Userdao udo;

    public UserServiceImpl(Userdao udo) {
        this.udo = udo;
    }

    @Override
    @Transactional
    public List<Positions> plist() {
        return udo.plist();
    }
}

它不起作用。ERROR: org.springframework.web.context.ContextLoader - Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xxx.Services.UserService com.xxx.java.HomeController.uservice; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl' defined in file [D:\xxxWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\abcd\WEB-INF\classes\com\xxx\ServiceImp\UserServiceImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.xxx.ServiceImp.UserServiceImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.xxx.ServiceImp.UserServiceImpl.<init>()

我是初学者。请帮助我解决这个问题。如何在Spring MVC中实现DI。

3 个答案:

答案 0 :(得分:1)

你可以这两种方式。

  1. 您可以使用基于字段的自动装配。但在这种情况下,您将需要一个默认构造函数。

    @Service
    @Transactional
    public class UserServiceImpl implements UserService {
    
        @Autowired // field based DI
        private Userdao udo;
    
        // default constructor
        public UserServiceImpl() {}
    
        @Override
        @Transactional
        public List<Positions> plist() {
            return udo.plist();
        }
    }
    
  2. 您可以使用基于构造函数的依赖注入。为此,只需将@Autowire注释移动到构造函数即可。并将其从现场移除。

    @Service
    @Transactional
    public class UserServiceImpl implements UserService {
    
        private Userdao udo;
    
        @Autowired // constructor based DI
        public UserServiceImpl(Userdao udo) {
            this.udo = udo;
        }
    
        @Override
        @Transactional
        public List<Positions> plist() {
            return udo.plist();
        }
    
    }
    

答案 1 :(得分:1)

第一次设置没问题,应该可以使用。

在第二个设置中,您将获得以下异常

class [com.xxx.ServiceImp.UserServiceImpl]: No default constructor found;

这意味着它所说的,因为你已经定义了一个构造函数public UserServiceImpl(Userdao udo) Spring无法找到一个无参数的构造函数。

您可以删除此构造函数并使用第一个构造函数,也可以自己定义无参数构造函数。

您实际上不需要在bean中定义构造函数,因为您不会自己创建bean对象。如果您使用自动布线构造函数参数,则只需要它。

如果你正在尝试自动装配构造函数,那么你可以像下面这样做。

@Service
@Transactional
public class UserServiceImpl implements UserService {

    private Userdao udo;

    @Autowired //autowired constructor, instead of the field
    public UserServiceImpl(Userdao udo) {
        this.udo = udo;
    }

    @Override
    @Transactional
    public List<Positions> plist() {
        return udo.plist();
    }
}

答案 2 :(得分:1)

简单来说:如果你定义一个构造函数(重载)那么你必须在构造函数上使用public class Person { public string FirstName { get; set; } public string MiddleName { get; set; } public string LastName { get; set; } public DateTime DateOfBirth { get; set; } public Person(string fn, string mn, string ln, DateTime dob) { FirstName = fn; MiddleName = mn; LastName = ln; DateOfBirth = dob; } //Other properties, methods, events... } class Program { static void Main() { Person person1 = new Person("Leopold", "", "Hyggesen", yyyy); Console.WriteLine("person1 First name = {0} Middle name = {1} Last name = {2} Date of birth = {3}", person1.FirstName, person1.MiddleName, person1.LastName, person1.DateOfBirth); 注释,如果你没有定义一个构造函数,那么您必须为每个@Autowired使用@Autowired注释作为依赖注入。例如:

构造函数重载:

Object

没有构造函数重载

@Service
@Transactional
public class UserServiceImpl implements UserService {

    private final Userdao userDao;
    private final RoleDao roleDao;

    @Autowired
    public UserServiceImpl(Userdao userDao, RoleDao roleDao) {
        this.userDao = userDao;
        this.roleDao = roleDao;
    }

}

使用单个@Service @Transactional public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Autowired private RoleDao roleDao; // default constructor public UserServiceImpl() {} } 定义构造函数比使用多个对象@Autowired

更好