Spring Boot学习分享(一)——启动配置篇

Spring Boot启动配置篇

(项目使用maven管理)


使用了一段时间的Spring Boot,发现Spring Boot确实十分的方便快捷,自带的Tomcat以及热部署可以让web项目的开发效率极大的提高。

将Spring Boot和Spring相对比,Spring Boot将Spring所需配置的大量xml文件通过注解的形式进行简化,只留下了一个application.yml,同时也能十分容易地整合其它框架


pom.xml


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <!-- dependencies所需的dependency -->
        <!-- 数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.5</version>
        </dependency>
        <!-- 使用mysql,必须加,否则无法使用mysql,版本号不需要加 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- 测试用 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- web支持配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <!-- tomcat启动配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- 热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>provided</scope>
            <optional>true</optional>
        </dependency>


    <!-- 插件 -->
    <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <!-- maven执行install时不指定单元测试 -->
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                <!-- 热部署开启-->
                    <fork>true</fork>
                </configuration>
            </plugin>

application.yml

spring: 
  #spring mvc的配置,使用jsp需要用到
  #mvc:
    #view:
      #prefix: 
      #suffix: 
  #数据源
  datasource: 
    url: jdbc:mysql://localhost/student?useUnicode=true&characterEncoding=UTF-8&useSSL=true
    username: 
    password: 
    driver-class-name: com.mysql.jdbc.Driver
    # 使用druid 数据源,数据库连接池
    type: com.alibaba.druid.pool.DruidDataSource 
    dbcp2:
      min-idle: 1
      max-idle: 2
      initial-size: 1
      time-between-eviction-runs-millis: 3000
      min-evictable-idle-time-millis: 300000
      #validation-query: SELECT "ZTM" FROM DUAL
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false

启动类,右键点击类名,直接Run As Java Application就可以运行

@SpringBootApplication//标注spring boot的启动入口
@EnableTransactionManagement //打开事务管理功能
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

猜你喜欢

转载自blog.csdn.net/madonghyu/article/details/79590863