SpringBoot04-使用application.properties配置文件(创建公共类,可重复使用)

本文以Spring Boot配置数据源为例,介绍了具体步骤。首先创建application.properties配置文件,接着创建类加载配置文件数据,使用lombok插件和相关注解读取数据。然后在其他类中加载数据类,最后在测试类中进行测试,通过地址栏输入地址查看结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们这里以配置数据源为例子

第一步:创建一个application.properties配置文件,文件名字必须是这个

文件的内容为以下的内容

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springboot?characterEncoding=utf-8
jdbc.username=root
jdbc.password=1234

第二步:创建一个类以加载配置文件中的数据

我这里使用了lombok插件,@data是这个插件的注解,同时我们加上@ConfigurationProperties(prefix = "jdbc")这个注解,他可以读取配置文件的数据,其中prefix是表明文件数据key的前缀,因为上面的数据中前面都加了jdbc的

@ConfigurationProperties(prefix = "jdbc")
@Data
public class JDBCProperties {
    private String url;

    private String driver;

    private String username;

    private String password;
}

第三步:在其他需要数据的类中加载这个数据类,在使用@EnableConfigurationProperties(JDBCProperties.class)这个注解引入数据类后,我使用的注解的形式来操作这个类中的数据@Autowired

@Configuration
@EnableConfigurationProperties(JDBCProperties.class)
public class JdbcConfig {

   @Autowired
   private JDBCProperties jdbcProperties;

    @Bean
    public DataSource dataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(jdbcProperties.getUrl());
        druidDataSource.setDriverClassName(jdbcProperties.getDriver());
        druidDataSource.setUsername(jdbcProperties.getUsername());
        druidDataSource.setPassword(jdbcProperties.getPassword());
        return druidDataSource;
    }
}

第四步:在测试类中进行测试,我这里是在控制其中进行的打印,启动启动类后再地址栏输入地址即可

@Controller
public class MyController {

    @Autowired
     private DataSource dataSource;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        System.out.println(dataSource);
        return "hello springboot 123 你好 点发";
    }
}

 

可以通过在Spring Boot应用程序中使用`@Value`注解来获取在`application.properties`文件中配置的变量。可以创建一个公共类,如下所示: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class ConfigProperties { @Value("${myapp.config.variable1}") private String variable1; @Value("${myapp.config.variable2}") private int variable2; // getter methods for the variables public String getVariable1() { return variable1; } public int getVariable2() { return variable2; } } ``` 这个类使用`@Value`注解来注入`application.properties`文件中的变量。`@Component`注解将这个类声明为一个Spring组件,使它可以被其他类依赖注入。通过在其他类中注入这个类,就可以方便地获取`application.properties`文件中的配置变量,如下所示: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class MyOtherClass { private ConfigProperties configProperties; @Autowired public MyOtherClass(ConfigProperties configProperties) { this.configProperties = configProperties; } public void someMethod() { String variable1 = configProperties.getVariable1(); int variable2 = configProperties.getVariable2(); // use the variables as needed } } ``` 在上面的例子中,`MyOtherClass`通过构造函数注入`ConfigProperties`类,然后可以使用`ConfigProperties`类中的getter方法获取`application.properties`文件中的变量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值