Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 字段需要找不到类型为的bean;_Spring Boot_Service - Fatal编程技术网

Spring boot 字段需要找不到类型为的bean;

Spring boot 字段需要找不到类型为的bean;,spring-boot,service,Spring Boot,Service,结构没有问题。spring引导可以扫描UserMapper,但不能扫描UserService。我试图给我的UserService@Mapper组件,然后它可以被扫描。但我不知道如何使用其他方法让它被扫描。我试过@Service,但没用 package com.mywebprojet.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure

结构没有问题。spring引导可以扫描UserMapper,但不能扫描UserService。我试图给我的UserService@Mapper组件,然后它可以被扫描。但我不知道如何使用其他方法让它被扫描。我试过@Service,但没用

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);
}

您应该编写一个UserService实现,并用@Service而不是接口对其进行注释。例如:

@Service
public class UserServiceImpl implements UserService {

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

}在此处输入代码

您应该编写一个UserService实现,并用@Service注释它,而不是接口。例如:

@Service
public class UserServiceImpl implements UserService {

@Override
public void insert(User user) {
    // Implementation
}
}在这里输入代码