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

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条)

报告相同问题?

悬赏问题

  • ¥30 51单片机C语言数码管驱动单片机为AT89C52
  • ¥100 只改动本课件的 cal_portfolio_weight_series(decision_date), 跑完本课件。设计一个信息比率尽量高的策略。
  • ¥20 如何在visual studio 2022中添加ImageMagick库
  • ¥50 如何实现uniapp编译的微信小程序做可回溯视频
  • ¥15 求Houdini使用行家,付费。价格面议。
  • ¥15 前端高拍仪调用问题报错
  • ¥15 想用octave解决这个数学问题
  • ¥15 Centos新建的临时ip无法上网,如何解决?
  • ¥15 海康威视如何实现客户端软件对设备语音请求的处理。
  • ¥15 支付宝h5参数如何实现跳转