1,通过@value来注入对应的值,直接在字段上添加@value 获取application.properties文件中的值。

@Configuration
public class DemoConfig { @Value("${jdbc.driver}")
private String driver; @Value("${jdbc.url}")
private String url; @Value("${jdbc.username}")
private String username; @Value("${jdbc.password}")
private String password; public DemoConfig() {
System.out.println(password);
} public String getDriver() {
return driver;
} public void setDriver(String driver) {
this.driver = driver;
} public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} @Override
public String toString() {
return "DemoConfig [driver=" + driver + ", url=" + url + ", username=" + username + ", password=" + password
+ "]";
} }

2,通过配置@ConfigurationProperties(prefix="jdbc") ,通过前缀去找application.properties中的文件,这种情况只要配置文件中的字段名和类中字段名一致即刻,springboot会自动去查找application.properties中的字段属性,去查找对应名字加上前缀的配置。

package com.java.test;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix="jdbc")
public class DemoConfig1 {
private String driver; private String url; private String username; private String password; public String getDriver() {
return driver;
} public void setDriver(String driver) {
this.driver = driver;
} public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} @Override
public String toString() {
return "DemoConfig1 [driver=" + driver + ", url=" + url + ", username=" + username + ", password=" + password
+ "]";
} }

3,controller 的调用,这俩通过配置文件获取字段值的数据。

package com.java.test;

import org.apache.log4j.Logger;
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; @Controller
public class HomeController { @Value("${jdbc.url}")
private String url ; @Autowired
private DemoConfig1 demoConfig1; @Autowired
private DemoConfig demoConfig; private Logger logger = Logger.getLogger(HomeController.class); @RequestMapping("/")
public String index() { logger.info("xx"+demoConfig1.getPassword());
logger.info("xx"+demoConfig.getUrl()); logger.info("this is index");
logger.info("ss"+url);
return "index.html";
} }

4,整个应用的启动文件

package com.java.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication
public class AppConfig implements CommandLineRunner{ @Autowired
private DemoConfig1 demoConfig1; public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(AppConfig.class, args);
String[] beanDefinitionNames = context.getBeanDefinitionNames();
for (int i = 0; i < beanDefinitionNames.length; i++) {
System.out.println(beanDefinitionNames[i]);
}
} @Override
public void run(String... args) throws Exception
{
System.err.println(demoConfig1);
} }

5,配置文件的内容

配置文件内容为:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springboot
jdbc.username=root
jdbc.password=123456

springboot @value 注解的使用的更多相关文章

  1. SpringBoot 常用注解(持续更新)

    SpringBoot 常用注解 @SpringBootApplication @Bean @ComponentScan @ControllerAdvice @ExceptionHandler @Res ...

  2. 常见的springmvc、SpringBoot的注解

    springMvc的常用注解 : @Controller :用于标记在一个类上,使用它标记的类就是一个springmcv Controller对象,分发处理器将会扫描使用了该注解 的类的方法,并检测该 ...

  3. 浅谈SpringBoot核心注解原理

    SpringBoot核心注解原理 今天跟大家来探讨下SpringBoot的核心注解@SpringBootApplication以及run方法,理解下springBoot为什么不需要XML,达到零配置 ...

  4. SpringBoot的注解注入功能移植到.Net平台(开源)

    *:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...

  5. SpringBoot(14)—注解装配Bean

    SpringBoot(14)-注解装配Bean SpringBoot装配Bean方式主要有两种 通过Java配置文件@Bean的方式定义Bean. 通过注解扫描的方式@Component/@Compo ...

  6. SpringBoot 入门篇(二) SpringBoot常用注解以及自动配置

    一.SpringBoot常用注解二.SpringBoot自动配置机制SpringBoot版本:1.5.13.RELEASE 对应官方文档链接:https://docs.spring.io/spring ...

  7. [技术博客] SPRINGBOOT自定义注解

    SPRINGBOOT自定义注解 在springboot中,有各种各样的注解,这些注解能够简化我们的配置,提高开发效率.一般来说,springboot提供的注解已经佷丰富了,但如果我们想针对某个特定情景 ...

  8. Springboot @ConditionalOnProperty注解

    最近看了一段代码其中用到了@ConditionalOnProperty注解,直接没有了解过这个注解,今天看到了顺便了解一下 具体代码如下 @Configuration public class Web ...

  9. Spring SpringMVC SpringBoot SpringCloud 注解整理大全

    Spring SpringMVC SpringBoot SpringCloud 注解整理 才开的博客所以放了一篇以前整理的文档,如果有需要添加修改的地方欢迎指正,我会修改的φ(๑˃∀˂๑)♪ Spri ...

  10. 【实战】Springboot +jjwt+注解实现需登录才能操作

    springboot +jjwt+注解实现需登录才能调用接口 1.开发需要登录才能进行操作的自定义注解NeedLogin,后面可以写在需要登陆后操作的接口上 package com.songzhen. ...

