一個(gè)springboot的項(xiàng)目,使用mavan管理,希望做成模塊化劃分
例如分為service、admin-web、front-web、interface,每個(gè)模塊為一個(gè)獨(dú)立的maven項(xiàng)目
service為最底層公共服務(wù),提供業(yè)務(wù)邏輯處理,數(shù)據(jù)庫(kù)存儲(chǔ)等
其他三個(gè)為控制層,提供頁(yè)面展示、接口等
現(xiàn)在問(wèn)題:每一個(gè)模塊都有自己的配置文件application.yml,整合的時(shí)候怎么將兩個(gè)文件合并,例如,啟動(dòng)admin-web時(shí),加載service中的application.yml
大致項(xiàng)目結(jié)構(gòu)如下:
例如數(shù)據(jù)庫(kù)配置文件放在service的yml中,頁(yè)面thymeleaf相關(guān)配置放在web的yml中,web依賴于service,目前在web的yml中使用spring.profiles.include 可以將service中,對(duì)應(yīng)yml文件引入項(xiàng)目,并且可以實(shí)現(xiàn)各模塊配置文件相互獨(dú)立,但有點(diǎn)問(wèn)題:
問(wèn)題:是否能在service中提供一個(gè)@Configuration,能將service中yml的屬性值注入到spring容器中,而不再需要在web的yml中顯示引入service的yml文件,這樣的話只需要在web的pom中依賴service就可以了,配置文件不需要做處理
項(xiàng)目代碼:
https://gitee.com/soft_xiang/...
數(shù)據(jù)庫(kù)為公網(wǎng)測(cè)試庫(kù),隨時(shí)刪數(shù)據(jù),不要亂來(lái)
====2018.06.19更新=============================================
分模塊配置文件加載問(wèn)題已初步解決,參見我的回答,推薦第二種方法
第二種方法引起的多環(huán)境配置文件加載問(wèn)題暫未解決,走過(guò)路過(guò)的都幫忙看一下
====2018.06.19更新 end=============================================
試過(guò)在module1-service中添加自定義datasource(只支持properties,Yml可能能實(shí)現(xiàn),但還不太會(huì)寫),可以實(shí)現(xiàn)(此種方式不清楚是否會(huì)影響原來(lái)spring datasource機(jī)制,對(duì)spring原理不熟)
代碼如下:
@Component
@ConfigurationProperties(prefix = "spring.datasource")
@PropertySource("classpath:service-jdbc.properties")
public class MyDataSourceProperties {
private String url;
...
}
@EnableConfigurationProperties(MyDataSourceProperties.class)
public class MyDataSourceConfig {
@Autowired
private MyDataSourceProperties myDataSourceProperties;
@Bean
@Primary
public DataSource dataSource() {
return DataSourceBuilder.create(myDataSourceProperties.getClassLoader()).type(myDataSourceProperties.getType()).driverClassName(myDataSourceProperties.determineDriverClassName()).url(myDataSourceProperties.determineUrl()).username(myDataSourceProperties.determineUsername()).password(myDataSourceProperties.determinePassword()).build();
}
}
完整代碼:https://gitee.com/soft_xiang/...
在module1-service中自定義properties bean(此種方法較好,推薦),代碼如下
@Configuration
public class ServiceConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("application-module1-service.yml"));
configurer.setProperties(yaml.getObject());
return configurer;
}
}
由第二種用法引起的新的問(wèn)題:
項(xiàng)目中如果存在多環(huán)境配置文件,如application-module1-service-dev.yml/application-module1-service-test.yml/application-module1-service/-release.yml時(shí),怎樣根據(jù)module1-web中配置的spring.profiles.active加載對(duì)應(yīng)的配置文件?
思路為在加載文件時(shí)使用SpringContextUtil獲取配置文件中的active,在properties()中根據(jù)active加載文件
代碼如下:
SpringContextUtil.java
@Order(Integer.MIN_VALUE)
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext context = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.context = applicationContext;
}
/// 獲取當(dāng)前環(huán)境
public static String getActiveProfile() {
return context.getEnvironment().getActiveProfiles()[0];
}
/// 獲取當(dāng)前環(huán)境
public static String[] getActiveProfiles() {
return context.getEnvironment().getActiveProfiles();
}
}
ServiceConfig
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
String path = "application-module1-service.yml";
try {
String profile = SpringContextUtil.getActiveProfile();
if (StringUtils.isNotBlank(profile)) {
path = "application-module1-service-" + profile + ".yml";
}
}catch (NullPointerException e){
e.printStackTrace();
System.out.println("SpringContextUtil...未加載...");
}
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource(path));//File引入
configurer.setProperties(yaml.getObject());
return configurer;
}
完整代碼:https://gitee.com/soft_xiang/...
然而這里會(huì)有循環(huán)依賴問(wèn)題
運(yùn)行代碼會(huì)有
SpringContextUtil...未加載...
沒有實(shí)現(xiàn)根據(jù)active加載對(duì)應(yīng)配置文件
用application.yml配置無(wú)非也是生產(chǎn)bean, 比如你示例代碼中application-module1-service.yml
:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://140.143.26.196:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true
username: test
可以在編碼中這樣去實(shí)現(xiàn):
@Bean
@Primary
public DataSource dataSource() {
return DataSourceBuilder
.create()
.username("")
.password("")
.url("")
.driverClassName("")
.build();
}
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。