68 32
68 32

Reputation: 11

Field required a bean of type that could not be found.'

The structure has no problem. The spring-boot can scan UserMapper but can't scan UserService. I tried to give my UserService @Mapper component, then it could be scanned. But I don't know how to use other methods to let it be scanned. I tried @Service but it doesn't work.

package com.mywebprojet.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MywebprojectApplication {
        public static void main(String[] args) {
        SpringApplication.run(MywebprojectApplication.class, args);
    }
}

package com.mywebprojet.springboot.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.mywebprojet.springboot.entity.User;
import com.mywebprojet.springboot.mapper.UserMapper;
import com.mywebprojet.springboot.service.UserService;
@RestController
@RequestMapping(value = "user")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;
}



package com.mywebprojet.springboot.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.mywebprojet.springboot.entity.User;
@Service
public interface UserService{
    void insert(User user);
}

Upvotes: 1

Views: 1165

Answers (1)

pepevalbe
pepevalbe

Reputation: 1380

You should write a UserService Implementation and have it annotated with @Service, not the interface. For example:

@Service
public class UserServiceImpl implements UserService {

    @Override
    public void insert(User user) {
        // Implementation
    }
}

Upvotes: 0

Related Questions