Java ClassCastException-客户和Customerservice是否在loader应用程序的未命名模块中?
我正在尝试在我的主应用程序中调用我的客户Bean,以测试我的Bean设置是否正确(我可以看到它们被创建),并得到以下错误: 线程“main”java.lang.ClassCastException中的异常:类 com.r00107892.bank.domain.Customer无法强制转换为类 com.r00107892.bank.services.CustomerService (com.r00107892.bank.domain.Customer和 com.r00107892.bank.services.CustomerService位于的未命名模块中 加载程序“app”)位于com.r00107892.bank.MainApp.main(MainApp.java:24) 我已经检查了我的Customer.java、CustomerDAO.java、CustomerDAOImpl.java、CustomerService.java、customerservicecimpl.java、我的mainApp和我的BeanConfig.java,没有发现问题 我更改了BeanConfig,这样它就不再显式地将Customer命名为Bean,而是使用ComponentScan 主应用程序Java ClassCastException-客户和Customerservice是否在loader应用程序的未命名模块中?,java,spring,javabeans,Java,Spring,Javabeans,我正在尝试在我的主应用程序中调用我的客户Bean,以测试我的Bean设置是否正确(我可以看到它们被创建),并得到以下错误: 线程“main”java.lang.ClassCastException中的异常:类 com.r00107892.bank.domain.Customer无法强制转换为类 com.r00107892.bank.services.CustomerService (com.r00107892.bank.domain.Customer和 com.r00107892.bank.se
@Configuration
public class MainApp {
public static void main(String[] args) {
AnnotationConfigApplicationContext context= new
AnnotationConfigApplicationContext (BeanConfig.class);
System.out.println("Bean names: " + Arrays.toString(context.getBeanNamesForType(BeanConfig.class)));
CustomerService customerService = (CustomerService) context.getBean("customer");
System.out.println(customerService.getCustomerByAccountNumber('1'));
context.close();
}
}
Customer.java
@Component
public class Customer{
public String name;
public int account;
public Customer() {
}
public Customer(int account, String name){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public int getAccount() {
return account;
}
public void setAccount(int account) {
this.account = account;
}
public void myDetails() {
System.out.println("My name is "+ this.name);
System.out.println("My name is" + this.account);
}
public String toString(String name, int account) {
String sentence = name + " " + account;
return sentence;
}
客户服务
@Service
public interface CustomerService {
Customer getCustomerByAccountNumber(int accountNumber);
}
CustomerServiceImpl
public class CustomerServiceImpl implements CustomerService {
@Autowired
CustomerDAO customerDao;
public Customer getCustomerByAccountNumber(int accountNumber) {
return customerDao.findById(accountNumber);
}
我希望看到1号帐户(已在数据库中)的客户名称被打印出来。应该是:
CustomerService customerService = (CustomerService) context.getBean("customerService");
您没有为bean分配名称,因此默认情况下它将采用类名。
bean是customerService,而不是customer(我想你是说它是一个实体?是的,我是说让customer成为一个实体。当我将CustomerService更改为bean时,它会显示NoSuchBeanDefinition。我最初在BeanConfig中定义了一个名为customer的bean,但这也导致了cast错误。您的配置在哪里?您有
@ComponentScan
吗?我的配置在BeanConfig.class中,并且有:Configuration@ComponentScan(basePackages=“com.r00107892.bank”)公共类BeanConfig{-这与我的主应用程序com.r00107892.bank在同一个包中,你必须用@Service
而不是interfaceCustomerService
来注释CustomerService
当我这样做时,它仍然没有这样的定义。如果我将serviceinpl注释为服务,当我调用我的Bean时,我仍然调用CustomerService cust吗omerService=(CustomerService)context.getBean(“CustomerService”);或者getBean是“客户”