• @PropertySource配置的用法


    目录

    功能:

    用法一:@PropertySource + @Value

    用法二:@PropertySource 和 @ConfigurationProperties

    用法三:@ConfigurationProperties + application.yml


    功能:

    • 加载指定的属性文件(*.properties)到 Spring 的 Environment 中。可以配合 @Value 和                                                    @ConfigurationProperties 使用。
    • @PropertySource 和 @Value组合使用,可以将自定义属性文件中的属性变量值注入到当前类的使用@Value注解的成员变量中。
    • @PropertySource 和 @ConfigurationProperties组合使用,可以将属性文件与一个Java类绑   定,将属性文件中的变量值注入到该Java类的成员变量中。

    源码

    1. package org.springframework.context.annotation;
    2. import java.lang.annotation.Documented;
    3. import java.lang.annotation.ElementType;
    4. import java.lang.annotation.Repeatable;
    5. import java.lang.annotation.Retention;
    6. import java.lang.annotation.RetentionPolicy;
    7. import java.lang.annotation.Target;
    8. import org.springframework.core.io.support.PropertySourceFactory;
    9. @Target(ElementType.TYPE)
    10. @Retention(RetentionPolicy.RUNTIME)
    11. @Documented
    12. @Repeatable(PropertySources.class)
    13. public @interface PropertySource {
    14. /**
    15. * 属性源的名称
    16. */
    17. String name() default "";
    18. /**
    19. * 属性文件的存放路径
    20. */
    21. String[] value();
    22. /**
    23. * 如果指定的属性源不存在,是否要忽略这个错误
    24. */
    25. boolean ignoreResourceNotFound() default false;
    26. /**
    27. * 属性源的编码格式
    28. */
    29. String encoding() default "";
    30. /**
    31. * 属性源工厂
    32. */
    33. Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
    34. }

    用法一:@PropertySource + @Value

    属性文件:jdbc.properties

    1. jdbc.driverClassName=jdbc.mysql
    2. jdbc.url=mysql:jdbc://localhost:3306/ssm_db
    3. jdbc.username=root
    4. jdbc.password=123
    1. import org.springframework.beans.factory.annotation.Value;
    2. import org.springframework.context.annotation.PropertySource;
    3. import org.springframework.stereotype.Component;
    4. @Component
    5. @PropertySource(value = {"classpath:jdbc.properties"})
    6. public class JdbcProperty {
    7. private String driverClassName;
    8. private String url;
    9. private String username;
    10. private String password;
    11. public String getDriverClassName() {
    12. return driverClassName;
    13. }
    14. public void setDriverClassName(String driverClassName) {
    15. this.driverClassName = driverClassName;
    16. }
    17. public String getUrl() {
    18. return url;
    19. }
    20. public void setUrl(String url) {
    21. this.url = url;
    22. }
    23. public String getUsername() {
    24. return username;
    25. }
    26. public void setUsername(String username) {
    27. this.username = username;
    28. }
    29. public String getPassword() {
    30. return password;
    31. }
    32. public void setPassword(String password) {
    33. this.password = password;
    34. }
    35. @Override
    36. public String toString() {
    37. return "JdbcProperty{" +
    38. "driverClassName='" + driverClassName + '\'' +
    39. ", url='" + url + '\'' +
    40. ", username='" + username + '\'' +
    41. ", password='" + password + '\'' +
    42. '}';
    43. }
    44. }

    用法二:@PropertySource 和 @ConfigurationProperties

    1. @Component
    2. @PropertySource(value = {"classpath:jdbc.properties"})
    3. @ConfigurationProperties(prefix = "jdbc")
    4. public class JdbcProperty {
    5. private String driverClassName;
    6. private String url;
    7. private String username;
    8. private String password;
    9. public String getDriverClassName() {
    10. return driverClassName;
    11. }
    12. public void setDriverClassName(String driverClassName) {
    13. this.driverClassName = driverClassName;
    14. }
    15. public String getUrl() {
    16. return url;
    17. }
    18. public void setUrl(String url) {
    19. this.url = url;
    20. }
    21. public String getUsername() {
    22. return username;
    23. }
    24. public void setUsername(String username) {
    25. this.username = username;
    26. }
    27. public String getPassword() {
    28. return password;
    29. }
    30. public void setPassword(String password) {
    31. this.password = password;
    32. }
    33. @Override
    34. public String toString() {
    35. return "JdbcProperty{" +
    36. "driverClassName='" + driverClassName + '\'' +
    37. ", url='" + url + '\'' +
    38. ", username='" + username + '\'' +
    39. ", password='" + password + '\'' +
    40. '}';
    41. }

    测试案例:

    1. @RestController
    2. public class UserController {
    3. @Autowired
    4. private UserService userService;
    5. @Autowired
    6. JdbcProperty jdbcProperty;
    7. @PostMapping("/save")
    8. public String save(@RequestParam Map<String, Object> userMap){
    9. System.out.println(jdbcProperty.toString());
    10. // userService.save(userMap);
    11. return "save success";
    12. }
    13. }

    测试结果:

    JdbcProperty{driverClassName='jdbc.mysql', url='mysql:jdbc://localhost:3306/ssm_db', username='root', password='123'}

    用法三:@ConfigurationProperties + application.yml

    DbConfig.java

    1. @Configuration
    2. public class DBConfig {
    3. @Bean
    4. @ConfigurationProperties(prefix = "jdbc")
    5. public DataSource dataSource(){
    6. return new DruidDataSource();
    7. }
    8. @Bean
    9. public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
    10. SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    11. factoryBean.setDataSource(dataSource);
    12. return factoryBean;
    13. }
    14. }

    application.yml

    1. server:
    2. port: 1111
    3. jdbc:
    4. driverClassName: jdbc.mysql
    5. url: mysql:jdbc://localhost:3306/ssm_db
    6. username: root
    7. password: 123

    测试案例:

  • 相关阅读:
    【回顾一下Docker的基本用法】
    【案例实践】HEC-RAS 1D/2D水动力与水环境模拟、HEC-RAS与HEC-FDA耦合、桥梁分析、泄洪道设定
    Qt SQL示例:Books(图书评级)
    STM32使用ThreadX示例以及tx_thread_create解析
    生物化学 核磁共振 氢谱 n+1定律 邻碳耦合 同碳耦合
    百度上海智能研发中心一面
    第二证券|元宇宙赛道西北首个“吃螃蟹者”入场
    Docker 之 基础篇(安装、卸载、阿里云镜像加速、常用命令、发布、应用安装)
    白酒:中国的酒文化的传承与发扬
    讯飞有一个可以根据描述文本自动生成PPT的AI接口,有趣
  • 原文地址:https://blog.csdn.net/tumu6889/article/details/125480908