E-commerce social platform source code please add penguin beg: three five six two forty-seven fifty-nine. This article describes how to create a project with multiple modules in SpringBoot, including two modules, one as libarary. The libary jar has a service, and the main project calls this service.
Create root project
Create a Maven project with the following POM files:
<? 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 - the SNAPSHOT < / version > < packaging > pom < / packaging > < name > springboot - multi - module < / name > < description > Demo projectfor Spring Boot</description>
</project>Copy the code
Note that the Packaging label is a POM attribute.
Create the libary project
The Libary project is maven project, and the packaging label of poM files is 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; }}Copy the code
Provide a method of external exposure:
@Configuration
@EnableConfigurationProperties(ServiceProperties.class)
public class ServiceConfiguration {
@Bean
public Service service(ServiceProperties properties) {
returnnew Service(properties.getMessage()); }}Copy the code
Create a Springbot project
Create a Web service by importing the corresponding dependencies:
@SpringBootApplication
@Import(ServiceConfiguration.class)
@RestController
public class DemoApplication {
private final Service service;
@Autowired
public DemoApplication(Service service) {
this.service = service;
}
@GetMapping("/")
public String home() {
returnservice.message(); } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}Copy the code
E-commerce social platform source code please add penguin beg: three five six two forty-seven fifty-nine