JAVA-9-[SpringBoot]非web应用程序创建和配置文件读取
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方法@Overridepublic 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=bigdataredis.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方法@Overridepublic 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 {@Autowiredprivate MysqlProperties mysqlProperties;public static void main(String[] args) {SpringApplication.run(MainDemo.class, args);}//重写CommandLineRunner接口中的run方法@Overridepublic 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这个包下面了,这个错误也就不会再出现了。