JAVA-9-[SpringBoot] Non-web application creation and configuration file reading

SpringBoot commonly used 3 ways to read configuration files!
How Spring Boot non-web applications are created

1 Non-web application

Sometimes some projects do not need to provide web services, such as running scheduled tasks, if they are all started according to the web project, some resources will be wasted at this time.
1. The Spring CommandLinerunner interface implements the boot entry class;
2. The run() method covers the commandlinerunner interface, and the specific processing logic is written in the run method.

1.1 Create startup dependencies for pure java projects

<?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 Implement the CommandLineRunner interface

The entry class of Spring Boot implements the CommandLineRunner interface.

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 read the configuration file

2.1 configuration file 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 Use @Value to read configuration files

This method is suitable for the case where the parameters of the object are relatively small

We can directly use the @Value annotation on the properties of the object, and at the same time pass in the corresponding properties in the configuration file in the form of ${}.

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 Using configuration classes

Use the @Configuration annotation above this class to use this class as 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

If you need to use it anywhere, you can get the attribute value by injecting @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 Errors and solutions

3.1 Your ApplicationContext is unlikely to start

Solve the error: Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
The reason is that the code is placed directly in the default package. For example, in the src\main\java directory,
a subdirectory should be created under src\main\java, such as src\main\java\com\test.
In this case, the code is under the com.test package, and this error will not appear again.

Guess you like

Origin blog.csdn.net/qq_20466211/article/details/129947961
Recommended