zoukankan      html  css  js  c++  java
  • springBoot配置

    概述

      虽然说springBoot讲究“约定大于配置,但有时或多或少也需要一些额外的配置项,比如说数据库配置,队列配置,缓存配置等等。springBoot的全局配置文件为application.properties

    自定义配置

    application.properties,一些常量配置也可以写在这里

    date.year = 2019
    date.month = 7
    date.day = 29
    date.hour = 10
    date.minute = 29

    通过注解 “@Value(${config.name})”绑定到你想要的属性上面去

    package com.xmlxy.firstspringbootproject;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    /*测试类 2019-7-29*/
    @RestController
    public class DemoController
    {
        @Value("${date.year}")
        String year;
    
        @Value("${date.month}")
        String mouth;
    
        @Value("${date.day}")
        String day;
    
        @Value("${date.hour}")
        String hour;
    
        @Value("${date.minute}")
        String minute;
    
        @RequestMapping(value = "/demo",method = RequestMethod.GET)
        public String demo()
        {
            return "Hello SpringBoot";
        }
        @RequestMapping(value = "/demo1",method = RequestMethod.GET)
        public String demo1()
        {
            return year + "." + mouth + "." + day + "  " + hour + ":" + minute;
        }
    }

    通过 127.0.0.1:8080/demo1 访问,可以看到我们配置中的值被绑定到属性中去了

    但是我们会发现,如果需要绑定的值很多,一个个绑定会累到死,所以springBoot提供绑定一个对象bean,新建Config.java

    package com.xmlxy.bean;
    
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Component;
    
    @Component
    @ConfigurationProperties(prefix = "date")
    @Data
    public class Config {
        private String year;
        private String month;
        private String day;
        private String hour;
        private String minute;
    
    }

    如果IDEA没配置lombok可以百度配置一下,这样就省得写get and set的一些函数,然后在改下DemoController.class类

    package com.xmlxy.firstspringbootproject;
    
    import com.xmlxy.bean.Config;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    /*测试类 2019-7-29*/
    @ComponentScan("com.xmlxy.bean")
    @RestController
    public class DemoController
    {
        @Autowired
        private Config config;
    
        @RequestMapping(value = "/demo",method = RequestMethod.GET)
        public String demo()
        {
            return "Hello SpringBoot";
        }
    
        @RequestMapping(value = "/demo1",method = RequestMethod.GET)
        public String demo1()
        {
            return config.getYear() + "." + config.getMonth() + "." + config.getDay() + "  " + config.getHour() + ":" + config.getMinute();
        }
    }

    输出的结果与上面一致。而且在配置文件中各个属性的参数是可以互相引用的

    application.properties

    date.year = 2019
    date.month = 7
    date.day = 29
    date.hour = 14
    date.minute = 54
    date.desc = ${date.year}.${date.month}.${date.day} ${date.hour}:${date.minute}

    DemoController.java新添加一个例

    package com.xmlxy.firstspringbootproject;
    
    import com.xmlxy.bean.Config;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    /*测试类 2019-7-29*/
    @ComponentScan("com.xmlxy.bean")
    @RestController
    public class DemoController
    {
        @Autowired
        private Config config;
    
        @Value("${date.desc}")
        private String desc;
    
        @RequestMapping(value = "/demo",method = RequestMethod.GET)
        public String demo()
        {
            return "Hello SpringBoot";
        }
    
        @RequestMapping(value = "/demo1",method = RequestMethod.GET)
        public String demo1()
        {
            return config.getYear() + "." + config.getMonth() + "." + config.getDay() + "  " + config.getHour() + ":" + config.getMinute();
        }
        @RequestMapping(value = "/demo2",method = RequestMethod.GET)
        public String demo2()
        {
            return desc;
        }
    }

    访问 http://127.0.0.1:8080/demo2

    随机数

    通过${random}来产生随机数,包括int值,long等,支持属性的随机值。

    date.year = 2019
    date.month = 7
    date.day = 29
    date.hour = 14
    date.minute = 54
    date.desc = ${date.year}.${date.month}.${date.day} ${date.hour}:${date.minute}
    date.value = ${random.int}

    DemoController.java

    package com.xmlxy.firstspringbootproject;
    
    import com.xmlxy.bean.Config;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    /*测试类 2019-7-29*/
    @ComponentScan("com.xmlxy.bean")
    @RestController
    public class DemoController
    {
        @Autowired
        private Config config;
    
        @Value("${date.desc}")
        private String desc;
    
        @Value("${date.value}")
        private int value;
    
        @RequestMapping(value = "/demo",method = RequestMethod.GET)
        public String demo()
        {
            return "Hello SpringBoot";
        }
    
        @RequestMapping(value = "/demo1",method = RequestMethod.GET)
        public String demo1()
        {
            return config.getYear() + "." + config.getMonth() + "." + config.getDay() + "  " + config.getHour() + ":" + config.getMinute();
        }
        @RequestMapping(value = "/demo2",method = RequestMethod.GET)
        public String demo2()
        {
            return desc;
        }
    
        @RequestMapping(value = "/demo3",method = RequestMethod.GET)
        public String demo3()
        {
            return String.valueOf(value);
        }
    
    }

    访问 http://127.0.0.1:8080/demo3 得

    自定义配置文件

     引入自定义的配置文件jdbc.properties

    jdbc.url = 10.105.9.119

    将jdbc的配置文件引入

    package com.xmlxy.firstspringbootproject;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.PropertySource;
    
    @SpringBootApplication
    @PropertySource(value = "classpath:jdbc.properties",encoding = "utf-8")
    public class FirstSpringbootProjectApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(FirstSpringbootProjectApplication.class, args);
        }
    
    }

    测试获取下url属性值

    package com.xmlxy.firstspringbootproject;
    
    import com.xmlxy.bean.Config;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    /*测试类 2019-7-29*/
    @ComponentScan("com.xmlxy.bean")
    @RestController
    public class DemoController
    {
        @Autowired
        private Config config;
    
        @Value("${date.desc}")
        private String desc;
    
        @Value("${date.value}")
        private int value;
    
        @Value("${jdbc.url}")
        private String url;
    
        @RequestMapping(value = "/demo",method = RequestMethod.GET)
        public String demo()
        {
            return "Hello SpringBoot";
        }
    
        @RequestMapping(value = "/demo1",method = RequestMethod.GET)
        public String demo1()
        {
            return config.getYear() + "." + config.getMonth() + "." + config.getDay() + "  " + config.getHour() + ":" + config.getMinute();
        }
        @RequestMapping(value = "/demo2",method = RequestMethod.GET)
        public String demo2()
        {
            return desc;
        }
    
        @RequestMapping(value = "/demo3",method = RequestMethod.GET)
        public String demo3()
        {
            return String.valueOf(value);
        }
    
        @RequestMapping(value = "/demo4",method = RequestMethod.GET)
        public String demo4()
        {
            return url;
        }
    }

     jdbc连接数据库

    pom.xml引入jdbc依赖支撑

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
    </dependency>

    application.properties

    date.year = 2019
    date.month = 7
    date.day = 29
    date.hour = 14
    date.minute = 54
    date.desc = ${date.year}.${date.month}.${date.day} ${date.hour}:${date.minute}
    date.value = ${random.int}
    spring.datasource.url = jdbc:mysql://10.105.9.119:3306/hwc_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false
    spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
    spring.datasource.username = root
    spring.datasource.password = DsideaL147258369

    dao接口

    TestDao.java

    package com.xmlxy.dao;
    
    import com.xmlxy.bean.TestData;
    
    public interface TestDao
    {
        int add(TestData testDao);
    }

    TestDaoImp.java

    package com.xmlxy.dao;
    
    import com.xmlxy.bean.TestData;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
    import org.springframework.stereotype.Repository;
    
    import java.util.HashMap;
    import java.util.Map;
    
    @Repository
    public class TestDaoImp implements TestDao
    {
        @Autowired
        private NamedParameterJdbcTemplate jdbcTemplate;
        @Override
        public int add(TestData testData) {
    
            String sql = "INSERT t_hwc_db(name,age) VALUES(:name,:age)";
            Map<String,Object> param = new HashMap<>();
            System.out.printf("本次插入的值===>" + testData.getName() + ",年龄为==>" + testData.getAge());
            param.put("name",testData.getName());
            param.put("age",testData.getAge());
            return jdbcTemplate.update(sql,param);
        }
    }

    访问接口测试

    package com.xmlxy.firstspringbootproject;
    
    import com.xmlxy.bean.Config;
    import com.xmlxy.bean.TestData;
    import com.xmlxy.dao.TestDaoImp;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    /*测试类 2019-7-29*/
    @ComponentScan("com.xmlxy.bean")
    @ComponentScan("com.xmlxy.dao")
    @RestController
    public class DemoController
    {
        @Autowired
        private Config config;
    
        @Value("${date.desc}")
        private String desc;
    
        @Value("${date.value}")
        private int value;
    
        @Value("${jdbc.url}")
        private String url;
    
        @Autowired
        private TestDaoImp testDaoImp;
    
        @RequestMapping(value = "/demo",method = RequestMethod.GET)
        public String demo()
        {
            return "Hello SpringBoot";
        }
    
        @RequestMapping(value = "/demo1",method = RequestMethod.GET)
        public String demo1()
        {
            return config.getYear() + "." + config.getMonth() + "." + config.getDay() + "  " + config.getHour() + ":" + config.getMinute();
        }
        @RequestMapping(value = "/demo2",method = RequestMethod.GET)
        public String demo2()
        {
            return desc;
        }
    
        @RequestMapping(value = "/demo3",method = RequestMethod.GET)
        public String demo3()
        {
            return String.valueOf(value);
        }
    
        @RequestMapping(value = "/demo4",method = RequestMethod.GET)
        public String demo4()
        {
            return url;
        }
    
        @RequestMapping(value = "/demo5",method = RequestMethod.GET)
        public String demo5()
        {
            TestData testData = new TestData();
            testData.setName("ipad");
            testData.setAge(18);
            testDaoImp.add(testData);
            return "插入成功";
        }
    
    
    
    }

    访问 http://127.0.0.1:8080/demo5 查看数据库,可以发现数据已经插入

  • 相关阅读:
    vue框架的vue-router路由的运用
    如何用vue的computed的set和get方法
    如何学习vue的计算属性computed
    从下往上显示的弹出框动画
    vue父传子的使用
    vue局部组件如何使用
    Data 日期-项目(Thread)
    Jframe-项目
    Thread 线程
    Thread知识 (火车票到结束-项目)
  • 原文地址:https://www.cnblogs.com/dslx/p/11251293.html
Copyright © 2011-2022 走看看