Spring Boot Tutorial (31) Create a springboot project with multiple modules

Create root project

Create a maven project with the pom file:


<?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>com.forezp</groupId>
    <artifactId>springboot-multi-module</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>springboot-multi-module</name>
    <description>Demo project for Spring Boot</description>



</project>

Note that the packaging tag is a pom attribute.

Create libary project

The libary project is a maven project, and the packaging tag of its pom file is the jar attribute. Create a service component that reads the service.message property of the configuration file.

@ConfigurationProperties("service")
public class ServiceProperties {

    /**
     * A message for the service.
     */
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

Provide a method for external exposure:

@Configuration
@EnableConfigurationProperties(ServiceProperties.class)
public class ServiceConfiguration {
    @Bean
    public Service service(ServiceProperties properties) {
        return new Service(properties.getMessage());
    }
}

Create a springbot project

Introduce the corresponding dependencies and create a web service:

@SpringBootApplication
@Import(ServiceConfiguration.class)
@RestController
public class DemoApplication {

    private final Service service;

    @Autowired
    public DemoApplication(Service service) {
        this.service = service;
    }

    @GetMapping("/")
    public String home() {
        return service.message();
    }

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

Add in the configuration file application.properties:

service.message=Hello World

Open a browser to access: http://localhost:8080/ ; the browser displays:

Hello World

The description does refer to the method in libary.

source code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324935585&siteId=291194637