运行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