【问题标题】:ClassCastException - Customer and Customerservice are in unnamed module of loader app?ClassCastException - Customer 和 Customerservice 在加载器应用程序的未命名模块中?
【发布时间】:2019-11-10 22:37:16
【问题描述】:

我试图在我的主应用程序中调用我的客户 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 位于未命名的模块中 com.r00107892.bank.MainApp.main(MainApp.java:24) 处的加载程序'app')

我检查了我的 Customer.java、CustomerDAO.java、CustomerDAOImpl.java、CustomerService.java、CustomerServiceImpl.java、我的 mainApp 和我的 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();
    }
    }

客户.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);

}

客户服务实现

public class CustomerServiceImpl implements CustomerService {

    @Autowired
    CustomerDAO customerDao;


    public Customer getCustomerByAccountNumber(int accountNumber) {
        return customerDao.findById(accountNumber);
   }

我希望看到 1 号帐户(已在数据库中)的客户名称被打印出来。

【问题讨论】:

  • 你必须用@Service注释CustomerServiceImpl而不是接口CustomerService
  • 当我这样做时,它仍然显示 NoSuchBeanDefinition。如果我将 serviceImpl 注释为服务,当我调用我的 Bean 时,我仍然调用 CustomerService customerService = (CustomerService) context.getBean("customerService");或者 getBean 是“客户”

标签: java spring javabeans


【解决方案1】:

应该是:

CustomerService customerService = (CustomerService) context.getBean("customerService");

您没有为 bean 分配名称,因此默认情况下它将采用类名称。 bean 是 customerService 而不是 customer(我假设您的意思是这是一个实体?)

【讨论】:

  • 是的,我的意思是让客户成为一个实体。当我更改为 CustomerService 作为 bean 时,它显示 NoSuchBeanDefinition。
  • 我最初确实在 BeanConfig 中定义了一个名为 customer 的 bean,但这也给出了转换错误。
  • 你的配置在哪里?你有@ComponentScan吗?
  • 我的配置在 BeanConfig.class 中,并且具有: Configuration @ComponentScan(basePackages="com.r00107892.bank") public class BeanConfig { - 这与我的 MainApp 位于同一个包中,即 com .r00107892.bank
猜你喜欢
  • 1970-01-01
  • 2022-01-23
  • 2020-01-05
  • 2021-07-01
  • 1970-01-01
  • 2019-09-03
  • 2018-05-28
  • 2016-06-04
  • 1970-01-01
相关资源
最近更新 更多