package com.uas.ps.message; import com.uas.account.web.AccountConfigurer; import com.uas.ps.core.util.ContextUtils; import com.uas.sso.web.SSOConfigurer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Profile; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import java.io.*; import java.util.Properties; import java.util.logging.Logger; /** * 应用入口 * * @author sunyj * @since 2017年8月16日 下午4:00:03 */ @SpringBootApplication @EnableWebMvc public class Application { private static final Logger logger = Logger.getLogger(SSOConfigurer.class.getName()); public static void main(String[] args) throws FileNotFoundException { File logFile = new File("logs/log.log"); if (!logFile.getParentFile().exists()) { logFile.getParentFile().mkdir(); } System.setErr(new PrintStream(new FileOutputStream(logFile, true))); SpringApplication application = new SpringApplication(Application.class); application.addListeners(new ApplicationListener() { @Override public void onApplicationEvent(ApplicationPreparedEvent event) { ContextUtils.setApplicationContext(event.getApplicationContext()); } }); application.run(args); } @Bean(name = "accountConfigurer") @Profile(value = {"dev"}) public AccountConfigurer devAccountConfigurer(ApplicationContext applicationContext) { return accountConfigurer(applicationContext, "classpath:config/account-dev.properties"); } @Bean(name = "accountConfigurer") @Profile(value = {"test", "prod"}) public AccountConfigurer prodAccountConfigurer(ApplicationContext applicationContext) { return accountConfigurer(applicationContext, "classpath:account-prod.properties"); } private AccountConfigurer accountConfigurer(ApplicationContext applicationContext, String location) { AccountConfigurer configurer = new AccountConfigurer(); // 解决Spring Boot应用不支持Resource.getFile方式读取配置文件的问题 Properties prop = getProperties(applicationContext, location); if (prop != null) { configurer.initProperties(prop); } else { logger.severe("Initializing is not available AccountConfigLocation on the classpath"); } return configurer; } private Properties getProperties(ApplicationContext applicationContext, String location) { Properties prop = null; try (InputStream stream = applicationContext.getResource(location).getInputStream()) { prop = new Properties(); prop.load(stream); } catch (IOException e) { e.printStackTrace(); logger.severe(" account read config file error. \n" + e.toString()); } return prop; } }