码迷,mamicode.com
首页 > 编程语言 > 详细

springboot快速开发(简单web)

时间:2019-03-01 23:31:47      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:pom   需要   figure   []   ann   contex   autowire   nec   默认   

这是一个springboot基础配置文件介绍的demo。只涉及到 控制层与前端的简单交互,用于验证代码的畅通。

spring-boot  pom.xml解释

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--springboot的父级依赖,提供了相关的Maven依赖,并省去相关依赖包的版本声明-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xiaoeyu</groupId>
    <artifactId>demo2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo2</name>
    <description>Demo project for Spring Boot</description>
    
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <!--springboot使用的技术依赖-->
    <dependencies>
        <!--spring web需要的相关包:包括spring启动bean,aop,core等等-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--用于连接数据库的一种,默认采用hibernate,我不会用,后面有mybatis的-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--添加数据库驱动依赖,在spring-boot-dependencies中指定-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>
    <!--springboot的编辑插件-->
    <build>
        <plugins>
            <!--将项目搭建成一个可以独立运行的jar包,有内嵌的Tomcat-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

spring-boot配置文件解释:

  在spring-boot中可以使用.properties与.yml两种格式的配置文件,前者的优先级更高

  properties采用键值对的形式,yml采用  键:空格 值   的形式。yml的同一目录下会统一表示。properties的前缀每个都要写

以application.yml格式作为说明。从第一列开始 同一个目录下的会归结到同一列

#本文件是springboot的全局配置文件,入口类启动时会自动加载本文件,同样的名字properties格式优先级更高
server:
  servlet:
#    设置根路径,访问的时候localhost:端口/abc/
    context-path: /abc
spring:
  mvc:
#    后台为时间的类型的参数,可以解析前台传送的yyyy-MM-dd类型的字符串,配置文件只能拦截一种
    date-format: yyyy-MM-dd
#    后台返回时间类型的数据,把时间类型的数据转为yyyy-MM-dd类型的字符串
  jackson:
    date-format: yyyy-MM-dd
  http:
    encoding:
      charset: utf-8
#spring.profiles.active:指定使用哪个环境配置文件。现在这个是主要的配置,如果没有就从用下面dev对应的配置文件(application-xxx.yml)
#本文件的配置为主配置,有些配置:端口号  在开发与实际应用是是不一样的8080,80需要分开设置。这个标签用于指定使用哪种环境参数进行(在指定的配置文件中配置合适的参数)
#文件名有要求,与本文件名前部分一致 application-xxx.yml xxx与active后的一样 profiles: active: dev

开发环境下的配置文件。application-dex.yml

#定义为开发环境配置文件
server:
  #  使用端口号
  port: 8888
logging:
  #  设置日志文件输出位置
  file: e:logs/spring-boot.log
  level:
#    设置指定位置的日志输出级别
    org.apache: debuge
    org.springframework.web: debuge
    org.springframework.boot.web: debuge
#数据源配置,与数据进行连接时的信息
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/bcs?characterEncoding=UTF-8
    username: root
    password:

启动文件解释

  用spring-boot创建的项目,会自动生成一个  项目名+Application的class文件,作为项目的入口类。

package com.xiaoeyu.demo2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;

/*
项目的入口类。类名为: 项目名+Application
在运行其中的main方法时,Spring Boot会自动扫描入口类所在的同级包及其子包中的Bean,所以声明Bean的类不应在入口类所在的包之外。
 */
@SpringBootApplication/*开启springboot的自动配置,在项目启动时自动加载*/
//项目启动时加载的配置文件,application.properties默认加载,别的需要手动指定
//custom.properties是自定义的额配置文件,需要在这里声明,才会被加载到项目中生效
@PropertySource("custom.properties")
public class Demo2Application {
    public static void main(String[] args) {
        //run方法,启动内置的Tomcat服务器
        SpringApplication.run(Demo2Application.class, args);
    }

}

配置文件传值

通过配置文件给属性(单个属性)或者对象属性(多个属性)传值,自己写custom.properties给类中的属性传值

#在类中,可以通过@Value("${app.name}")获取到下面的值,并注入给所修饰的属性
app.name=123456
info.url=jdbc:mysql://localhost:3306
info.username=root
info.password=
info.driverClassName=com.mysql.jdbc.Driver
package com.xiaoeyu.demo2.controller;

import com.xiaoeyu.demo2.pojo.Info;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HepplController {
    @Value("${app.name}")//获取配置文件对应app.name的值,单个属性传值
    private String appName;

    @Autowired//多个属相传值,根据Info对象的类上的注解进行匹配
    private Info info;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        //检测传值
        System.out.println(appName);
        System.out.println(info.toString());
        return "hello";
    }

}

Info实体类,没有特殊意义,仅仅作为传参验证

package com.xiaoeyu.demo2.pojo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 通过配置文件给属性赋值
 */
//与Controller,Service,Repository一样的功能,Component语义更广,三个都可以用这个代替。被修饰的类,在spring启动过程中会被实例化
@Component
//在入口类上,添加了启动时加载的配置文件,把该配置文件中以info开都与属性对应的键的值传递给本类中的属性
@ConfigurationProperties(prefix = "info")
public class Info {
    private String url;
    private String username;
    private String password;
    private String driverClassName;
//set,get,tostring方法省略
}

 

springboot快速开发(简单web)

标签:pom   需要   figure   []   ann   contex   autowire   nec   默认   

原文地址:https://www.cnblogs.com/xiaoeyu/p/10401048.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!