{{format('0')}} {{format('247')}} {{format('1047')}}

【Spring】核心概念:注解开发 加载 properties 文件 [ 编程杂谈 ]

大数据男孩 文章 正文

想做一个技术博客,奈何实力不够
分享

明妃

{{nature("2022-09-15 17:45:11")}}更新

说明

Properties文件是Java中很常用的一种配置文件,文件后缀为 .properties,属文本文件,文件的内容格式是键=值的格式,可以用#作为注释,Java编程中用到的地方很多,运用配置文件,可以便于Java深层次的解耦

注解加载 properties 文件

在 配置类 添加注解 PropertySource

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ComponentScan({"cn.bigdataboy.dao","cn.bigdataboy.service"})
@PropertySource({"classpath:jdbc.properties"}) // 可以添加多个配置文件 不允许通配符
//@PropertySource({"jdbc.properties"}) // 可以添加多个配置文件 不允许通配符
public class SpringConfig {
}

通过简单类型注入的方式,可以把参数注入到 Bean

@Service  // 业务层 Bean
public class NameServiceImpl implements NameService {

    @Value("${jdbc.username}") // 占位符
    private String username;

    public void save() {
        System.out.println(this.username + " service save ...");
    }
}

mark

案例代码:https://pan.bigdataboy.cn/s/qB6C6

评论 0
0
{{userInfo.data?.nickname}}
{{userInfo.data?.email}}
TOP 2
Spark 2.0 单机模式与集群模式 安装

{{nature('2020-01-02 16:47:07')}} {{format('12518')}}人已阅读

TOP 3
Office 2016 Pro Plus 激活

{{nature('2019-12-11 20:43:10')}} {{format('9405')}}人已阅读

TOP 4
Linux上 MySQL 开启远程登陆的两种方法

{{nature('2019-12-26 17:20:52')}} {{format('7398')}}人已阅读

TOP 5
Linux 安装 MySQL 5.7

{{nature('2019-12-26 16:03:55')}} {{format('4876')}}人已阅读

目录

标签云

Java Spring 注解

一言

# {{hitokoto.data.from || '来自'}} #
{{hitokoto.data.hitokoto || '内容'}}
作者:{{hitokoto.data.from_who || '作者'}}
自定义UI
配色方案

侧边栏