SpringConfiguration.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package com.Config;
  2. import com.alibaba.druid.pool.DruidDataSource;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.ComponentScan;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.context.annotation.PropertySource;
  8. import javax.sql.DataSource;
  9. /**
  10. * spring 核心配置文件类
  11. */
  12. @Configuration
  13. // 组件扫描
  14. //<context:component-scan base-package="com"></context:component-scan>
  15. @ComponentScan({"com"})
  16. @PropertySource("classpath:jdbc.properties")
  17. public class SpringConfiguration {
  18. // 写一个dataSource
  19. @Value("${jdbc.url}")
  20. String jdbcUrl;
  21. @Value("${jdbc.driver}")
  22. String driver;
  23. @Value("${jdbc.username}")
  24. String userName;
  25. @Value("${jdbc.password}")
  26. String password;
  27. @Bean("getJdbcUrl")
  28. public String getJdbcUrl() {
  29. return jdbcUrl;
  30. }
  31. @Bean("dataSource")
  32. public DataSource dataSource() {
  33. DruidDataSource druidDataSource = new DruidDataSource();
  34. druidDataSource.setDriverClassName(driver);
  35. druidDataSource.setUrl(jdbcUrl);
  36. druidDataSource.setUsername(userName);
  37. druidDataSource.setPassword(password);
  38. return druidDataSource;
  39. }
  40. }