栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot读取application.properties

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot读取application.properties

application.properties文件中内容

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm
spring.datasource.username=root
spring.datasource.password=123456789
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.type-aliases-package=com.myspringboot.pojo

SpringBoot启动类

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.myspringboot.mapper")  //@MapperScan 用户扫描MyBatis的Mapper接口
public class App {
    
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
    
    

}

Controller测试类如下:


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@Controller
@RequestMapping("/test")
public class GetApplicationController {
    
    @Value("${spring.datasource.password}")
    private String password;
    
    
    @RequestMapping("/value")
    public String getByValue(Model model) {  //获取属性成功
        System.out.println(password);//获取属性成功
        model.addAttribute("value",password);
        return "readApplication";
    }
    
    @Autowired
    Environment env;
    
    
    @RequestMapping("/env")
    public String getByEnviroment(Model model) {
        String value = env.getProperty("spring.datasource.url");
        model.addAttribute("value",value);
        System.out.println(value);//获取属性成功
        return "readApplication";
    }

}



 

pom.xml文件内容

    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">
    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.10.RELEASE
    

    com.myspringboot
    MySpringBoot02
    0.0.1-SNAPSHOT
    
        1.8
    

    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
 
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
 
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        
 
                
        
            mysql
            mysql-connector-java
        
 
                
        
            com.alibaba
            druid
            1.0.9
        

                
        
            org.springframework.boot
            spring-boot-starter-test
        

        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        

    

readApplication.html





read application


    read application

测试

启动App.class

右键>Run as> Java Application

 

输入网址localhost:8080/test/value

 输入网址localhost:8080/test/env

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/845256.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号