Please indicate the source for reprinting:
http://blog.csdn.net/forezp/article/details/71024153
This article is from Fang Zhipeng's blog
This article mainly introduces how to create a project with multiple modules in springboot. There are two modules in the chestnut, one is the library project, and the other is the main project, which calls libary. The libary jar has a service, and the main project calls this service.
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>
<modules>
<module>libary</module>
<module>application</module>
</modules>
</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 springboot 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.
References
https://spring.io/guides/gs/multi-module/
Source code download
https://github.com/forezp/SpringBootLearning
Excellent article recommendation:
- More springboot tutorials: springBoot unofficial tutorial | Article summary
- More springcoud tutorials: The easiest SpringCloud tutorial ever | Article summary