Исключение в потоке «main» java.lang.ClassCastException: class
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, CustomerServiceImpl.java, мое основное приложение и мой BeanConfig.java, и я не могу найти проблему.
Я изменил свой BeanConfig, так что он больше не называет Customer явно как Bean и использует ComponentScan.
Основное приложение
Код: Выделить всё
@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();
}
}
Код: Выделить всё
@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);
}
Код: Выделить всё
public class CustomerServiceImpl implements CustomerService {
@Autowired
CustomerDAO customerDao;
public Customer getCustomerByAccountNumber(int accountNumber) {
return customerDao.findById(accountNumber);
}
Подробнее здесь: https://stackoverflow.com/questions/587 ... e-of-loade
Мобильная версия