Java 自动连线导致spring启动应用程序中出现异常
我在让自动连线注释在我的spring boot应用程序中工作时遇到问题 我收到的错误是“无法自动连接字段。没有类型为的限定bean” 我已经验证了控制器上的代码不是POJO,并且具有Spring注释 我也无法在包之外运行我的主方法。有什么建议吗Java 自动连线导致spring启动应用程序中出现异常,java,spring,spring-boot,Java,Spring,Spring Boot,我在让自动连线注释在我的spring boot应用程序中工作时遇到问题 我收到的错误是“无法自动连接字段。没有类型为的限定bean” 我已经验证了控制器上的代码不是POJO,并且具有Spring注释 我也无法在包之外运行我的主方法。有什么建议吗 package com.xxx.controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.S
package com.xxx.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
@SpringBootApplication
public class SampleWebJspApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SampleWebJspApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleWebJspApplication.class, args);
}
}
用户服务类
package com.xxx.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xxx.entity.User;
import com.xxx.repository.UserRepository;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> findAll() {
return userRepository.findAll();
}
}
package com.xxx.service;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Service;
导入com.xxx.entity.User;
导入com.xxx.repository.UserRepository;
@服务
公共类用户服务{
@自动连线
私有用户存储库用户存储库;
公共列表findAll(){
返回userRepository.findAll();
}
}
用户存储库类
package com.xxx.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.xxx.entity.User;
public interface UserRepository extends JpaRepository<User, Integer> {
}
package com.xxx.repository;
导入org.springframework.data.jpa.repository.JpaRepository;
导入com.xxx.entity.User;
公共接口用户存储库扩展了JpaRepository{
}
可能有两个原因:
- 组件的包不包括在
包中@ComponentScan
- 存储库的包不包括在
包中@EnableJPARepositories
- 组件的包不包括在
包中@ComponentScan
- 存储库的包不包括在
包中@EnableJPARepositories
- 在@SpringBootApplication下添加@ComponentScan({“com.xxx”}) 或者修改为@SpringBootApplication(scanBasePackages={“com.xxx”})
- 只需将类“SampleWebJspApplication”移动到包“com.xxx”。这可能是最好的解决方案,只要你的这个类看起来不像控制器
- 在@SpringBootApplication下添加@ComponentScan({“com.xxx”}) 或者修改为@SpringBootApplication(scanBasePackages={“com.xxx”})
- 只需将类“SampleWebJspApplication”移动到包“com.xxx”。这可能是最好的解决方案,只要你的这个类看起来不像控制器
- 可能有两个原因: