fix: spring-boot profile별 설정 읽을수 있도록 fix

dev
Jonguk. Lim 6 months ago
parent ea740def67
commit 72b4259712

@ -6,8 +6,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import cokr.xit.foundation.web.AbstractController;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Controller
@RequestMapping(
name = "Nims 관리",
@ -15,7 +16,7 @@ import cokr.xit.foundation.web.AbstractController;
)
public class TestController extends AbstractController {
// FIXME: property injection으로 변경??
@Value("${app.api.host:http://localhost:9076}")
@Value("${app.api.host:}")
private String apiHost;
@RequestMapping(

@ -0,0 +1,111 @@
package cokr.xit.foundation.boot;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.egovframe.rte.fdl.cmmn.trace.LeaveaTrace;
import org.egovframe.rte.fdl.property.impl.EgovPropertyServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.env.Environment;
import org.springframework.util.AntPathMatcher;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.ObjectMapper;
/** xit foundation Bean .
* @author mjkhan
*/
@Configuration
@ComponentScan(basePackages = "cokr.xit")
public class CommonConfig {
@Autowired
Environment env;
/**AntPathMatcher .
* @return AntPathMatcher
*/
@Bean
public AntPathMatcher antPathMatcher() {
return new AntPathMatcher();
}
/**ObjectMapper .
* @return ObjectMapper
*/
@Bean
public ObjectMapper objectMapper() {
ObjectMapper bean = new ObjectMapper();
bean.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
bean.configure(Feature.ALLOW_COMMENTS, true);
return bean;
}
/**LeaveaTrace .
* @return LeaveaTrace
*/
@Bean
public LeaveaTrace leaveaTrace() {
return new LeaveaTrace();
}
//private Yml yml = new Yml("application.yml", "application.yml", env);
@Bean
public Yml yml() {
return new Yml("application.yml", "application.yml", env);
}
/**application.yml MessageSource Bean .
* <pre><code> messageSource:
* basenames:
* - classpath:message/message-common
* - classpath:message/authentication-message
* - classpath:org/egovframe/rte/fdl/property/messages/properties</code></pre>
* @return ReloadableResourceBundleMessageSource
*/
@Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource bean = new ReloadableResourceBundleMessageSource();
bean.setDefaultEncoding("UTF-8");
bean.setCacheSeconds(60);
List<String> basenames = yml().getValues("messageSource.basenames");
if (!basenames.isEmpty())
bean.setBasenames(basenames.toArray(new String[basenames.size()]));
return bean;
}
/**application.yml EgovPropertyServiceImpl Bean .
* <pre><code> propertyService:
* properties: #
* - property0: value0
* - property1: value1
* extFileName: #
* - encoding: UTF-8
* filename: classpath*:properties/your-file-01.properties
* - encoding: UTF-8
* filename: classpath*:properties/your-file-02.properties</code></pre>
* @return EgovPropertyServiceImpl
*/
@Bean
public EgovPropertyServiceImpl propertyService() {
EgovPropertyServiceImpl bean = new EgovPropertyServiceImpl();
Map<String, String> properties = yml().getMap("propertyService.properties");
if (!properties.isEmpty())
bean.setProperties(properties);
Set<?> filenames = yml().getMaps("propertyService.extFileName").stream().collect(Collectors.toSet());
if (!filenames.isEmpty())
bean.setExtFileName(filenames);
return bean;
}
}

@ -0,0 +1,148 @@
package cokr.xit.foundation.boot;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import cokr.xit.foundation.Assert;
/**yml .
* <p>
* @author mjkhan
*/
public class Yml {
private Map<String, ?> source;
private final Environment env;
/** Yml .
* @param rootName
* @param path yml
*/
public Yml(String rootName, String path, Environment environment) {
this.env = environment;
load(rootName, path);
}
/** yml .
* @param rootName
* @param path yml
* @return Yml
*/
public Yml load(String rootName, String path) {
source = null;
try {
List<PropertySource<?>> sources = new YamlPropertySourceLoader()
.load(rootName, new ClassPathResource(path));
if (!sources.isEmpty()) {
source = (Map<String, ?>)sources.get(0).getSource();
}
// Spring-boot profile별 설정이 있는 경우 처리
String[] activeProfiles = env.getActiveProfiles();
for (String profile : activeProfiles) {
PropertySource<?> profileSource = sources.stream()
.filter(ps -> ps.getName().equals(profile))
.findFirst()
.orElse(null); // default to null if no matching profile is found
if (profileSource != null) {
// Merge the properties of the profile into the source map
((Map<String, Object>)source).putAll((Map<String, ?>)profileSource.getSource());
}
}
return this;
} catch (Exception e) {
throw Assert.runtimeException(e);
}
}
/** ( ) .
* @param key .
* <pre><code> spring:
* application:
* name: my-application
* </code></pre>
* @return
*/
public String getValue(String key) {
if (source == null) return "";
Object obj = source.get(key);
return obj != null ? obj.toString() : "";
}
/** ( ) .
* <pre><code> list:
* - item-0
* - item-2
* - item-3</code></pre>
* @param prefix
* @return
*/
public List<String> getValues(String prefix) {
if (source == null) return Collections.emptyList();
return getPrefixed(prefix).stream()
.map(entry -> entry.getValue().toString())
.collect(Collectors.toList());
}
private List<Map.Entry<String, ?>> getPrefixed(String prefix) {
return source.entrySet().stream()
.filter(entry -> entry.getKey().startsWith(prefix))
.collect(Collectors.toList());
}
/** ( ) Map .
* <pre><code> parent:
* - property-0: value-0
* - property-1: value-1
* - property-2: value-2</code></pre>
* @param prefix
* @return Map
*/
public Map<String, String> getMap(String prefix) {
if (source == null) return Collections.emptyMap();
LinkedHashMap<String, String> map = new LinkedHashMap<>();
getPrefixed(prefix).stream().forEach(entry -> putTo(map, entry));
return map;
}
private void putTo(LinkedHashMap<String, String> map, Map.Entry<String, ?> entry) {
String key = entry.getKey();
key = key.substring(key.lastIndexOf(".") + 1);
String val = entry.getValue().toString();
map.put(key, val);
}
/** ( ) Map .
* <pre><code> parent:
* - property-0: value-0.0
* property-1: value-0.1
* - property-0: value-1.0
* property-1: value-1.1</code></pre>
* @param prefix
* @return Map
*/
public List<Map<String, String>> getMaps(String prefix) {
if (source == null) return Collections.emptyList();
return getPrefixed(prefix).stream()
.map(entry -> {
String str = entry.getKey();
return str.substring(0, str.lastIndexOf("."));
})
.collect(Collectors.toSet()).stream()
.map(this::getMap)
.collect(Collectors.toList());
}
}
Loading…
Cancel
Save