lbt162020 2021-07-04 13:45 采纳率: 68.8%
浏览 4044
已采纳

spring的注解开发 Cannot load configuration class

运行UserController的时候报错

public class UserController {
    public static void main(String[] args) {
       // ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        ApplicationContext app=new AnnotationConfigApplicationContext(SpringConfiguration.class);
        UserService userService=(UserService) app.getBean("dataSource");
        userService.save();
    }
}

@ComponentScan("com")
@PropertySource("classpath:jdbc.properties")  //用于指定properties文件的配置
public class DataSourceConfiguration {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String user;
    @Value("${jdbc.password}")
    private String password;
    @Bean("dataSource")//把当前方法的返回值作为bean对象存入spring的ioc容器中(以指定名称)
    public DataSource getDateSource() throws PropertyVetoException {
        ComboPooledDataSource dateSource=new ComboPooledDataSource();
        dateSource.setDriverClass(driver);
        dateSource.setJdbcUrl(url);
        dateSource.setUser(user);
        dateSource.setPassword(password);
        return dateSource;
    }
}
@Configuration
//组件扫描
@ComponentScan("com.itheima")
@Import({DataSourceConfiguration.class})
public class SpringConfiguration {

}

Exception in thread "main" java.lang.IllegalStateException: Cannot load configuration class: com.itheima.config.SpringConfiguration
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:414)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:254)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:128)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:88)
    at com.itheima.web.UserController.main(UserController.java:13)
Caused by: java.lang.ExceptionInInitializerError
  • 写回答

5条回答 默认 最新

  • CSDN专家-sinJack 2021-07-04 14:14
    关注

    你只是注入了DataSource的bean对象,并没有注入UserService,所以你获取的是DataSource.
    DataSource dataSource=(DataSource) app.getBean("dataSource");
    如有帮助,请采纳。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路