這篇文章主要介紹在springboot中怎么創建含有多個module的工程,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
栗子中含有兩個 module,一個作為libarary. 工程,另外一個是主工程,調用libary .其中libary jar有一個服務,main工程調用這個服務。
創建一個maven 工程,其pom文件為:
<?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 屬性。
libary工程為maven工程,其pom文件的packaging標簽為jar 屬性。創建一個service組件,它讀取配置文件的 service.message屬性。
@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; } }
提供一個對外暴露的方法:
@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中加入:
service.message=Hello World
打開瀏覽器訪問:http://localhost:8080/;瀏覽器顯示:
Hello World
說明確實引用了libary中的方法。
以上是“在springboot中怎么創建含有多個module的工程”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。