gpt4 book ai didi

java - 类型不匹配 : cannot convert from Object to AccountDAO

转载 作者:行者123 更新时间:2023-12-02 09:18:06 25 4
gpt4 key购买 nike

你好,我正在做一个简单的 spring 应用程序,由于某种原因出现了这个错误:

  • 类型不匹配:无法从 Object 转换为 AccountDAO

我有 3 个类(class),AccountDao:

package com.luv2code.aopdemo.dao;

import org.springframework.stereotype.Component;

@Component
public class AccountDAO {

public void addAccount() {

System.out.println(getClass() + ": DOING MY DB WORK: ADDING AN ACCOUNT");

}

}

演示配置:

package com.luv2code.aopdemo;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@EnableAspectJAutoProxy
@ComponentScan("com.luv2code.aopdemo")
public class DemoConfig {



}

和MainDemoApp:

package com.luv2code.aopdemo;


import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.luv2code.aopdemo.dao.AccountDAO;



public class MainDemoApp {

public static void main(String[] args) {

//read spring config java class
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DemoConfig.class);

//get the bean from spring container
AccountDAO theAccountDAO = context.getBean("accountDAO", AccountDAO.class); //THE PROBLE IS HERE

//call the business method
theAccountDAO.addAccount();

//close the context
context.close();

}

}

错误出现在 MainDemoApp 的 AccountDAO theAccountDAO = context.getBean("accountDAO", AccountDAO.class);(只有=后面的部分是红色的)

最佳答案

你能试试这个吗:

AccountDAO theAccountDAO = (AccountDAO) context.getBean("accountDAO", AccountDAO.class);

关于java - 类型不匹配 : cannot convert from Object to AccountDAO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58864429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com