建立一個maven 工程,其pom文件爲:html
<?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>
須要注意的是packaging標籤爲pom 屬性。web
libary工程爲maven工程,其pom文件的packaging標籤爲jar 屬性。建立一個service組件,它讀取配置文件的 service.message屬性。spring
@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; } }
提供一個對外暴露的方法:apache
@Configuration @EnableConfigurationProperties(ServiceProperties.class) public class ServiceConfiguration { @Bean public Service service(ServiceProperties properties) { return new Service(properties.getMessage()); } }
引入相應的依賴,建立一個web服務:瀏覽器
@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); } }
在配置文件application.properties中加入:springboot
service.message=Hello World
打開瀏覽器訪問:http://localhost:8080/;瀏覽器顯示:app
Hello World
說明確實引用了libary中的方法。maven
源碼來源this