随机推荐

  1. Python源码编译安装,supervisor配置管理

    apt-get remove 会删除软件包而保留软件的配置文件 apt-get purge 会同时清除软件包和软件的配置文件 virtualenv -p /usr/local/bin/python3. ...

  2. AWS 存储服务(三)

    目录 AWS S3 业务场景 挑战 解决方案 S3的好处 S3 属性 存储桶 Buckets 对象 Object S3 特性 S3 操作 可用性和持久性 一致性 S3 定价策略 S3高级功能 存储级别 ...

  3. Egret入门学习日记 --- 第十九篇(书中 8.8~8.10 节 内容)

    第十九篇(书中 8.8~8.10 节 内容) 开始 8.8节. 重点: 1.类型推断. 2.类型强制转换,使其拥有代码提示功能. 3.除了TS自带的类型判断,Egret官方也提供了类型判断的方法. 操 ...

  4. dropout含义与原理

    含义 在训练过程中,对神经网络单元按照一定比例暂时将其丢弃. 原理 由于网络参数过多,训练数据少,或者训练次数过多,会产生过拟合的现象.dropout产生的一个重大原因就是为了避免过拟合. 每一层的神 ...

  5. WebRoot/WEBINF下的classes文件内无法生成编译文件,classes下没有文件,eclipse无法编译项目

    其实主要问题还是一个eclipse配置的问题. 如下图.将这一项的勾取消掉. 转自:https://blog.csdn.net/qq_36443497/article/details/79684231

  6. C语言--函数嵌套调用

    一.实验作业(6分) 本周作业要求: 选一题PTA题目介绍. 学习工程文件应用,设计实现学生成绩管理系统. 学生成绩管理系统要求 设计一个菜单驱动的学生成绩管理程序,管理n个学生m门考试科目成绩,实现 ...

  7. SpringBoot(二)启动原理

    SpringBoot自动配置模块 该配置模块的主要使用到了SpringFactoriesLoader,即Spring工厂加载器,该对象提供了loadFactoryNames方法,入参为factoryC ...

  8. [转帖]CentOS 8.0.1905 和CentOS Stream(滚动发行)正式发布

    CentOS 8.0.1905 和CentOS Stream(滚动发行)正式发布 https://zhuanlan.zhihu.com/p/84001292 还发现openssl 的 版本太高 不兼容 ...

  9. GFS(Google File System,谷歌文件系统)----(1)文件系统简介

    分布式文件系统 系统是构建在普通的.廉价的机器上,因此故障是常态而不是意外 系统希望存储的是大量的大型文件(单个文件size很大) 系统支持两种类型读操作:大量的顺序读取以及小规模的随机读取(larg ...

  10. Django ORM 高性能查询优化

    一.QuerySet 可切片 使用Python 的切片语法来限制查询集记录的数目 .它等同于SQL 的LIMIT 和OFFSET 子句. >>> Entry.objects.all( ...