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());

      }

SpringBoot Beans定义 连接池的更多相关文章

  1. SpringBoot 使用Hikaricp连接池

    1.添加pom.xml依赖 如果是SpringBoot2.0,那么默认的连接池就是Hikaricp,不需要配置 其他的,如果继承 <parent> <groupId>org.s ...

  2. SpringBoot下Druid连接池的使用配置

    Druid是一个JDBC组件,druid 是阿里开源在 github 上面的数据库连接池,它包括三部分: * DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体 ...

  3. springboot使用lettuce连接池

    springboot对连接池的使用非常智能,配置文件中添加lettuce.pool相关配置,则会使用到lettuce连接池,并将相关配置设置为连接池相关参数,(前提是这些参数是springboot配置 ...

  4. springboot整合druid连接池、mybatis实现多数据源动态切换

    demo环境: JDK 1.8 ,Spring boot 1.5.14 一 整合durid 1.添加druid连接池maven依赖 <dependency> <groupId> ...

  5. springboot集成druid连接池

    使用druid连接池主要有几步: 1.添加jar和依赖 <groupId>org.mybatis.spring.boot</groupId> <artifactId> ...

  6. SpringBoot 使用Druid连接池

    1.pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  7. SpringBoot 基于lettuce 连接池 配置redis多数据源操作 生产配置

    添加pom<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons- ...

  8. springboot缓存及连接池配置

    参见https://coding.imooc.com/lesson/117.html#mid=6412 1.springboot的springweb自己默认以及配置好了缓存,只需要在主文件(XxxAp ...

  9. springboot使用druid连接池连接Oracle数据库的基本配置

    #阿里连接池配置 #spring.datasource.druid.driver-class-name=oracle.jdbc.driver.OracleDriver #可配可不配,阿里的数据库连接池 ...

随机推荐

  1. 关于发布WP 8.1应用信息不匹配问题的解决办法

    错误提示:   与此更新关联的程序包标识符与已上传程序包中的标识符不匹配: The package identity associated with this update doesn't match ...

  2. [转]Intellij Idea自动添加注释的方法

    Intellij Idea自动添加注释的方法 阿历Ali 关注 2017.08.20 21:22* 字数 914 阅读 2741评论 0喜欢 6 程序媛阿历终于要写第一篇关于开发的文章了!!! 阿历用 ...

  3. 自欺欺人的使用 NSTimer 销毁

    自欺欺人的使用 NSTimer 销毁 Demo地址 1.NSTimer是要加到runloop中才会起作用. 常见的创建timer方式 // 第一种方式 @property (nonatomic , s ...

  4. Sql Server 2012 分页方法分析(offset and fetch)

    最近在分析 Sql Server 2012 中 offset and fetch 的新特性,发现 offset and fetch 无论语法的简洁还是功能的强大,都是相当相当不错的.其中  offse ...

  5. ABAP和XML数据格式互相转换的两种方式

    ABAP和XML数据格式互相转换是广大开发人员经常遇到的需求.本文介绍两种方式. 1. ABAP提供了一个工具类cl_proxy_xml_transform,通过它的两个方法abap_to_xml_x ...

  6. javaee 第七周作业

    一.什么是JSON? JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式.它基于 ECMAScript (欧洲计算机协会制定的js规范)的一 ...

  7. flask_SQLAlchemy 中常用的过滤和执行器

    常用的SQLAlchemy查询过滤器 过滤器 说明 filter() 把过滤器添加到原查询上,返回一个新查询 filter_by() 把等值过滤器添加到原查询上,返回一个新查询 limit 使用指定的 ...

  8. python 人脸识别试水(一)

    1.安装python,在这里我的版本是python 3.6 2.安装pycharm,我的版本是pycharm 2017 3.安装pip  pip 版本10 4.安装 numpy    :pip ins ...

  9. 使用SpringBoot-JPA进行自定义的保存及批量保存

    更多精彩博文,欢迎访问我的个人博客 说明 SpringBoot版本:2.1.4.RELEASE java版本:1.8 文中所说JPA皆指spring-boot-starter-data-jpa 使用J ...

  10. 【转】C# WinForm中的Label如何换行

    第一种是把Label的AutoSize属性设为False,手动修改Label的大小.这样的好处是会因内容的长度而自动换行,但是当内容的长度超过所设定的大小时,多出的内容就会无法显示.因此,这种方法适合 ...