Spring中Bean创建报错:WildFly日志显示依赖不满足问题求助
Hey there, let's break down why you're hitting this Spring bean creation error and how to fix it. The core problem here is that Spring can't find a bean matching the CustomerService type your CustomerController is trying to inject—hence the NoSuchBeanDefinitionException in the logs.
Let's start with the most likely culprit in your code:
Looking at your CustomerServiceImpl class, it doesn't implement a corresponding CustomerService interface. Most Spring applications follow the interface-based dependency injection pattern, where controllers inject the interface type (not the concrete class). Without this interface, Spring can't map your service implementation to the dependency your controller expects.
Fix 1: Define the CustomerService interface and have your service implement it
First, create the interface with all the methods your service provides:
package com.edifixio.training.service; import java.util.List; import com.edifixio.training.entity.Customer; public interface CustomerService { List<Customer> getCustomers(); void saveCustomer(Customer theCustomer); Customer getCustomer(int theId); void deleteCustomer(int theId); }
Then update your CustomerServiceImpl to implement this interface:
@Service public class CustomerServiceImpl implements CustomerService { // Add interface implementation here @Autowired private CustomerDAO customerDAO; // Keep all your existing @Transactional methods as-is }
Next, verify Spring can scan your service class:
Fix 2: Ensure component scanning covers your service package
Make sure your Spring configuration (either Java config or XML) includes component scanning for the com.edifixio.training package (or at least com.edifixio.training.service). This is how Spring discovers classes annotated with @Service, @Repository, etc.
- For Java configuration:
@Configuration @ComponentScan(basePackages = "com.edifixio.training") public class AppConfig { // Other bean definitions if needed } - For XML configuration:
<context:component-scan base-package="com.edifixio.training"/>
Double-check the DAO dependency (just to be thorough):
Even though the main error is about CustomerService, it's worth confirming your CustomerDAO is properly registered as a bean too. Ensure your CustomerDAO implementation has the @Repository annotation and is within the scanned package. If the DAO bean is missing, it would also cause a dependency failure in CustomerServiceImpl.
Once you make these changes, redeploy your application on WildFly and the bean creation error should be resolved.
内容的提问来源于stack exchange,提问作者Subhadeep Roy

