[英]Embeded Tomcat 9 to execute @PostConstruct
I am creating an executable jar where using Jsf myfaces 2.3 with Tomcat 9 embedded, so when i execute the jar it will read a war file and deploy it on the embedded server, Its reading the bean and displaying contents correctly. 但是 tomcat 似乎沒有執行 @PostConstruct 注釋。
例如:我在 bean 中有以下內容:
@PostConstruct
public void init() {
System.out.println("init bean called..........");
}
但它不執行調用 bean 的 void bean。
我已將 javax.annotation-api-1.3.2 jar 包含在 WEB-INF\Lib 文件夾中。
String contextPath = "/Test";
String warFilePath = "D:\\Test\\embedded\\Test.war";
StandardContext ctx = (StandardContext) tomcat.addWebapp(contextPath, warFilePath);
((StandardJarScanner) ctx.getJarScanner()).setScanAllDirectories(true);
((StandardJarScanner) ctx.getJarScanner()).setScanAllFiles(true);
((StandardJarScanner) ctx.getJarScanner()).setScanClassPath(true);
tomcat.start();
tomcat.getServer().await();
沒有錯誤消息...在控制台上。 根本不執行@PostConstruct init void。 任何幫助將不勝感激。
明白了..只需要啟用命名。
tomcat.enableNaming();
在我的主要空白中:
Tomcat tomcat = new Tomcat();
tomcat.setBaseDir("temp");
tomcat.setPort(8080);
tomcat.enableNaming();
tomcat.getConnector(); // Tomcat 9 we need to get the connector. it not by default.
String contextPath = "/Test";
String warFilePath = "D:\\Test\\embedded\\Test.war";
tomcat.getHost().setAppBase(".");
StandardContext ctx = (StandardContext) tomcat.addWebapp(contextPath, warFilePath);
((StandardJarScanner) ctx.getJarScanner()).setScanAllDirectories(true);
((StandardJarScanner) ctx.getJarScanner()).setScanAllFiles(true);
((StandardJarScanner) ctx.getJarScanner()).setScanClassPath(true);
tomcat.start();
tomcat.getServer().await();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.