spring boot - How to access properties from dependent jar -
i've core project handles common concerns , applies aspects, called "common ". service projects uses core project common processes.
i have manage exception handling in "common " , have read messages properties file in common. services can have custom messages too.
so put messageutils in common
messageutils.java @component public class messageutils { @autowired private messagesource messagesource; private static messagesourceaccessor accessor; @postconstruct private void init() { accessor = new messagesourceaccessor(messagesource, locale.english); } public static string getmessage(string messagekey) { return accessor.getmessage(messagekey); }
}
in common, have common-messages.properties
|-src/main/resources |--common-messages_en.properties
lets service named service_1 depends common.
service_1 have service-messages_en.properties
|-src/main/resources |--service-messages_en.properties
to inject common specific configurations i've declared config class in common , imported in service's spring boot initializer class. defined "common-messages_en.properties" property source in commonconfig.
commonconfig.java
@componentscan({ "com.example.common.core"}) @entityscan({ "com.example.common.entity" }) @enablejparepositories(basepackages = { "com.example.common.dao" }) @propertysource(value = { "classpath:common-messages_en.properties"}) public class commonconfig { }
so, in service_1 i've bootapp class starts springboot app. commonconfig imported there.
@springbootapplication @import(commonconfig.class) public class bootapp extends springbootservletinitializer { public static void main(string[] args) { springapplication.run(bootapp.class, args); } }
when call
messageutils.getmessage("somemessagekey.thatdefinedin.service");
it's ok, can read service's messages.
but when want read common message in service;
messageutils.getmessage("somemessagekey.thatdefinedin.common");
it gets nosuchmessageexception.
how can merge different property files in different jars ?
ok. when initalize reloadableresourcebundlemessageresource while booting that;
@bean public reloadableresourcebundlemessagesource messagesource() { reloadableresourcebundlemessagesource messagesource = new reloadableresourcebundlemessagesource(); messagesource.setbasenames("classpath:service-messages","classpath:common-messages"); messagesource.setcacheseconds(3600); //refresh cache once per hour return messagesource; }
(supposing default locale "en") can access both common , service messages @ runtime.
Comments
Post a Comment