springboot 프로젝트 시작 후 첫 페이지 주소 인쇄

1009 단어 자바
/**
 * 
 * @author guokaige
 *
 */
@Component
public class BeanFactoryUtil implements ApplicationContextAware {

	protected static ApplicationContext ctx = null;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		ctx = applicationContext;
		this.printAddress();
	}

	public static Object getBean(String beanId) {
		return ctx.getBean(beanId);
	}

	public void printAddress() {
		try {
			String host = InetAddress.getLocalHost().getHostAddress();
			TomcatServletWebServerFactory tomcatServletWebServerFactory = (TomcatServletWebServerFactory) ctx
					.getBean("tomcatServletWebServerFactory");
			int port = tomcatServletWebServerFactory.getPort();
			String contextPath = tomcatServletWebServerFactory.getContextPath();
			System.out.println("---------    ,  : http://" + host + ":" + port + contextPath + "/");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}

}

좋은 웹페이지 즐겨찾기