我正在尝试遵循Spring Boot的示例,我在互联网上搜索了几个小时而没有找到解决方案的解决方案。大多数解决方案我发现他们说要使用@ComponentScan扫描程序包,我是否缺少任何东西,任何帮助都值得赞赏。
SpringBootApplication类:
package ben;
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan({"services","repository", "web"})
public class SpringBootWebApplication
{
public static void main (String [] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
PersonRepository类:
package ben.repository;
@Repository
public interface PersonRepository extends CrudRepository<Bde, Integer> {
}
PersonService:
package ben.services;
import models.Bde;
public interface PersonService
{
public Iterable <Bde> findAll();
}
PersonServiceImpl:
package ben.services;
@Service
public class PersonServiceImpl implements PersonService
{
@Autowired
private PersonRepository personRepository;
@Override
public Iterable<Bde> findAll()
{
return personRepository.findAll();
}
}
PersonRest类:
package ben.web;
@RestController
public class PersonRest
{
@Autowired
//@Qualifier("PersonServiceImpl")
private PersonService personService;
@RequestMapping("/person")
@ResponseBody
public Iterable <Bde> findAll() {
Iterable <Bde> persons=personService.findAll();
return persons;
}
}
如建议的那样更新包结构:
答案 0 :(得分:3)
您只扫描服务包。
尝试一下...
@ComponentScan(basePackages = { "services", "repository" })
答案 1 :(得分:2)
您将自己限制为软件包service
@ComponentScan("services")
这等于
@ComponentScan(basePackages = "services")
您需要指定所有软件包才能实例化bean
有关如何扫描所有bean(服务,存储库和Web)的示例
@ComponentScan({"services","repository", "web"})
您也可以执行以下操作:
SpringBootWebApplication
类将位于该包的根目录。一个例子:
您的所有应用都位于com.yourapp
上。
如果将SpringBootWebApplication
放在com.yourapp
中,则不再需要@ComponentScan
注释,并且仅使用@SpringBootApplication
就可以简化您的类:
package com.yourapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
答案 2 :(得分:1)
将目录结构更改为以下内容:
ben---- SpringBootWebApplication.java (pkg: ben)
|
----repository (pkg: ben.repository)
| |
| ------ PersonRepository
|
----services (pkg: ben.services)
|
----web (pkg: ben.web)
然后更新您的SpringBootWebApplication类
package ben
@SpringBootApplication
public class SpringBootWebApplication
{
public static void main (String [] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}