JAVA-9-[SpringBoot]非web应用程序创建和配置文件读取

文章介绍了如何创建SpringBoot的非web应用程序,通过实现CommandLineRunner接口来启动,并展示了两种读取配置文件的方法:使用@Value注解和配置类。在配置文件application.properties中定义了数据库和Redis的连接信息,然后在代码中通过注解或配置类注入并使用这些属性。
摘要由CSDN通过智能技术生成

SpringBoot 常用读取配置文件的 3 种方法!
Spring Boot非web应用程序的创建方式

1 非web应用

有时有些项目不需要提供web服务,比如跑定时任务的项目,如果都是按照web项目启动,这个时候会浪费一些资源。
1、Spring CommandLinerunner接口实现booot入口类;
2、run()方法覆盖commandlineruner接口,在run方法中编写具体的处理逻辑。

1.1 创建纯java项目的启动依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

</project>

1.2 实现CommandLineRunner接口

Spring Boot的入口类实现CommandLineRunner接口。

package com.net;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainDemo implements CommandLineRunner {
    @Value("${country}")
    private String country1;
    public static void main(String[] args) {
        SpringApplication.run(MainDemo.class, args);
    }

    //重写CommandLineRunner接口中的run方法
    @Override
    public void run(String... args) throws Exception {
        //调用业务方法
        System.out.println("str = " + country1);
    }
}

2 读取配置文件

2.1 配置文件application.properties

mysql.host=192.168.0.1
mysql.port=3306
mysql.username=root
mysql.password=bigdata

redis.host=192.168.0.2
redis.port=3307

2.2 使用@Value读取配置文件

这种方法适用于对象的参数比较少的情况

我们可以直接在对象的属性上使用@Value注解,同时以${}的形式传入配置文件中对应的属性。

package com.net;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainDemo implements CommandLineRunner {
    @Value("${mysql.host}")
    private String host1;
    @Value("${redis.host}")
    private String host2;
    public static void main(String[] args) {
        SpringApplication.run(MainDemo.class, args);
    }

    //重写CommandLineRunner接口中的run方法
    @Override
    public void run(String... args) throws Exception {
        //调用业务方法
        System.out.println("mysql = " + host1);
        System.out.println("redis = " + host2);
    }
}

2.3 使用配置类

在该类的上方使用 @Configuration 注解,将该类作为配置。

2.3.1 MysqlProperties.java

package com.net;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MysqlProperties {
    public String getHost1() {
        return host1;
    }

    @Value("${mysql.host}")
    private String host1;
    @Value("${mysql.port}")
    public String port1;
}

2.3.2 MainDemo.java

如果哪里需要用到,通过@Autowired注入进去就可以获取属性值了。

package com.net;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

@Component
@SpringBootApplication
public class MainDemo implements CommandLineRunner {
    @Autowired
    private MysqlProperties mysqlProperties;

    public static void main(String[] args) {
        SpringApplication.run(MainDemo.class, args);
    }

    //重写CommandLineRunner接口中的run方法
    @Override
    public void run(String... args) throws Exception {
        //调用业务方法
        System.out.println("mysql = " + mysqlProperties.getHost1());
        System.out.println("redis = " + mysqlProperties.port1);
    }
}

3 错误及解决

3.1 Your ApplicationContext is unlikely to start

解决错误:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
原因是代码直接放在默认包里边,比如src\main\java目录下
应该在src\main\java下建立子目录,比如src\main\java\com\test。
这样的话,代码就在com.test这个包下面了,这个错误也就不会再出现了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮皮冰燃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值