当我尝试执行SpringBoot时出现错误。因为我需要一个“ bean”,所以我不明白为什么要得到这个,我有所有注释
17-09-2018 12:24:53.905 [restartedMain] WARN o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext.refresh - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'parameterController': Unsatisfied dependency expressed through field 'pgService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'parameterServiceImp': Unsatisfied dependency expressed through field 'pgRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'es.my.repository.ParameterRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
更多错误:
申请无法开始
Description:
Field pgRepository in es.service.ParameterServiceImp required a bean of type 'es.repository.ParameterRepository' that could not be found.
Action:
Consider defining a bean of type 'es.repository.ParameterRepository' in your configuration.
我在控制器中->使用@Autowired
@RestController
@RequestMapping(value = { "/param" })
@CrossOrigin
public class ParameterController {
@Autowired
ParameterService pgService;
@RequestMapping(method = RequestMethod.GET, value = "/get", produces =
MediaType.APPLICATION_JSON_VALUE)
public List<Parameter> getAllParameters() {
List<Parameter> list = pgService.selectAll();
return list;
}
在我的服务中->我不使用注释
public interface ParameterService {
public List<Parameter> selectAll();
}
Imple->我使用Service和Autowired
@Service
public class ParameterServiceImp implements ParameterService {
@Autowired
ParameterRepository pgRepository;
public List<Parameter> selectAll() {
return pgRepository.findAll());
}
}
存储库->在这里,我有查询。
public interface ParameterRepository extends CrudRepository<Parameter, String> {
}
型号-> 我的POJO
@Entity
@Table(name = "Parameter")
public class Parameter {
@Id
@NotNull
@Column(name = "ID")
private String id;
@NotNull
@Column(name = "name")
private String name;
// getters setters and construct
}
我有@Entity,@Service,@Autowired但出现错误
答案 0 :(得分:1)
如果在未指定@SpringBootApplication
的情况下使用basePackage
,它将默认为当前程序包。就像您添加@ComponentScan
和@EnableJpaRepositories
一样,没有基本包。
如果您将其他软件包设置为@SpringBootApplication
,请确保还添加了具有适当basePackage的@EnableJpaRepositories
。储存库不会仅被@ComponentScan
识别(或通过任何其他显式或隐式方式将其声明为Bean)。
答案 1 :(得分:0)
@SpingbootApplication的.some.package软件包吗? 否则,您需要为基本软件包a.some.package
添加组件扫描注释。答案 2 :(得分:0)
尝试添加下一个注释。
@EnableJpaRepositories(basePackages = {"<repository-package-here>"})