SpringBoot配置自定义线程池的数据源

来源:互联网 发布:剑灵火炮兰捏脸数据图 编辑:程序博客网 时间:2024/04/19 17:37

加载maven架包支持

pom.xml

[html] view plain copy
  1. <span style="white-space:pre">      </span>!--   
  2.             操作数据源  
  3.             配置oracle数据库  
  4.             然后再加载四要素  
  5.          -->  
  6.         <dependency>  
  7.             <groupId>org.springframework.boot</groupId>  
  8.             <!-- spring-boot-starter-jdbc or spring-boot-starter-data-jpa -->  
  9.             <artifactId>spring-boot-starter-data-jpa</artifactId>  
  10.         </dependency>  
  11.         <dependency>  
  12.           <groupId>oracle</groupId>  
  13.           <artifactId>orcale</artifactId>  
  14.           <version>3.2.8</version>  
  15.             <scope>system</scope>  
  16.             <systemPath>C:\app\Administrator\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar</systemPath>  
  17.         </dependency>  
  18.           
  19.         <!-- 新的数据库连接池架包dbcp -->  
  20.         <dependency>  
  21.           <groupId>commons-dbcp</groupId>  
  22.           <artifactId>commons-dbcp</artifactId>  
  23.           <version>1.4</version>  
  24.         </dependency>  



创建数据源

BeanContainer

[java] view plain copy
  1. package cn.et.boot.lesson01.source;  
  2.   
  3. import javax.sql.DataSource;  
  4.   
  5. import org.apache.commons.dbcp.BasicDataSource;  
  6. import org.springframework.boot.context.properties.ConfigurationProperties;  
  7. import org.springframework.context.annotation.Bean;  
  8. import org.springframework.context.annotation.Configuration;  
  9.   
  10.   
  11.   
  12. /** 
  13.  * 相当于标示该类是一个bean容器 
  14.  * @author Administrator 
  15.  */  
  16. @Configuration  
  17. public class BeanContainer {  
  18.   
  19.     /** 
  20.      * @Bean 
  21.      * 表示该方法是创建一个bean 
  22.      * 方法名是bean的id 
  23.      *  
  24.      * @ConfigurationProperties 
  25.      * 读取application.properties文件 
  26.      * 设置前缀,自动读取application.properties文件中的属性 
  27.      */  
  28.     @ConfigurationProperties(prefix="mysource")  
  29.     @Bean  
  30.     public DataSource dataSource(){  
  31.         BasicDataSource dataSource = new BasicDataSource();  
  32.           
  33.         //dataSource.setDriverClassName(driverClassName)  
  34.         return dataSource;  
  35.     }  
  36.       
  37. }  


application.properties

[plain] view plain copy
  1. #数据源的四要素  
  2. mysource.url=jdbc:oracle:thin:@localhost:1521:orcl  
  3. mysource.username=scott  
  4. mysource.password=tiger  
  5. mysource.driverClassName=oracle.jdbc.OracleDriver  


程序入口

IocController

[java] view plain copy
  1. package cn.et.boot.lesson01.source;  
  2.   
  3. import javax.sql.DataSource;  
  4.   
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.boot.SpringApplication;  
  7. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  8. import org.springframework.web.bind.annotation.RequestMapping;  
  9. import org.springframework.web.bind.annotation.RestController;  
  10.   
  11. /** 
  12.  *  @RestController 
  13.  *      配置消息转换器 Jackson 
  14.  *      同时在Action上添加了@ResponseBody 
  15.  */  
  16. @RestController  
  17. /** 
  18.  * 自动增加spring.xml文件,并且配置自动扫描 
  19.  * 自动增加web.xml 同时在web.xml过滤器、拦截器... 
  20.  *  
  21.  * @EnableAutoConfiguration要替换成@SpringBootApplication 
  22.  * 不然扫描不到dao层,装配不了EmpDaoImpl 
  23.  * 要装配的对象必须位于HelloController类共一个包或它的子包下才可以扫描的到 
  24.  */  
  25. @SpringBootApplication  
  26. public class IocController {  
  27.       
  28.     @Autowired  
  29.     DataSource dataSource;  
  30.       
  31.     /** 
  32.      * 这里是可以直接返回List,@RestController会自动帮我们转换到json 
  33.      * @return 
  34.      */  
  35.     @RequestMapping("/dataSource")  
  36.     public String hello(){  
  37.         return "bean";  
  38.     }  
  39.       
  40.     public static void main(String[] args) {  
  41.         //发布程序的方法入口  
  42.         SpringApplication.run(IocController.class, args);  
  43.     }  
  44. }  
阅读全文
0 0
原创粉丝点击