Spring boot 字段需要找不到类型为的bean;
结构没有问题。spring引导可以扫描UserMapper,但不能扫描UserService。我试图给我的UserService@Mapper组件,然后它可以被扫描。但我不知道如何使用其他方法让它被扫描。我试过@Service,但没用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
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
}
}在这里输入代码