🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 使用完全注解架在自己的类 applicationContext.xml的bean配置全删除. 注解类: ~~~ @Configuration //表示该类是注解类 @ComponentScan(basePackages = "com.like") //扫描该包 @PropertySource(value = "classpath:config.properties") //加载properties配置文件 public class SpringConfig { @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; @Bean(name = "c3p0") //自己创建c3p0连接池给spring容器 public DataSource createDataSourceC3p0() throws Exception { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass(driver); dataSource.setJdbcUrl(url); dataSource.setUser(username); dataSource.setPassword(password); return dataSource; } //4.3之前要给spring添加一个解析器来解析 ${} @Bean public static PropertySourcesPlaceholderConfigurer createPropertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } } ~~~ 测试: ~~~ ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); DataSource c3p0 = (DataSource) context.getBean("c3p0"); System.out.println(c3p0.getConnection()); ~~~