Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 类型不匹配:无法从对象转换为AccountDAO_Java_Spring - Fatal编程技术网

Java 类型不匹配:无法从对象转换为AccountDAO

Java 类型不匹配:无法从对象转换为AccountDAO,java,spring,Java,Spring,您好,我正在执行一个简单的spring应用程序,出于某种原因,出现了以下错误: 类型不匹配:无法从对象转换为AccountDAO 我有三个班,分别是: package com.luv2code.aopdemo.dao; import org.springframework.stereotype.Component; @Component public class AccountDAO { public void addAccount() { System.ou

您好,我正在执行一个简单的spring应用程序,出于某种原因,出现了以下错误:

  • 类型不匹配:无法从对象转换为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");

    }

}

DemoConfig:

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();

    }

}

错误出现在位于AccountDAO的MainDemoApp中,AccountDAO=context.getBean(“AccountDAO”,AccountDAO.class); (只有=后面的部分是红色的)

您可以尝试以下方法:

AccountDAO theAccountDAO=(AccountDAO)context.getBean(“AccountDAO”,AccountDAO.class)

在调用
context.getBean(…)
之前尝试
context.refresh()
,如果它不能帮助执行以下操作:

  • @SpringBootApplication
    注释
    MainDemoApp.java
  • DemoConfig
    中删除
    @ComponentScan
    ,并显式创建
    @Bean
  • 以调试模式启动,查看是否为
    AccountDAO
    创建bean 我一直试图从buildpath中一个接一个地删除一个库(如果没有任何更改,则读取),只是为了尝试,即使我不知道会产生什么效果。删除org.springframework.context.jar后,错误消失了


    哈哈。有人可以解释一下吗?

    你能用堆栈跟踪显示错误吗?线程“main”java.lang中的异常。错误:未解决的编译问题:类型不匹配:无法从对象转换到com.luv2code.aopdemo.MainDemoApp.main(MainDemoApp.java:18)上的AccountDAO(MainDemoApp.main)(MainDemoApp.java:18)我尝试了这个:线程“main”中的异常java.lang.NoSuchFieldError:org.springframework.context.annotation.AnnotatedBeanDefinitionReader.(AnnotatedBeanDefinitionReader.java:53)org.springframework.context.annotation.AnnotatedBeanDefinitionReader.(AnnotatedBeanDefinitionReader.java:71)org.springframework.context.annotation.AnnotationConfigApplicationContext。(AnnotationConfigApplicationContext.java:66)以及其他一些我无法发布的内容,因为字符限制通常发生在IDE没有关于依赖项的最新信息时。在这种情况下,
    刷新依赖项
    会有所帮助。