java - 创建 bean 时出错

标签 java spring javabeans

我有以下 AccountController 类

@Controller
@RequestMapping("/rest/accounts")
public class AccountController {
private AccountService accountService;

@Autowired
public AccountController(AccountService accountService) {
    this.accountService = accountService;
}
... implemented methods here
} 

AccountService类如下

public interface AccountService {
public Account findAccount(BigInteger id);

public Account createAccount(Account data);

public Account deleteAccount(String email);

public Boolean updateAccount(String email,String password);

public List<Account> findAllAccounts();

// public Account deleteAccount();

public Customer createCustomer(String accountId, Customer data);

public List<Customer> findCustomersByAccount(String accountId);

}

AccountServiceImpl类如下

@Service

公共(public)类 AccountServiceImpl 实现 AccountService{

@Autowired
private AccountRepository accountRepo;

@Autowired
private CustomerRepository custRepo;
..implemented methods here 

}

我的 servlet.xml 有以下组件扫描

<context:component-scan base-package="com.sam.spring.web.rest.mvc" />

/src/resource/context.xml 具有以下内容

<context:component-scan base-package="com.sam.spring.web.core.services"></context:component-scan>
<context:component-scan base-package="com.sam.spring.web.core.repositories.jpa"></context:component-scan>

我收到以下错误:

org.springframework.beans.factory.BeanCreationException: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountController' defined in file ...

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.sam.spring.web.core.services.AccountService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

我在这个设置中做错了什么?

最佳答案

试试这个 <context:component-scan base-package="com.sam.spring.web.rest.mvc, com.sam.spring.web.core.services, com.sam.spring.web.core.repositories.jpa" />

关于java - 创建 bean 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35324482/

相关文章:

Java jackson嵌入式对象反序列化

java - ViewResolver 不解析 View 名称,而是请求 URL,Spring 3.0

spring - @Transactional on Spring shutdown 正确关闭 Hsqldb

java - 无法使用 OpenCSV 将 JavaBeans 写入 CSV

java.lang.IllegalStateException : No thread-bound request found: 错误

java - 如何将重复模式与 Java 正则表达式匹配?

spring - 何时使用 AbstractAnnotationConfigDispatcherServletInitializer 和 WebApplicationInitializer?

java - 如何在Eclipse中使用javabean?

java - jsp中的变量除了java类可以定义在什么地方?

java - 对元素仍在最后位置的列表进行排序