SpringBoot Beans定义 连接池

SpringBoot Beans定义

原有Spring框架,定义Bean方法如下

  1. xml配置
  2. 组件扫描、@Controller、@Service...

原有Spring框架,参数注入方法如下

常用的参数注入有注入基本值/对象

  1. xml配置
  2. @Value、@Autowired、@Resource等

SpringBoot框架,定义Bean方法如下

  1. 利用@Configuration+@Bean
  2. 利用组件扫描@ComponentScan+@Controller\@Service\@Configuration...

SpringBoot框架,参数注入方法如下

  1. Bean对象之间注入就使用@Autowired或@Resource即可
  2. 从配置文件注入基本值@EnableConfigurationProperties(@EnableAutoConfiguration(功能包含前面的EnableConfigurationProperties))+@ConfigurationProperties+@Value("$(key)")

    可以将application.properties中的参数注入到对象中。

@SpringBootApplication注解

该注解主要包含以下功能:

  • @Configuration bean定义
  • @ComponentScan 组件扫描(路径默认是本包和子包路径)
  • @EnableAutoConfiguration 自动配置

@EnableAutoConfiguration自动配置原理

开启Spring自动配置后,会调用spring-boot-autoconfigure.jar进行处理。包中META-INF/spring.factories文件,定义了自动配置启用的功能。

例如:

org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration,\
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\

通过自动配置,底层创建了DispatcherServlet、RequestMappingHanlderMapping、ViewResolver、DataSource、JdbcTemplate对象放入Spring容器,使用时也可以直接注入应用。

SpringBoot连接池

  1. 默认连接池使用方法

    使用方法如下:

    • 在pom.xml中追加spring-boot-stater-jdbc和驱动包支持

      <!-- 追加spring-jdbc/tomcat-jdbc连接池等 -->
      <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
      </dependency>

      <!-- ojdbc6引入采用了build-path -->

    • 在application.properties追加datasource定义

      spring.datasource.username=SCOTT
      spring.datasource.password=TIGER
      spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE
      spring.datasource.driverClassName=oracle.jdbc.OracleDriver
    • 从Spring容器获取dataSource和jdbcTemplate对象

      ApplicationContext ac = 
          SpringApplication.run(BootBeanFactory.class);
      DataSource ds = ac.getBean("dataSource",DataSource.class);
      JdbcTemplate template = ac.getBean("jdbcTemplate",JdbcTemplate.class);
  2. 默认连接池规则

    在引入spring-boot-starter-jdbc后,内部包含了tomcat-jdbc包,里面有tomcat连接池.然后通过自动配置DataSourceAutoConfigurer创建DataSource对象。

    SpringBoot创建默认DataSource时,规则如下:

    • 优先寻找创建Tomcat连接池
    • 如果没有Tomcat连接池,会查找创建HikariCP
    • 如果没有HikariCP连接池,会查找创建dbcp
    • 如果没有dbcp连接池,会查找创建dbcp2
    • 可以使用spring.datasource.type属性指定连接池类型

      spring.datasource.type=org.apache.commons.dbcp.BasicDataSource
      
  3. 多数据源应用

    如果系统需要访问多个不同的数据库,可以手动创建多个连接池对象。(默认连接池不再创建)

    @Configuration
    public class DataSourceConfig {

    @Bean("dbcpDS1")</br>
    @Primary//注入时默认注入该类型对象</br>
    @ConfigurationProperties(prefix="spring.datasource")</br>
    public DataSource createDbcp1(){</br>
    

    // BasicDataSource dbcp = new BasicDataSource();

    // dbcp.setUsername("SCOTT");

    // dbcp.setPassword("TIGER");

    // dbcp.setDriverClassName("oracle.jdbc.OracleDriver");

    // dbcp.setUrl("jdbc:oracle:thin:@localhost:1521:XE");

    // return dbcp;

    DataSource dbcp = DataSourceBuilder.create()

    .type(BasicDataSource.class).build();

    return dbcp;

    }

    }

    当存在多个DataSource对象时,会引起底层注入异常,需要将某一个追加@Primary标记,指定为默认注入对象。

  4. SpringBoot DAO

    自动配置已经默认创建了JdbcTemplate对象,开发者只需要编写实体类、Dao接口、Dao实现类,注入JdbcTemplate使用。

    @Repository
    public class JdbcBookDao implements BookDao{

    @Autowired</br>
    private JdbcTemplate jdbcTemplate;</br></br>
    
    @Override</br>
    public List&lt;Book&gt; findAll() {</br>
        String sql = "select * from xdl_book";</br>
        RowMapper&lt;Book&gt; rowMapper = new BeanPropertyRowMapper&lt;Book&gt;(Book.class);</br>
        return jdbcTemplate.query(sql, rowMapper);</br>
    }</br></br>
    

    }

  5. SpringBoot Mybatis

    引入mybatis-spring-boot-starter集合包,会自动引入mybatis、mybatis-spring等包。

    • 在pom.xml引入mybatis-spring-boot-starter

      <!-- mybatis-spring -->
      <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.2.2</version>
      </dependency>
    • 编写实体类

      public class Product implements Serializable{
      private int id;
      private String name;
      private String keywords;
      private Date add_time;
      public int getId() {
      return id;
      }
      public void setId(int id) {
      this.id = id;
      }
      //其他省略
      }
    • 编写Mapper接口,在接口方法中定义SQL语句

      public interface ProductDao {

      @Select("select * from xdl_product")</br>
      public List&lt;Product&gt; findAll();</br></br>
      

      }

    • 在主启动类追加@MapperScanner标记

      @SpringBootApplication
      @MapperScan(basePackages={"cn.xdl.dao"})//扫描mapper接口创建对象
      public class BootBeanFactory {

      }

    • 获取Spring容器productDao对象使用

      ApplicationContext ac = 
          SpringApplication.run(BootBeanFactory.class);
      ProductDao proDao = ac.getBean("productDao",ProductDao.class);
      List<Product> list = proDao.findAll();
      for(Product pro:list){
      System.out.println(pro.getId()+" "+pro.getName());
      }
posted @ 2018-05-22 14:49  星朝  阅读(481)  评论(0编辑  收藏  举报