我在 Spring 查找存储库时遇到问题,在 Spring 应用程序停止并抛出另一个异常之前,日志中会抛出此警告:
抛出错误之前发出警告:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'UserREST': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mycom.mobile.respository.userRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=userRepository)}
Spring 应用程序停止时抛出错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userRepository in com.mycom.mobile.controller.UserREST required a bean of type 'com.mycom.mobile.respository.UserRepository' that could not be found.
Action:
Consider defining a bean of type 'com.mycom.mobile.respository.userRepository' in your configuration.
实体
package "com.mycom.mobile.model";
Entity
@Table(name="users")
public class User implements Serializable {
private static final long serialVersionUID = -3009157732242241606L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@Column(name="userName")
private String userName;
@Column(name="userLevel")
private String userLevel;
@Column(name="userCountry")
private String userCountry;
protected User(){
}
public User(String userName, String userLevel, String userCountry){
this.userName = userName;
this.userLevel = userLevel;
this.userCountry = userCountry;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName= userName;
}
public long getUserLevel() {
return this.userLevel;
}
public void setUserLevel(String userLevel) {
this.userLevel= userLevel;
}
public long getUserCountry() {
return this.userCountry;
}
public void setUserCountry(String userCountry) {
this.userLevel= userCountry;
}
}
存储库
package "com.mycom.mobile.repository";
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
}
Controller
package "com.mycom.mobile.controller";
@RestController
@CrossOrigin(origins = "*")
@RequestMapping(value = "v1/api")
public class UserREST {
@Autowired
//@Qualifier("userRepository")
private UserRepository userRepository;
private Logger logger = Logger.getLogger(this.getClass());
UserREST() {
}
@RequestMapping(value = "add", method = RequestMethod.POST)
public ResponseEntity<Object> addUser(@RequestBody User user) {
userRepository.save(user);
List<User> users = Lists.newArrayList(userRepository.findAll());
return new ResponseEntity<Object>(users, HttpStatus.OK);
}
}
主 Spring 应用
package "com.mycom.mobile.UserService";
@SpringBootApplication()
// Search for any controllers that can be found under the below package ..
@ComponentScan(basePackages={"com.mycom.mobile"})
@EnableJpaRepositories(basePackages={"com.mycom.mobile.repository"})
@EntityScan(basePackages= {"com.mycom.mobile.model"})
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
public static String getRootPath() {
File file = new java.io.File(".");
try {
return file.getCanonicalPath();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
}
当我从这里读到时,尝试解决它但仍然遇到同样的问题: http://www.baeldung.com/spring-nosuchbeandefinitionexception#cause-2
尝试添加@Qualifier注释但没有成功。
您能否告诉我我在这里做错了什么,或者为什么即使我在应用程序的配置中定义了存储库也找不到存储库?
编辑: 我已尝试以下链接中的答案,但仍然面临问题:
No qualifying bean of type found for dependency in Spring Boot single table
最佳答案
您可能需要在 UserREST
类中为 userRepository
添加 setter。
关于java - "org.springframework.beans.factory.UnsatisfiedDependencyException"的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49238298/